更新JTable日期

时间:2012-03-26 06:24:12

标签: java swing jtable

用于初始化:

Vector<Vector> rowdata = new Vector<Vector>();
Vector columnname = new Vector();
JTabel result = new JTable(rowdata, columnname);
JScrollPane sqlresult = new JScrollPane(result);

在后面的部分中,我在rowdatacolumnname向量中分配值。如何在GUI上的JTable中显示新值?

另外..

result.repaint();

.. ..和

((DefaultTableModel)result.getModel()).fireTabelDataChanged()

..似乎什么都不做。

2 个答案:

答案 0 :(得分:3)

1)对JTable使用DefaultTableModel

2)定义列类

3)添加行DefaultTableModel.addRow(myVector);

例如

import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

public class DefaultTableModelDemo {

    public static final String[] COLUMN_NAMES = {
        "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"
    };
    private DefaultTableModel model = new DefaultTableModel(COLUMN_NAMES, 0);
    private JTable table = new JTable(model);
    private JPanel mainPanel = new JPanel(new BorderLayout());
    private Random random = new Random();

    public DefaultTableModelDemo() {
        JButton addDataButton = new JButton("Add Data");
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(addDataButton);
        addDataButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                addDataActionPerformed();
            }
        });
        model = new DefaultTableModel(COLUMN_NAMES, 0) {

            private static final long serialVersionUID = 1L;

            @Override
            public Class getColumnClass(int column) {
                return getValueAt(0, column).getClass();
            }
        };
        table = new JTable(model) {

            private static final long serialVersionUID = 1L;

            @Override
            public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
                Component c = super.prepareRenderer(renderer, row, column);
                if (isRowSelected(row) && isColumnSelected(column)) {
                    ((JComponent) c).setBorder(new LineBorder(Color.red));
                }

                return c;
            }
        };
        mainPanel.add(new JScrollPane(table), BorderLayout.CENTER);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);
    }

    private void addDataActionPerformed() {
        for (int i = 0; i < 5; i++) {
            Object[] row = new Object[COLUMN_NAMES.length];
            for (int j = 0; j < row.length; j++) {
                row[j] = random.nextInt(5);
            }
            model.addRow(row);
        }
    }

    public JComponent getComponent() {
        return mainPanel;
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame("DefaultTableModelDemo");
                frame.getContentPane().add(new DefaultTableModelDemo().getComponent());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

答案 1 :(得分:1)

rowdata和columnname用于初始化JTable,在组件JTable存在之后,您需要使用JTable.getColumnModel()和JTable.getModel()来添加列或行。

如果您只想添加列,请JTable.addColumn(TableColumn aColumn)。 如果您想知道如何在示例后添加行,请查看here