【问题标题】:How to set rows and columns of a JTable Dynamic如何设置 JTable 动态的行和列
【发布时间】:2015-01-14 22:49:40
【问题描述】:

我的问题是如何设置动态的,JTable 中的行数和列数?我的意思是如果用户想要创建一个 2 行 2 列的表,他只需输入数字。我该怎么做,我已经尝试过使用 DefaultModel 没有成功。

我将不胜感激。

谢谢

【问题讨论】:

  • 您的意思是“锯齿状”表吗?就像用户被允许一行有 3 列和另一行有 4 列?或者您只是在向表中添加行时遇到问题?
  • 贴出你试过的代码。
  • TableModel in 是解决方案...
  • 添加行有问题,让我试一试,如果不行我会贴出代码,谢谢大家的快速回复

标签: java swing jtable rows dynamic-columns


【解决方案1】:

DefaultTableModel 有两种方法可以在运行时定义行数/列数。

你可以...

只需创建一个新的DefaultTableModel,将所需的行和列传递给它...

DefaultTableModel model = new DefaultTableModel(rows, cols);

然后将其应用于JTable。显然,这将替换现有的表模型,这意味着您将丢失它的所有数据。

你可以...

创建一个主DefaultTableModel 并将其应用于JTable 并简单地使用

model.setRowCount(rows);
model.setColumnCount(cols);

根据需要动态更新行数和列数。这将允许您保留表模型中的数据(预计当您删除行或列时,它会丢失)

可运行示例

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTable;
import javax.swing.SpinnerNumberModel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JTable table;
        private DefaultTableModel model;
        private JSpinner fldRows;
        private JSpinner fldColumns;

        public TestPane() {

            setLayout(new BorderLayout());

            fldRows = new JSpinner(new SpinnerNumberModel(1, 1, 999999, 1));
            fldColumns = new JSpinner(new SpinnerNumberModel(1, 1, 999999, 1));

            JPanel options = new JPanel(new GridBagLayout());
            options.add(new JLabel("Rows: "));
            options.add(fldRows);
            options.add(new JLabel("Columns: "));
            options.add(fldColumns);

            JButton update = new JButton("Update");
            options.add(update);

            update.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    int rows = (int) fldRows.getValue();
                    int cols = (int) fldColumns.getValue();

                    // Dynamic master model...
//                  model.setRowCount(rows);
//                  model.setColumnCount(cols);

                    // Replace model
                    table.setModel(new DefaultTableModel(rows, cols));
                }
            });

            model = new DefaultTableModel();
            table = new JTable();
            add(new JScrollPane(table));
            add(options, BorderLayout.NORTH);

        }

    }

}

有关详细信息,请参阅...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-02
    • 1970-01-01
    • 1970-01-01
    • 2011-10-14
    • 1970-01-01
    • 1970-01-01
    • 2016-06-23
    • 2018-03-15
    相关资源
    最近更新 更多