简体   繁体   English

从DefaultTableModel更新JTable

[英]Update JTable from DefaultTableModel

so what i want to do is update a JTable when a button is pressed. 所以我想做的就是按下按钮时更新JTable。 Im using a DefaultTableModel as a source. 我使用DefaultTableModel作为源。 Table class: 表类:

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

}

the form class: 表单类:

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

As you can see i call the addRow function but in never updates in the JTable, what am i missing? 如您所见,我调用了addRow函数,但从未在JTable中进行更新,我缺少了什么? Thnx 日Thnx

You're creating two TableDemo objects -- one you add to the GUI, tablePanel, the other you add a row to, tb; 您正在创建两个TableDemo对象-一个添加到GUI tableTablenel,另一个添加一行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()});
    }
});

Solution: Don't do this (obviously)! 解决方案:不要这样做(显然)! Use only one TableDemo instance, display it and make changes to it. 仅使用一个TableDemo实例,对其进行显示和更改。 So get rid of the tablePanel variable and only use the tb variable. 因此,摆脱tablePanel变量,而仅使用tb变量。

Other problems: 其他问题:

  • You're using null layouts. 您正在使用null布局。 While null layouts and setBounds() might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. 尽管null布局和setBounds()似乎是Swing新手喜欢创建复杂GUI的最简单和最佳方法,但您创建的Swing GUI越多,使用它们时将遇到的困难就越大。 They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one. 当GUI调整大小时,它们不会重新调整组件的大小;它们是需要增强或维护的皇家女巫;放置在滚动窗格中时,它们会完全失败;在所有平台或与原始分辨率不同的屏幕分辨率下查看时,它们看起来都是令人毛骨悚然的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM