Java新线程打开新窗口?

时间:2016-04-16 16:39:08

标签: java multithreading swing

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

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[] { "", "" , ""});


     }

  }

1 个答案:

答案 0 :(得分:1)

您的PopulateDisplay类扩展了test。因此,PopulateDisplay实例 test,当您创建PopulateDisplay时,您正在创建新的test,因此您需要重新制作一个全新的JFrame并显示它。

PopulateDisplay不应该test扩展。

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