2016-03-28 23 views
0

テーブルを空にする必要がありますが、 "Load Data"ボタンをクリックした後、データベースから取得したデータを表示します。この部分の返信データを確認すると、データを変更した後にJava JTableのrepaint()メソッドが機能しない

for (i=0; i < data.length; i++){ 
    for (j=0; j < 4; j++){ 
     if (data[i][j] != null) 
      System.out.print(data[i][j] + " "); 
    } 
    if (data[i][j-1] != null) 
     System.out.println(); 
} 

となりますので、問題はありません。誰かがなぜrepaint()が働いていないのか、何が間違っているのか説明できますか?ここで

は私のコードです:

public class UserInterface { 

    JFrame frame = new JFrame("User Interface"); 
    JPanel panel = new JPanel(); 
    JButton button = new JButton("Load Data"); 
    JTable table; 
    JScrollPane scrollPane; 
    String[] columnNames = {"name", "age", "address", "phone number"}; 
    String[][] data = new String[100][4]; 

    public UserInterface(){ 

     frame.getContentPane().setSize(200, 300); 
     table = new JTable(data, columnNames); 
     scrollPane = new JScrollPane(table); 
     panel.add(button); 
     panel.add(scrollPane); 
     frame.add(panel); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setVisible(true); 

     button.addActionListener(new ActionListener() 
     { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
      // TODO Auto-generated method stub 
       ConnectDatabase cd = new ConnectDatabase(); 
       try { 
        data = cd.getData(); 
        System.out.println("we got the data from db on the ui."); 
        int i, j; 
        for (i=0; i<data.length; i++){ 
         for (j=0; j<4; j++){ 
          if (data[i][j] != null) 
           System.out.print(data[i][j] + " "); 
         } 
         if (data[i][j-1] != null) 
          System.out.println(); 
        } 
       } catch (ClassNotFoundException e1) { 
       // TODO Auto-generated catch block 
        e1.printStackTrace(); 
       } catch (SQLException e1) { 
       // TODO Auto-generated catch block 
        e1.printStackTrace(); 
       } 
       table.repaint(); 
       scrollPane.repaint(); 
       System.out.println("table repaint is done."); 
      } 
     }); 

    } 


    @SuppressWarnings("unused") 
    public static void main(String[] args) { 
    // TODO Auto-generated method stub 
     UserInterface ui = new UserInterface(); 
    } 

} 
+0

(検証呼び出してみ)または再検証()または再描画と一緒に無効()メソッド()。 –

答えて

2

私はdataに値を設定したときにそれは私のために働いているようです。データのオブジェクトをdata = cd.getData();で変更しているため、テーブルには自分が行ったことが分かりません。返されたデータを調べ、元の配列を更新してみてください。

String[][] temp = cb.getData(); 
for (i=0; i<temp.length; i++){ 
    for (j=0; j<4; j++){ 
     data[i][j] = temp[i][j]; 
    } 
} 

2つの配列は同じサイズになりますか?私はのTableModelを使用することをお勧めし、例えばこれを行うだろう:

DefaultTableModel model = new DefaultTableModel(new Object[]{"name", "age", "address", "phone number"},0); 
table = new JTable(model); 

// Now when you populate the table, you would do this for example: 
String[][] temp = cb.getData(); 
for (i=0; i<temp.length; i++){ 
    model.addRow(new Object[]{temp[i][0],temp[i][1],temp[i][2],temp[i][3]}); 
} 
+0

ありがとうございます、それは働いています。 – kata

+0

2番目の例のように記述すると、2番目の例は必要ありませんが、実際にはそれを行うより良い方法と思われます。再度、感謝します。 – kata

2

repaintはそれを行うための方法ではありません。 repaint()の両方の呼び出しを削除する必要があります。

JTableは構築中にそのコピーを作成し、まったく使用しないため、配列を更新しても差はありません。

あなたは、新しいデータを(のコピー)を使用するように、テーブルのモデルを伝える必要があります:

DefaultTableModel model = (DefaultTableModel) table.getModel(); 
model.setDataVector(data, columnNames); 
関連する問題