组合框更改JTable

时间:2018-09-22 13:57:35

标签: java mysql swing jtable

一段时间以来,我一直在寻找答案。什么 我想做的是我的组合框显示3个项目:

  1. 学生
  2. 项目
  3. 医生

每个都有各自的表,所以如果我的组合框 向学生显示他们在JTable中显示学生表,依此类推。

1 个答案:

答案 0 :(得分:0)

您可以通过以下示例获取帮助。它可以满足您的需求。尽管这不是一种完美的方法,但我仅作了几处更改以显示其工作原理。

享受:)

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

    public class TableWithComboBox extends JPanel implements ActionListener {
        private boolean DEBUG = true;
        JPanel jPanel2;
        JFrame frame;
        JTable table;
        JComboBox comboBox;

        public TableWithComboBox() {
            super(new GridLayout(2, 0));

            table = new JTable(new MyTableModel());
            table.setPreferredScrollableViewportSize(new Dimension(500, 70));
            table.setFillsViewportHeight(true);

            // Create the scroll pane and add the table to it.
            JScrollPane scrollPane = new JScrollPane(table);

            // Add the scroll pane to this panel.

            String[] options = { "Students", "Doctor" };

            comboBox = new JComboBox(options);
            comboBox.setSelectedIndex(0);
            comboBox.addActionListener(this);

            add(comboBox);
            add(scrollPane);

        }

        public void actionPerformed(ActionEvent e) {
            JComboBox cb = (JComboBox) e.getSource();
            String optionName = (String) cb.getSelectedItem();
            if (optionName.equals("Doctor")) {
                table.setModel(new MyTableDoctorModel());
            } else {
                table.setModel(new MyTableModel());
            }
        }

        class MyTableDoctorModel extends AbstractTableModel {

            String[] columnNames = { "Name", "Department", "Hospital", "Experience" };

            Object[][] data = { { "Saira", "Dep1", "XYZ", new Integer(3) },
                    { "Saira", "Dep1", "XYZ", new Integer(3) },
                    { "Saira", "Dep1", "XYZ", new Integer(3) },
                    { "Saira", "Dep1", "XYZ", new Integer(3) },

            };

            public int getColumnCount() {
                return columnNames.length;
            }

            public int getRowCount() {
                return data.length;
            }

            public String getColumnName(int col) {
                return columnNames[col];
            }

            public Object getValueAt(int row, int col) {
                return data[row][col];
            }

            /*
             * JTable uses this method to determine the default renderer/ editor for
             * each cell. If we didn't implement this method, then the last column
             * would contain text ("true"/"false"), rather than a check box.
             */
            public Class getColumnClass(int c) {
                return getValueAt(0, c).getClass();
            }

            /*
             * Don't need to implement this method unless your table's editable.
             */
            public boolean isCellEditable(int row, int col) {
                // Note that the data/cell address is constant,
                // no matter where the cell appears onscreen.
                if (col < 1) {
                    return false;
                } else {
                    return true;
                }
            }

            /*
             * Don't need to implement this method unless your table's data can
             * change.
             */
            public void setValueAt(Object value, int row, int col) {
                if (DEBUG) {
                    System.out.println("Setting value at " + row + "," + col
                            + " to " + value + " (an instance of "
                            + value.getClass() + ")");
                }

                data[row][col] = value;
                double totalVal = 100.0 + Double.parseDouble(String.valueOf(table
                        .getValueAt(0, 4)));
                fireTableCellUpdated(row, col);

            }

        }

        class MyTableModel extends AbstractTableModel {

            String[] columnNames = { "Name", "Degree", "Board/University",
                    "Year of Passing", "CGPA", "Part-Time" };

            Object[][] data = {
                    { "Saira", "B.Tech", "VTU", new Integer(2015), new Float(8.33),
                            new Boolean(false) },
                    { "Smaira", "B.Sc", "CBSE", new Integer(2007), new Float(7.77),
                            new Boolean(true) },
                    { "John", "M.tech", "IIT", new Integer(2009), new Float(8.77),
                            new Boolean(false) },
                    { "Jia", "M.Sc", "Thapar", new Integer(2011), new Float(7.21),
                            new Boolean(true) },
                    { "Kerry", "B.Com", "DU", new Integer(2014), new Float(8.92),
                            new Boolean(false) }

            };

            public int getColumnCount() {
                return columnNames.length;
            }

            public int getRowCount() {
                return data.length;
            }

            public String getColumnName(int col) {
                return columnNames[col];
            }

            public Object getValueAt(int row, int col) {
                return data[row][col];
            }

            /*
             * JTable uses this method to determine the default renderer/ editor for
             * each cell. If we didn't implement this method, then the last column
             * would contain text ("true"/"false"), rather than a check box.
             */
            public Class getColumnClass(int c) {
                return getValueAt(0, c).getClass();
            }

            /*
             * Don't need to implement this method unless your table's editable.
             */
            public boolean isCellEditable(int row, int col) {
                // Note that the data/cell address is constant,
                // no matter where the cell appears onscreen.
                if (col < 1) {
                    return false;
                } else {
                    return true;
                }
            }

            /*
             * Don't need to implement this method unless your table's data can
             * change.
             */
            public void setValueAt(Object value, int row, int col) {
                if (DEBUG) {
                    System.out.println("Setting value at " + row + "," + col
                            + " to " + value + " (an instance of "
                            + value.getClass() + ")");
                }

                data[row][col] = value;
                double totalVal = 100.0 + Double.parseDouble(String.valueOf(table
                        .getValueAt(0, 4)));

                if (DEBUG) {
                    System.out.println("New value of data:");
                    printDebugData();
                }
            }

            private void printDebugData() {
                int numRows = getRowCount();
                int numCols = getColumnCount();
                for (int i = 0; i < numRows; i++) {
                    System.out.print("    row " + i + ":");
                    for (int j = 0; j < numCols; j++) {
                        System.out.print("  " + data[i][j]);
                    }
                    System.out.println();
                }
                System.out.println("--------------------------");
            }
        }

        /**
         * Create the GUI and show it. For thread safety, this method should be
         * invoked from the event-dispatching thread.
         */
        private void createAndShowGUI(TableWithComboBox newContentPane) {
            // Create and set up the window.
            frame = new JFrame("TableDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new FlowLayout());

            jPanel2 = new JPanel();

            frame.add(jPanel2);

            // Create and set up the content pane.

            frame.add(newContentPane);
            // newContentPane.setOpaque(true); //content panes must be opaque
            // frame.setContentPane(newContentPane);

            // Display the window.
            frame.pack();
            frame.setVisible(true);
        }

        public static void main(String[] args) {
            // Schedule a job for the event-dispatching thread:
            // creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    TableWithComboBox newContentPane = new TableWithComboBox();
                    newContentPane.createAndShowGUI(newContentPane);

                }
            });
        }

    }