2014-12-01 18 views
0

我试图在JTable中第一次加载文件名时,第一次按下按钮时,它第一次按loads fine,但第二次按下同一按钮时它goes blank当按下JButton时,JTable第二次变为空白

短的例子:

package exampleDemo; 

public class JTableExample { 

private JFrame frame; 
private JTable table_1; 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       JTableExample window = new JTableExample(); 
       window.frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

public JTableExample() { 
    initialize(); 
} 

    public void showEmptyTable() { 
     final String[] colNames = {"Sl.no.", "File Name", "Size", "Last Modified", "PO", "LN", "SI", "MS", "PT"}; 
     final String[][] emptyData = { 
      {"", "", "", "", "", "", "", "", "",}, 
      {"", "", "", "", "", "", "", "", "", }, 
      {"", "", "", "", "", "", "", "", "", }, 
      {"", "", "", "", "", "", "", "", "", }, 
      {"", "", "", "", "", "", "", "", "", },}; 
    table_1.setModel(new DefaultTableModel(emptyData, colNames)); 
    frame.getContentPane().add(table_1); 
    JScrollPane scrollPane = new JScrollPane(table_1); 
    scrollPane.setBounds(81, 162, 830, 180); 
    frame.getContentPane().add(scrollPane); 
}; 

private void initialize() { 
    frame = new JFrame(); 
    frame.setBounds(100, 100, 1002, 576); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.getContentPane().setLayout(null); 

    table_1 = new JTable(); 
    table_1.setBounds(81, 162, 830, 180); 
    frame.getContentPane().add(table_1); 

    JButton btnNewButton = new JButton("New button"); 
    btnNewButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      table_1.repaint(); 
      showEmptyTable(); 
     } 
    }); 
    btnNewButton.setBounds(578, 70, 89, 23); 
    frame.getContentPane().add(btnNewButton); 
    } 
} 
+2

1)为了更好地帮助越早,张贴[MCVE](http://stackoverflow.com/help/mcve)(最小完备可验证实施例)或[SSCCE](HTTP:// WWW。 )(简短,独立,正确的例子)。 2)'btnSearchContent.setBounds(913,175,124,23);'Java GUI必须在不同的操作系统上工作',屏幕大小,屏幕分辨率等。因此,它们不利于像素完美布局。请使用布局管理器或[它们的组合](http://stackoverflow.com/a/5630271/418556)以及[white space]的布局填充和边框(http://stackoverflow.com/a/17874718/ 418556)。 – 2014-12-01 15:36:32

+0

对于'Document Details'下面的东西,使用['JSeparator'](https://docs.oracle.com/javase/8/docs/api/javax/swing/JSeparator.html)。 – 2014-12-01 15:38:42

+0

感谢@AndrewThompson,我试图发布完整的代码,但它给出了格式化错误,这就是为什么只发布了函数,现在已经在pastebin中发布了[完整代码](http://pastebin.com/B4ii3yVQ)谢谢 – 2014-12-01 15:42:09

回答

1

你的代码看一眼就建议我,你要对这个错误的方式:你每次从字面上重建一个新的表模型单击该按钮我怀疑(虽然现在只是一种预感),因为每次单击该按钮时,都会通过一个新的滚动面板将相同的表添加到主JFrame中 - 每一次。

你想要做的是在加载帧的过程中初始化表格及其模型。然后,如果要清除表格,那么只需获取对该表格模型的引用并操纵现有模型。就是这样 - 不需要创建一个完整的新模型;无需通过滚动面板添加新表格。

所以,我想你的情况做的是添加一行到你的构造:

public YourClassName() { 
    initialize(); 
    initializeYourTable(); 
} 

然后initializeYourTable()应该是这样的:

private void initSearchTable() { 
    final String[] colNames = {"Sl.no.", "File Name", "Size", "Last Modified", "PO", "LN", "SI", "MS", "PT"}; 
    //PO: Part Order 
    //LN: Line Number 
    //SI: Serial Number 
    //MS: Material Supplier 
    //PT: Part Item 
    final String[][] emptyData = { 
     {"", "", "", "", "", "", "", "", "",}, 
     {"", "", "", "", "", "", "", "", "",}, 
     {"", "", "", "", "", "", "", "", "",}, 
     {"", "", "", "", "", "", "", "", "",}, 
     {"", "", "", "", "", "", "", "", "",},}; 
    table_3.setModel(new DefaultTableModel(emptyData, colNames)); 
    //frmViperManufacturingRecord.getContentPane().add(table_3); <-- NB this should be commented out!! 
    table_3.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 
    table_3.getColumnModel().getColumn(0).setPreferredWidth(40); 
    table_3.getColumnModel().getColumn(1).setPreferredWidth(290); 
    table_3.getColumnModel().getColumn(2).setPreferredWidth(80); 
    table_3.getColumnModel().getColumn(3).setPreferredWidth(178); 
    table_3.getColumnModel().getColumn(4).setPreferredWidth(25); 
    table_3.getColumnModel().getColumn(5).setPreferredWidth(25); 
    table_3.getColumnModel().getColumn(6).setPreferredWidth(25); 
    table_3.getColumnModel().getColumn(7).setPreferredWidth(25); 
    table_3.getColumnModel().getColumn(8).setPreferredWidth(25); 
    JScrollPane scrollPane = new JScrollPane(table_3); 
    scrollPane.setBounds(324, 209, 713, 160); 
    frmViperManufacturingRecord.getContentPane().add(scrollPane); 
} 

要清除您可以使用此表shortcut

private void resetTable() { 
    DefaultTableModel model = (DefaultTableModel)table_3.getModel(); 
    model.setRowCount(0); 
} 

因此,您需要更改actionPerformed

public void actionPerformed(ActionEvent arg0) { 
    table_3.repaint(); 
    //showEmptyTable(); 
    resetTable(); 
} 
+0

JTable dosn't不需要重绘repaint();错误的建议基于不佳的问题 – mKorbel 2014-12-01 18:02:53

+0

谢谢@mKorbel,我带** arooaroo **建议不要在每次重新构建一个新的表模型。如你所说,应该改变这个问题?我没有找到你以前的帖子的任何类似的答案,谢谢 – 2014-12-02 09:18:31

+0

谢谢@ mkorbel,如果不重绘​​();你会建议什么?谢谢 – 2014-12-02 10:44:34

相关问题