【问题标题】:Combo box changes the JTable组合框更改 JTable
【发布时间】:2019-02-26 16:02:24
【问题描述】:

我一直在努力寻找答案。什么 我正在尝试做的是我的组合框显示 3 个项目:

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

每个人都有自己的表格,所以如果我的组合框 向学生展示他们在JTable中展示学生的表格等等。

【问题讨论】:

  • 你的问题是什么?您知道如何将 ActionListener 添加到组合框以在用户选择项目时调用操作吗?你知道如何创建 JTable 吗?首先阅读Swing tutorial。关于 1) How to Use Combo Boxes、2) How to Use Tables 和 3) How to Write an ActionListener 的部分可帮助您开始使用 Swing 基础知识。
  • 或者是你使用SQL的问题。然后你可以阅读JDBC Database Access 教程。阅读教程后,您可以返回指定的问题来详细说明您的问题。 and so on - 我们不知道这意味着什么。该论坛用于针对特定问题提出特定问题。

标签: java mysql swing jtable


【解决方案1】:

您可以通过以下示例获得帮助。它可以满足您的需求。虽然这不是一种完美的方式,但我只是做了一些更改来展示它是如何工作的。

享受:)

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);

                }
            });
        }

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 2021-02-09
    • 2021-05-03
    相关资源
    最近更新 更多