jtable getvalueat does not recognize changed value

V

vlenin66

Hi,

I have a table, add a row to it:
These are the results after having added a new row: (ccc 1,2,3,4 4000)
Setting value at 2,1 to 1.2.3.4 (an instance of class
java.lang.String)
New value of data:
row 0: Cluster1 localhost 4000
row 1: Cluster4 linux-suse103-2 4000
row 2: ccc 1.2.3.4 4000
row 3: 4000
row 4: 4000
...
row 29: 4000
--------------------------
Rows:30
-----------------------------------------------------
However, the read out ( mtm.getValueAt(index, 1)) does not work:
----------------------------------------------------

index:0 rechner:localhost
localhost true
index:1 rechner:linux-suse103-2
linux-suse103-2 true
index:2 rechner:
-----------------------------------------

Can anybody help me, why does mtm.getValueAt not work?

Thanks in advance.

Here the complete program (sorry for the long list):
--------------------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.AbstractTableModel;


public class gridmonGuiConf1 extends JPanel {
private JButton checkHosts;
private JButton submit;
private JTable clusterTable;
private JList results;
private DefaultListModel listModel;
private JScrollPane listScrollPane;
private JScrollPane scrollPane;

private boolean DEBUG = true;


public gridmonGuiConf1() {
//construct preComponents
String[] resultsItems = {"Item 1"};

//construct components
listModel = new DefaultListModel();
listModel.addElement("linux-suse103-2");
results = new JList (listModel);
results.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
results.setSelectedIndex(0);
results.setVisibleRowCount(7);
listScrollPane = new JScrollPane(results);


checkHosts = new JButton ("CheckHosts");
submit = new JButton ("Submit");
JTable clusterTable = new JTable(new MyTableModel());
clusterTable.setPreferredScrollableViewportSize(new
Dimension(500,400));
clusterTable.setFillsViewportHeight(true);
clusterTable.setAutoCreateRowSorter(true);
JScrollPane scrollPane = new JScrollPane(clusterTable);



//adjust size and set layout
setPreferredSize (new Dimension (409, 318));
setLayout (null);

//add components
add (checkHosts);
add (submit);
add (scrollPane);
add (listScrollPane);

//set component bounds (only needed by Absolute Positioning)
checkHosts.setBounds (130, 300, 110, 20);
submit.setBounds (245, 300, 100, 20);
scrollPane.setBounds (10, 5, 455, 290);
listScrollPane.setBounds (10, 325, 455, 125);

checkHosts.setMnemonic(KeyEvent.VK_C);
submit.setMnemonic(KeyEvent.VK_S);

CheckHostsListener checkHostsListener = new
CheckHostsListener();
checkHosts.addActionListener(checkHostsListener);


}
class CheckHostsListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Boolean result;
//This method can be called only if
//there's a valid selection
//so go ahead and remove whatever's selected.
int index = 0;
NetworkHost nwh = new NetworkHost();
MyTableModel mtm = new MyTableModel();
int lsize = mtm.getRowCount();
System.out.println("Rows:"+lsize);
listModel.removeAllElements();
while(index < lsize){
String rechner = mtm.getValueAt(index, 1).toString();
System.out.println("index:"+index+"
rechner:"+rechner);
if(rechner.length() > 0){
result = nwh.CheckHost(rechner);
System.out.println(rechner +" "+result);
if(result == false) {
listModel.addElement( rechner +" "+result);
}
}
++index;
}

System.out.println("CheckHosts");
}
}

class MyTableModel extends AbstractTableModel {
private String[] columnNames = {"Cluster Name",
"Server name",
"port"};
private Object[][] data = {
{"Cluster1",
"localhost", new Integer(4000)},
{"Cluster4",
"linux-suse103-2", new Integer(4000)},
{"",
"", new Integer(4000)},
{"",
"", new Integer(4000)},
{"",
"", new Integer(4000)},
{"",
"", new Integer(4000)},
{"",
"", new Integer(4000)},
{"",
"", new Integer(4000)},
{"",
"", new Integer(4000)},
{"",
"", new Integer(4000)},
{"",
"", new Integer(4000)},
{"",
"", new Integer(4000)},
{"",
"", new Integer(4000)},
{"",
"", new Integer(4000)},
{"",
"", new Integer(4000)},
{"",
"", new Integer(4000)},
{"",
"", new Integer(4000)},
{"",
"", new Integer(4000)},
{"",
"", new Integer(4000)},
{"",
"", new Integer(4000)},
{"",
"", new Integer(4000)},
{"",
"", new Integer(4000)},
{"",
"", new Integer(4000)},
{"",
"", new Integer(4000)},
{"",
"", new Integer(4000)},
{"",
"", new Integer(4000)},
{"",
"", new Integer(4000)},
{"",
"", new Integer(4000)},
{"",
"", new Integer(4000)},
{"",
"", new Integer(4000)},
};

public int getColumnCount() {
return columnNames.length;
}

public int getRowCount() {
return data.length;
}

public String getColumnName(int col) {
return columnNames[col];
}

public Object getValueAt(int row, int col) {
return data[row][col];
}

/*
* JTable uses this method to determine the default renderer/
* editor for each cell. If we didn't implement this method,
* then the last column would contain text ("true"/"false"),
* rather than a check box.
*/
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}

public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
if (col < 0) {
return false;
} else {
return true;
}
}

public void setValueAt(Object value, int row, int col) {
if (DEBUG) {
System.out.println("Setting value at " + row + "," +
col
+ " to " + value
+ " (an instance of "
+ value.getClass() + ")");
}

data[row][col] = value;
fireTableCellUpdated(row, col);

if (DEBUG) {
System.out.println("New value of data:");
printDebugData();
}
}

private void printDebugData() {
int numRows = getRowCount();
int numCols = getColumnCount();

for (int i=0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j=0; j < numCols; j++) {
System.out.print(" " + data[j]);
}
System.out.println();
}
System.out.println("--------------------------");
}
}

public static void main (String[] args) {
JFrame frame = new JFrame ("gridmonGuiConf1");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new gridmonGuiConf1());
frame.pack();
frame.setVisible (true);
}
}

------------------------------------------------------
 
J

Janusch

Halo !


1> JTable clusterTable = new JTable(new MyTableModel());

2> MyTableModel mtm = new MyTableModel();

Change the lines to

0: MyTableModel mtm;
1: JTable clusterTable = new JTable(mtm=new MyTableModel());
2: // use only the one mtm !!!

Simple example obout your problem:

int x,y;
x=5;
System.out.println(y);

gives 0, not 5!

Janush
http://Kathorncity.no-ip.info
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top