テーブル内のデータの更新/更新に問題があります。DefaultTableModel を使用しています。ここにコードがありますので、知識とアドバイスを共有してください。よろしくお願いします。
*編集 1: * 元のコードにはテーブル データを更新する関数がありますが、新しい JTable と新しい JScrollPane を何度も作成しているコンストラクターのために間違っていることがわかります。そのため、新しいコンストラクターを作成し、そのバグを修正しました。新しいコンストラクターを実装した後も、テーブル データは更新されませんでした。次に、コンストラクターが受け取ったデータを他のクラス (passDatatoTable クラス) でデバッグしようとしました。関数 setValueAt() を使用してから System.out.println(getValueAt(i,1)) を使用すると、出力が null になりました。それだけです。最初に、GUI を作成するメイン クラスを次に示します。
import javax.swing.*;
public class Glavni {
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
gui Scanner = new gui();
Scanner.setVisible(true);
}
});
}
}
2 つ目は、データを gui に送信するクラスです。
public class passDatatoTable {
public void passData(){
String str1,str2,str3,str4;
for (int i =0;i<=10;i++){
str1="Column 1 of row: "+i;
str2="Column 2 of row: "+i;
str3="Column 3 of row: "+i;
str4="Column 4 of row: "+i;
gui SendStringsToGUI = new gui(str1, str2, str3, str4);
}
}
}
そしてここにguiクラスがあります。ボタン「データを追加」は、データを入力するために使用されます。コードは次のとおりです。
public class gui extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
String[][] data = new String[100][4];
String[] columnNames = new String[]{
"IP", "PC_NAME", "ttl", "db"
};
DefaultTableModel model = new DefaultTableModel(data,columnNames);
JTable table = new JTable(model);
JScrollPane scrollPane = new JScrollPane(table);
int i=0;
public gui(String IP, String PC_NAME, String ttl, String gw) {
//Used this constructor to avoid creating new gui in other classes
model.setValueAt(IP, i, 0);
model.setValueAt(PC_NAME, i, 1);
model.setValueAt(ttl, i, 2);
model.setValueAt(gw, i, 3);
i++;
model.fireTableDataChanged();
table.repaint();
scrollPane.repaint();
}
gui(){
JButton addData= new JButton("Add Data");
JButton next = new JButton("next");
JButton prev = new JButton("prev");
addData.addActionListener(this);
next.addActionListener(this);
prev.addActionListener(this);
JPanel panel = new JPanel(new BorderLayout());
JPanel buttonPanel = new JPanel();
buttonPanel.add(addData);
buttonPanel.add(prev);
buttonPanel.add(next);
panel.add(buttonPanel, BorderLayout.SOUTH);
panel.add(table.getTableHeader(), BorderLayout.PAGE_START);
panel.add(scrollPane, BorderLayout.CENTER);
getContentPane().add(panel);
}
ここに ActionListeners があります。ボタン next と prev はページングに使用され、ボタン「Add Data」はテーブルのデータを埋めるために使用されるクラス「passDatatoTable」の変数を作成するために使用されます
コードは次のとおりです。
public void actionPerformed(ActionEvent e) {
if ("Add Data".equals(e.getActionCommand())){
passDatatoTable passSomeData = new passDatatoTable();
passSomeData.passData();
}
if ("next".equals(e.getActionCommand())) {
Rectangle rect = scrollPane.getVisibleRect();
JScrollBar bar = scrollPane.getVerticalScrollBar();
int blockIncr = scrollPane.getViewport().getViewRect().height;
bar.setValue(bar.getValue() + blockIncr);
scrollPane.scrollRectToVisible(rect);
}
if ("prev".equals(e.getActionCommand())) {
Rectangle rect = scrollPane.getVisibleRect();
JScrollBar bar = scrollPane.getVerticalScrollBar();
int blockIncr = scrollPane.getViewport().getViewRect().height;
bar.setValue(bar.getValue() - blockIncr);
scrollPane.scrollRectToVisible(rect);
}
}