0

テーブル内のデータの更新/更新に問題があります。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);
 }

 }
4

3 に答える 3

3

Gui現在表示されているオブジェクトに関連付けられていない新しいオブジェクトのテーブル モデル データを設定しているGuiように見えるため、 には何も表示されませんJTable table

モデル更新メソッドを使用すると、必要に応じてモデル データを更新できます。

public void updateModel(String IP, String PC_NAME, String ttl, String gw) {
   model.setValueAt(IP, i, 0);
   model.setValueAt(PC_NAME, i, 1);
   model.setValueAt(ttl, i, 2);
   model.setValueAt(gw, i, 3);
   i++;
}

Guiオブジェクトの参照をオブジェクトに渡す必要がありPassDatatoTableます。後者のオブジェクトは、現在の形式では機能がほとんどないため、この機能をGuiクラスに組み込む方がよいでしょう。

また、呼び出す必要はありません:

model.fireTableDataChanged();

これは自動的に行われます。

于 2012-09-29T12:32:16.250 に答える
2

あなたの問題はここにあるかもしれません:

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); // **** line (A)
      }
   }
}

行 (A)で新しい gui オブジェクトを作成し、それにデータを渡しているようです。この gui オブジェクトは、表示されている gui オブジェクトとはまったく関係がないことに注意してください。したがって、この方法でデータを渡しても、表示されるデータに目に見える変化は生じません。

考えられる解決策は、コンストラクタ パラメータを介して gui オブジェクトへの参照を渡し、その gui オブジェクト (コンストラクタではない) のパブリック メソッドを使用してデータを設定または変更することです。

これが私が意味することの簡単な例です:

import java.awt.event.ActionEvent;

import javax.swing.*;

public class GuiTest2 {
   private static void createAndShowGui() {
      Gui2 mainPanel = new Gui2();

      JFrame frame = new JFrame("Gui2");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class Gui2 extends JPanel {
   private JTextField field = new JTextField(10);

   // pass a reference to the GUI object into the controller via
   // its constructor
   private JButton nextBtn = new JButton(new NextController(this, "Next"));

   public Gui2() {
      field.setEditable(false);
      field.setFocusable(false);

      add(field);
      add(nextBtn);
   }

   public void setTextFieldText(String text) {
      field.setText(text);
   }
}

class NextController extends AbstractAction {
   private static final String[] TEXTS = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
   private Gui2 gui;
   private int index = 0;

   // pass the Gui2 object into the controller's constructor
   public NextController(Gui2 gui, String name) {
      super(name);
      this.gui = gui; // set the Gui2 field with the parameter
   }

   @Override
   public void actionPerformed(ActionEvent arg0) {
      // use the Gui2 object here
      gui.setTextFieldText(TEXTS[index]);
      index++;
      index %= TEXTS.length;
   }

}
于 2012-09-29T12:15:30.883 に答える
0

ヒーローをありがとう。最後に、この問題を修正します。大したことは、テーブルにデータを埋めていたクラスにオブジェクトを送信することでした。言い換えれば、ここに私がしたことがあります:

テーブルにデータを渡していたクラスを覚えていますか? 私がここでやったようにそれを修正してください:

public class passDatatoTable {
passDatatoTable(ScanFrame gui){
    String str1,str2,str3;

    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;
            gui.model.setValueAt(str1, gui.i, 0);
            gui.model.setValueAt(str2, gui.i, 2);
            gui.model.setValueAt(str3, gui.i, 3);
            gui.i++;
    }
}}

それでは、guiクラスに戻りましょう。コンストラクターを削除するだけです (名前を好きな名前に変更し、メイン関数で呼び出します)。これが例です。注意: 必要がないため、2 つのコンストラクターはすべてなくなりました。

コード:

public void CreateAndShowGUI(){
setVisible(true);//add this line to show 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);
}

今、重要なパーが来ています。ボタン「データを追加」を覚えていますか?あなたがする必要があるのは、このGUIのrefrenceをテーブルにデータを埋めるクラス(passDatatoTableクラス)に送信することだけではなく、それは機能します。

public void actionPerformed(ActionEvent e) {
if ("Add Data".equals(e.getActionCommand())){

passDatatoTable passSomeData = new passDatatoTable();
passSomeData.passData(this);//here!! just add this and it will work!!
      }
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);
}

}

編集

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.CreateAndShowGUI();
        }
    });
}

    }

メインクラスを追加するのを忘れていました。はい、これ。問題/質問がある場合は、質問してください。@Hovercraft Full Of Eels と @Reimeus に感謝します。

于 2012-09-29T13:59:29.557 に答える