2016-04-08 90 views
0

所以我想要做的是按下按鈕時更新JTable。我使用DefaultTableModel作爲源。 表類:從DefaultTableModel更新JTable

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

正如你可以看到我所說的addRow功能,但從來沒有在JTable更新,我失去了什麼? Thnx

回答

4

您正在創建兩個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佈局。雖然Swing的新手可能看起來像是創建複雜GUI的最簡單也是最好的方式,但更多Swing GUI的創建使用它們時會遇到更嚴重的困難。它們不會在GUI大小調整時調整組件的大小,它們是增強或維護的皇室女巫,當它們放在滾動窗格中時它們會完全失敗,在所有平臺或屏幕分辨率與原始視圖不同時,它們看起來會非常糟糕。