2016-04-16 160 views
0

我正在创建一个Java swing应用程序。我复制了我的代码的精简版本。我有一个我想用一些数据填充的JTable。当用户按下GO时,它会打开一个新窗口并且JTable中的数据被填充。我希望数据在GO按钮所在的窗口中填充到JTable中。任何想法为什么每当我按下按钮时,一个新的窗口就会打开相同的窗口?Java新线程打开新窗口?

public class test extends JFrame { 

protected JPanel mainPane; 
protected JTable displayTable; 
protected JPanel tabbedPanel; 
protected JTabbedPane tabbedPane; 
protected DefaultTableModel displayModel; 
protected JButton displayButton; 
protected JComboBox<String> comboBox; 
public test() { 
    setVisible(true); 
    setBounds(100, 100, 1000, 600); 
    setResizable(false); 
    mainPane = new JPanel(); 
    mainPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
    setContentPane(mainPane); 
    GridBagLayout gbl_mainPane = new GridBagLayout(); 
    gbl_mainPane.columnWidths = new int[] { 0, 93, 42, 189, 165, 0, 184, 0 }; 
    gbl_mainPane.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
    gbl_mainPane.columnWeights = new double[] { 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, Double.MIN_VALUE }; 
    gbl_mainPane.rowWeights = new double[] { 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE }; 
    mainPane.setLayout(gbl_mainPane); 

    tabbedPane = new JTabbedPane(JTabbedPane.TOP); 
    GridBagConstraints gbc_tabbedPane = new GridBagConstraints(); 
    gbc_tabbedPane.gridwidth = 7; 
    gbc_tabbedPane.gridheight = 9; 
    gbc_tabbedPane.fill = GridBagConstraints.BOTH; 
    gbc_tabbedPane.gridx = 0; 
    gbc_tabbedPane.gridy = 0; 
    mainPane.add(tabbedPane, gbc_tabbedPane); 

    tabbedPanel = new JPanel(); 
    tabbedPane.addTab("Volume", null, tabbedPanel, null); 
    GridBagLayout gbl_tabbedPanel = new GridBagLayout(); 
    gbl_tabbedPanel.columnWidths = new int[] { 86, 86, 86, 73, 73, -30, 140, 120, 0 }; 
    gbl_tabbedPanel.rowHeights = new int[] { 249, 28, 35, 10, 3, 3, 23, 0, 0 }; 
    gbl_tabbedPanel.columnWeights = new double[] { 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE }; 
    gbl_tabbedPanel.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE }; 
    tabbedPanel.setLayout(gbl_tabbedPanel); 

    JScrollPane scrollPane = new JScrollPane(); 
    GridBagConstraints gbc_scrollPane = new GridBagConstraints(); 
    gbc_scrollPane.fill = GridBagConstraints.BOTH; 
    gbc_scrollPane.insets = new Insets(0, 0, 5, 0); 
    gbc_scrollPane.gridwidth = 8; 
    gbc_scrollPane.gridx = 0; 
    gbc_scrollPane.gridy = 0; 
    tabbedPanel.add(scrollPane, gbc_scrollPane); 

    displayTable = new JTable(); 
    displayTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
    displayTable.setShowVerticalLines(false); 
    displayTable.setShowHorizontalLines(false); 
    displayTable.getTableHeader().setReorderingAllowed(false); 
    displayTable.setModel(
      new DefaultTableModel(new Object[][] {}, new String[] { "1", "2", "3", "4", "5", "6", }) { 
       Class[] columnTypes = new Class[] { Object.class, String.class, Integer.class, Integer.class, 
         String.class, String.class, String.class, Integer.class }; 
      }); 

    displayTable.getColumnModel().getColumn(0).setMinWidth(100); 
    displayTable.getColumnModel().getColumn(1).setMinWidth(20); 
    scrollPane.setViewportView(displayTable); 
    displayModel = (DefaultTableModel) displayTable.getModel(); 
    scrollPane.setViewportView(displayTable); 
    displayModel = (DefaultTableModel) displayTable.getModel(); 

    displayButton = new JButton("GO"); 
    GridBagConstraints gbc_displayButton = new GridBagConstraints(); 
    gbc_displayButton.gridwidth = 2; 
    gbc_displayButton.anchor = GridBagConstraints.NORTH; 
    gbc_displayButton.fill = GridBagConstraints.HORIZONTAL; 
    gbc_displayButton.insets = new Insets(0, 0, 5, 0); 
    gbc_displayButton.gridx = 6; 
    gbc_displayButton.gridy = 2; 
    tabbedPanel.add(displayButton, gbc_displayButton); 
    displayButton.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent arg0) { 

      Thread populate = new Thread(new PopulateDisplay()); 
      populate.start(); 
      displayButton.setEnabled(false); 

     } 
    }); 

    comboBox = new JComboBox<String>(); 
    GridBagConstraints gbc_comboBox = new GridBagConstraints(); 
    gbc_comboBox.insets = new Insets(0, 0, 5, 5); 
    gbc_comboBox.fill = GridBagConstraints.HORIZONTAL; 
    gbc_comboBox.gridx = 1; 
    gbc_comboBox.gridy = 4; 
    tabbedPanel.add(comboBox, gbc_comboBox); 

    comboBox.addItem("Test 1"); 
    comboBox.addItem("Test 2"); 
    comboBox.addItem("Test3"); 


    } 
} 






class PopulateDisplay extends test implements Runnable { 

public void run() { 

    try { 
     getData(comboBox.getSelectedItem().toString()); 
     lookup(); 
    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

private void getData(String sector) throws UnsupportedEncodingException, IOException { 
    //DOES SOMETHING 
    } 


private void lookup() throws IOException { 

    //added here 
    displayModel.addRow(new Object[] { "", "" , ""}); 


    } 

    } 
+0

这个问题可能是隐藏在'// DOES SOMETHING'后面的coce。发布一个完整的,最小的例子来重现问题。请注意,从其他事件调度线程违反Swing并发规则的线程访问Swing组件,因此您的代码不正确(它从后台线程调用comboBox.getSelectedItem())。 –

+0

我添加了lookup()方法中的.addRow函数。 getData()方法仅执行Web服务调用并分析CSV文件。 – j1nrg

回答

1

您的PopulateDisplay类延伸test。所以,一个PopulateDisplay实例是一个test,当你创建一个PopulateDisplay时,你正在创建一个新的test,所以你正在制作一个全新的JFrame并显示它。

PopulateDisplay不应该延伸test

您还应该尊重Java命名约定:类以大写字母开头。

+0

我该如何修复它,以便我仍然可以使用Test类中的变量? – j1nrg

+0

使runnable成为内部测试类。或者将'this'作为参数传递给PopulateDisplay的构造函数。但无论如何,你的方法是完全错误的:你不能从EDT以外的线程访问摆动组件和他们的模型。阅读https://docs.oracle.com/javase/tutorial/uiswing/concurrency/,并考虑使用SwingWorker。 –

+0

我让runnable成为一个内部类,它起作用。谢谢 – j1nrg