2016-04-16 112 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