在JTable中显示旧值

时间:2018-06-30 10:32:41

标签: java swing awt

在此代码块中,在滚动窗格中单击后,我得到的是旧值,但是新值在获取时也会先消失,但它们会慢慢消失,而旧值会出现在该位置。

我尝试了几种组合,但未能达到预期的结果。同样,在变量声明期间,我将所有变量设为非静态变量,除了我认为不相关和不建议使用的框架,而且还将组件本身嵌入框架中。

//THE TABLE
table = new JTable();
//THE COLUMN

String[] columns = new String[] {
    "Serial No", "BundleId", "Bundle Count", "Record Count","Status","Date"
};
 //THE ROW

Object[][] data = dc.Success(date,afterDate);  
final Class[] columnClass = new Class[] {
        Boolean.class,String.class, String.class, String.class,String.class,String.class };

 //create table model with data
DefaultTableModel model = new DefaultTableModel(data, columns) {

    @Override
    public boolean isCellEditable(int row, int column)
    {
        return false;
    }

    @Override
    public Class<?> getColumnClass(int columnIndex)
    {
        return columnClass[columnIndex];
    }


    public Object getCellEditorValue() {
        return "";
    }
};

// JTable table = new JTable(model);

table.setModel(model);

//add the table to the frame
//ADD SCROLLPANE
scroll.setBounds(70, 80, 600, 400);
scroll.setViewportView(table);
frame.getContentPane().add(scroll);
// this.add(new JScrollPane(table));
// table.setAutoCreateRowSorter(true);
table.setPreferredScrollableViewportSize(new Dimension(320, 160));
TableColumn tc = table.getColumnModel().getColumn(BOOLEAN_COL);
tc.setHeaderRenderer(new SelectAllHeader(table, BOOLEAN_COL));
this.setTitle("Table Example");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
this.pack();
this.setVisible(true);
table.repaint();

1 个答案:

答案 0 :(得分:0)

如果您想通知JTable有关数据的更改,请使用类似于tableModel.fireTableDataChanged()的内容。

还有其他方法可用于更新特定范围的数据。 tableModel.fireTableRowsUpdated​(firstRow, lastRow)可能对您特别有用。这是因为建议您仅在单击该行时才更新该行。如果不是这种情况,请坚持使用tableModel.fireTableDataChanged()

从您的评论中,我注意到您正在尝试触发数据更改事件,然后无论如何都要更改整个模型。这不是必需的,并且如果模型保留旧值,则实际上可能是问题的根源。

相反,您应该更改当前模型的DataVector。要更改一行上的数据,可以使用以下代码摘要。摘录下面有一个完整的示例。

//Get your row's values from your database
//I'll use random values for this purpose
int col1Val = getRand(0, 9);
int col2Val = getRand(0, 9);

//Put the values in an object vector
Vector<Object> rowVals = new Vector<>();
rowVals.addElement(col1Val);
rowVals.addElement(col2Val);

//Set the object vector
model.getDataVector().setElementAt(rowVals, table.getSelectedRow());

//Notify the table the row has changed
model.fireTableRowsUpdated(table.getSelectedRow(), table.getSelectedRow());

完整示例

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;
import java.util.concurrent.ThreadLocalRandom;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class JTableExample extends JFrame {
    private static final String windowName = "JTable Example";
    private static final long serialVersionUID = 362702020844358278L;

    private JPanel tablePanel;
    private JScrollPane scrollPanel;
    private JTable table;
    private DefaultTableModel model;

    private JTableExample() {
        super(windowName);
        SetUp();
    }

    private void SetUp() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        SetUpJTable();
        SetUpJPanel();

        add(tablePanel);
        pack();
        setVisible(true);
    }

    private void SetUpJPanel() {
        tablePanel = new JPanel(new GridLayout());
        tablePanel.add(scrollPanel);
    }

    private void SetUpJTable() {
        String[] columns = new String[] {
            "Something", "Something Else"
        };

        String[][] data = new String[][] {
            {"1", "2"},
            {"3", "3"}
        };

        model = new DefaultTableModel(data, columns) {
            private static final long serialVersionUID = -3895234084030399437L;

            @Override
            public boolean isCellEditable(int row, int column)
            {
                return false;
            }
        };

        table = new JTable(model);

        table.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                //Get your row's values from your database
                //I'll use random values for this purpose
                int col1Val = getRand(0, 9);
                int col2Val = getRand(0, 9);

                //Put the values in an object vector
                Vector<Object> rowVals = new Vector<>();
                rowVals.addElement(col1Val);
                rowVals.addElement(col2Val);

                //Set the object vector
                model.getDataVector().setElementAt(rowVals, table.getSelectedRow());

                //Notify the table the row has changed
                model.fireTableRowsUpdated(table.getSelectedRow(), table.getSelectedRow());
            }
        });

        scrollPanel = new JScrollPane(table);
        scrollPanel.setPreferredSize(new Dimension(500, 500));
    }

    private static int getRand(int min, int max) {
        return ThreadLocalRandom.current().nextInt(min, max + 1);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> new JTableExample());
    }
}