2016-04-28 13 views
2

私は、特定の順序で単純な2列ビューを作成しようとしています。GridBagLayout注文を保持しない

それは次のようになります。私は、しかし、取得しています結果は左の空白のテーブルで一番上の行で

二つの静的ヘッダ、および各

下列あたり5枚のパネルとすべての詳細右に水平に並んだ。問題は詳細がインターネットからライブに読み込まれ、8秒ごとに更新されるので、データが更新されるごとに個々のセルを更新する必要があります(GridLayoutを使用しない理由)

コードループが無限に JPanelsをすべて初期化することはできません。各データポイントをオンラインから取得するには、少なくとも6秒かかるため、事前にすべてを初期化することはできません。

注文を手伝ってもらえますか?おかげ

import NeuralNet.NeuralNet; 

    import javax.swing.*; 
    import java.awt.*; 

public class MWindow { 

public MWindow(String[] c){ 

    JFrame mainFr = new JFrame("Current Predictions"); 
    mainFr.setSize(800, 800); 
    mainFr.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
    mainFr.setVisible(true); 
    JPanel p = new JPanel(new GridBagLayout()); 
    GridBagConstraints cns = new GridBagConstraints(); 
    cns.fill = GridBagConstraints.HORIZONTAL; 
    cns.insets = new Insets(5,5,15,15); 
    mainFr.add(p); 


    JTextArea man = new JTextArea("MANUFACTURING 5"); 
    cns.gridx = 0; 
    cns.gridy = 0; 
    cns.weightx = 1.0; 
    cns.weighty = 1.0; 
    cns.fill = GridBagConstraints.BOTH; 
    p.add(man, cns); 
    JTextArea inter = new JTextArea("INTERNET 5"); 
    cns.gridx = 1; 
    cns.gridy = 0; 
    cns.weightx = 1.0; 
    cns.weighty = 1.0; 
    cns.fill = GridBagConstraints.BOTH; 
    p.add(inter, cns); 

    JPanel aapl = new JPanel(); 
    JPanel msft = new JPanel(); 
    JPanel intc = new JPanel(); 
    JPanel ibm = new JPanel(); 
    JPanel tsla = new JPanel(); 
    JPanel fb = new JPanel(); 
    JPanel goog = new JPanel(); 
    JPanel yhoo = new JPanel(); 
    JPanel twtr = new JPanel(); 
    JPanel amzn = new JPanel(); 

    p.setBackground(Color.white); 
    mainFr.setBackground(Color.white); 

    while (true) { 
     for (String cmp : c) { 
      JPanel stkPanel = new JPanel(); 
      stkPanel.setBackground(Color.white); 
      if (!(cmp.equals("INTER5") || cmp.equals("MANU5") || cmp.equals("ALL"))) { 

       Asset a = new Asset(cmp); 
       NeuralNet n = Functions.loadNN(cmp); 
       NeuralNet nA = Functions.loadNN("ALL"); 
       NeuralNet n5; 
       if (cmp.equals("MSFT") || cmp.equals("AAPL") || cmp.equals("INTC") || cmp.equals("IBM") || cmp.equals("TSLA")) { 
        n5 = Functions.loadNN("MANU5"); 
       } else if (cmp.equals("TWTR") || cmp.equals("YHOO") || cmp.equals("GOOG") || cmp.equals("FB") || cmp.equals("AMZN")) { 
        n5 = Functions.loadNN("INTER5"); 
       } else { 
        System.out.println("ERROR"); 
        n5 = n; 
       } 

       double pred = n.PredictRows(Functions.formatData(a)); 
       double pred5 = n5.PredictRows(Functions.formatData(a)); 
       double predA = nA.PredictRows(Functions.formatData(a)); 
       JTextArea stkPred = new JTextArea(); 
       stkPred.setText("Stock: " + cmp + 
         "\nCurrent Price: " + "$" + a.getCurrPrice().toString() + 
         "\nPrediction 1: " + Double.toString(pred) + 
         "\nPrediction 2: " + Double.toString(pred5) + 
         "\nPrediction 3: " + Double.toString(predA)); 
       stkPred.setFont(new Font("Helvetica", Font.BOLD, 15)); 
       int pr = Functions.calcBS(pred, pred5, predA); 
       JTextArea act = new JTextArea(); 
       if (pr == -1) { 
        act.setText("SELL"); 
        act.setForeground(Color.red); 
       } else if (pr == 1) { 
        act.setText("BUY"); 
        act.setForeground(Color.green); 
       } else { 
        act.setText("HOLD"); 
        act.setForeground(Color.orange); 

       } 
       act.setFont(new Font("Helvetica", Font.BOLD, 15)); 

       stkPanel.add(stkPred); 
       stkPanel.add(act); 

       switch (cmp) { 
        case "MSFT": 
         msft.removeAll(); 
         msft.add(stkPanel); 
         msft.revalidate(); 
         msft.repaint(); 
         cns.gridx = 0; 
         cns.gridy = 2; 
         cns.weightx = 1.0; 
         cns.weighty = 1.0; 
         cns.fill = GridBagConstraints.BOTH; 
         p.add(msft, cns); 
        case "AAPL": 
         aapl.removeAll(); 
         aapl.add(stkPanel); 
         aapl.revalidate(); 
         aapl.repaint(); 
         cns.gridx = 0; 
         cns.gridy = 1; 
         cns.weightx = 1.0; 
         cns.weighty = 1.0; 
         cns.fill = GridBagConstraints.BOTH; 
         p.add(aapl, cns); 
        case "INTC": 
         intc.removeAll(); 
         intc.add(stkPanel); 
         intc.revalidate(); 
         intc.repaint(); 
         cns.gridx = 0; 
         cns.gridy = 3; 
         p.add(intc, cns); 
        case "IBM": 
         ibm.removeAll(); 
         ibm.add(stkPanel); 
         ibm.revalidate(); 
         ibm.repaint(); 
         cns.gridx = 0; 
         cns.gridy = 4; 
         p.add(ibm, cns); 
        case "TSLA": 
         tsla.removeAll(); 
         tsla.add(stkPanel); 
         tsla.revalidate(); 
         tsla.repaint(); 
         cns.gridx = 0; 
         cns.gridy = 5; 
         p.add(tsla, cns); 
        case "TWTR": 
         twtr.removeAll(); 
         twtr.add(stkPanel); 
         twtr.revalidate(); 
         twtr.repaint(); 
         cns.gridx = 1; 
         cns.gridy = 4; 
         p.add(twtr, cns); 
        case "FB": 
         fb.removeAll(); 
         fb.add(stkPanel); 
         fb.revalidate(); 
         fb.repaint(); 
         cns.gridx = 1; 
         cns.gridy = 1; 
         p.add(fb, cns); 
        case "AMZN": 
         amzn.removeAll(); 
         amzn.add(stkPanel); 
         amzn.revalidate(); 
         amzn.repaint(); 
         cns.gridx = 1; 
         cns.gridy = 5; 
         p.add(amzn, cns); 
        case "GOOG": 
         goog.removeAll(); 
         goog.add(stkPanel); 
         goog.revalidate(); 
         goog.repaint(); 
         cns.gridx = 1; 
         cns.gridy = 2; 
         p.add(goog, cns); 
        case "YHOO": 
         yhoo.removeAll(); 
         yhoo.add(stkPanel); 
         yhoo.revalidate(); 
         yhoo.repaint(); 
         cns.gridx = 1; 
         cns.gridy = 3; 
         p.add(yhoo, cns); 
       } 
       p.add(stkPanel); 
       p.revalidate(); 
      } 
     } 

     try { 
      Thread.sleep(10000); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 
} 
    } 

答えて

3

まず、私は強く、あなたがスイングのシングルスレッドな性質に違反しているとして、あなたは、Concurrency in Swingを見てお勧めします。

可能であれば、Worker Threads and SwingWorkerをご覧ください。

あなたのコードにはたくさんのことがありますが、多くのことが追加されたり削除されたりしています。これは単なる混乱です。

一般的に更新している情報は動的ではなく、データは表示されますが、提示される方法は異なります。

代わりに、表示する情報をカプセル化する単一のカスタムクラスを作成します。これがモデルになり、必要な形式で情報を表示できる単一のカスタムクラスが作成されます。

これらのコンポーネントをいくつか作成して、コンテナ内に配置したい場合は、Mapを使用してバインドします(変更すると、特定のデータソースのコンポーネントを参照できるようになります)。

次に、データ/モデルを変更するときにデータ/モデルを適切なコンポーネントに渡すだけで、単に表示を更新することができます。

JTableは、JLabel s/JTextFieldのシリーズだけでなく、おそらく道に向かうだろうから、UIをより簡単に更新できるようになります。

データソースの追加/削除が容易になる

関連する問題