当前位置: 首页 > 知识库问答 >
问题:

从DefaultTableModel更新JTable

柯振濂
2023-03-14
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import java.awt.Dimension;

public class TableDemo extends JPanel {
    String[] columnNames = {"Tipo","Cantidad"};
    DefaultTableModel dtm = new DefaultTableModel(null,columnNames);
    final JTable table = new JTable(dtm);

 public TableDemo() {
    table.setPreferredScrollableViewportSize(new Dimension(500, 700));
    table.setFillsViewportHeight(true);
    JScrollPane scrollPane = new JScrollPane(table);
    add(scrollPane);
 }

 public void addRow(Object [] row)
 {
     dtm.addRow(row);
 }

}
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
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.JSplitPane;
import javax.swing.JTextField;

public class MyForm extends JFrame {

JPanel panel;
JLabel label;
JTextField txtName;
JTextField txtSurname;
JButton btnAccept;
TableDemo tb  = new TableDemo();

public MyForm(){
    initControls();
}

private void initControls() {
    setTitle("Form Example");
    setSize(600, 400);
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    setLocationRelativeTo(null);

    panel = new JPanel();       
    panel.setLayout(null);

    label = new JLabel("Welcome to Swing");
    label.setBounds(50, 10, 200, 30);

    txtName = new JTextField();
    txtName.setBounds(50, 50, 200, 30);

    txtSurname = new JTextField();
    txtSurname.setBounds(270, 50, 200, 30);

    btnAccept = new JButton("Add");
    btnAccept.setBounds(120, 100, 150, 30);

    onAcceptClick();
    TableDemo tablePanel = new TableDemo();

    panel.add(label);
    panel.add(txtName);
    panel.add(txtSurname);
    panel.add(btnAccept);

    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
            tablePanel,panel );
    splitPane.setDividerLocation(205);
    splitPane.setEnabled(false);

    getContentPane().add(splitPane);
}

private void onAcceptClick() {
    btnAccept.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            tb.addRow(new Object[]{"taza",txtName.getText()});

        }
    });
public static void main(String[] args) {
    //Initilizer init = new Initilizer();

    Runnable init = new Runnable() {

        @Override
        public void run() {
            MyForm form = new MyForm();
            form.setVisible(true);
        }
    };

    SwingUtilities.invokeLater(init);   
}
}

共有1个答案

韩晋
2023-03-14

您正在创建两个TableDemo对象--一个添加到GUI中的tablePanel,另一个添加一行TB;

// ...

TableDemo tb  = new TableDemo();  // TableDemo #1. Never displayed

private void initControls() {
    // ...

    TableDemo tablePanel = new TableDemo();   // TableDemo #2

    // ...

    //  TableDemo #2 is here added to the GUI
    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, tablePanel,panel );

    // ...
}

btnAccept.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {

        // here you add a row to the non-displayed TableDemo #1
        tb.addRow(new Object[]{"taza",txtName.getText()});
    }
});

解决办法:不要这样做(显然)!只使用一个TableDemo实例,显示它并对它进行更改。所以去掉tablePanel变量,只使用tb变量。

其他问题:

    null
 类似资料: