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

問題はおそらく '/ /何かの後ろに隠れているコスです。問題を再現した完全で最小限の例を投稿してください。イベントディスパッチスレッドが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

実行可能ファイルをテストの内部クラスにします。あるいは、PopulateDisplayのコンストラクタの引数として 'this'を渡します。しかし、あなたのアプローチはとにかく完全に間違っています。あなたはEDT以外のスレッドからスイングコンポーネントとそのモデルにアクセスすることはできません。 https://docs.oracle.com/javase/tutorial/uiswing/concurrency/を読んで、SwingWorkerの使用を検討してください。 –

+0

私はrunnableを内部クラスにしました。ありがとう – j1nrg

関連する問題