Java Forum / General / October 2006
JTable setValueAt problem
ginjasvinja - 25 Oct 2006 09:10 GMT Hi everyone!
I have created a table, JTable with DefaultTableModel and ScrollPane, and I have 5 columns and 5 rows. I wanted my rows to be empty at start of application, and later to fill the rows with some values. But the problem shows when I try to to that. I use setValueAt(Object,int,int) in this way:
ivjScrollPaneTable.setValueAt("draginja",0,0); ivjScrollPaneTable.setValueAt("svinja",0,1); ivjScrollPaneTable.setValueAt("budala",0,2); ivjScrollPaneTable.setValueAt("glupak",0,3); ivjScrollPaneTable.setValueAt("nosilac virusa gripa",0,4);
As the result only (0,0) value shows in table and when I check other values, with getValueAt(int,int), I get this:
(0,0): draginja (0,1): draginja (0,2): draginja (0,3): draginja (0,4): draginja
So, all the cells are filled with the same and first value. Also, when I select the row with just (0,0)cell showed, all the other cells shows with value "draginja". Does anyone have idea why I get the result like this.
I also have tried to use insertRow(int,Vector) to do the same job, like this:
Vector data = new Vector(); data.add(0,"draginja"); data.add(1,"svinja"); data.add(2,"budala"); data.add(3,"glupak"); data.add(4,"nosilac virusa gripa"); System.out.println("Vektor sa podacima: "); System.out.println(data); ivjDefaultTableModel1.insertRow(0,data);
and I get the same results, all cells are "draginja", but now all the cells are shown in table.
Any ideas or help? Thanks!
hiwa - 25 Oct 2006 09:51 GMT > Hi everyone! > [quoted text clipped - 43 lines] > Any ideas or help? > Thanks! I think your table and model initialization part of the code is wrong. Post a small demo code that is generally compilable, runnable and could reproduce your problem. See: http://homepage1.nifty.com/algafield/sscce.html and http://www.yoda.arachsys.com/java/newsgroups.html
ginjasvinja - 25 Oct 2006 11:44 GMT Ok, my code is written in VisualAge, and may look like scrabbled:
[code] package count;
import java.text.*; import java.util.*; import java.awt.geom.Dimension2D;
/** * Insert the type's description here. * Creation date: (10/13/2006 12:27:24 PM) * @author: Administrator */ public class TestTable extends javax.swing.JFrame { private javax.swing.JPanel ivjJFrameContentPane = null; private javax.swing.JButton ivjJButton1 = null; private javax.swing.table.DefaultTableModel ivjDefaultTableModel1 = null; IvjEventHandler ivjEventHandler = new IvjEventHandler(); private javax.swing.JScrollPane ivjJScrollPane1 = null; private javax.swing.JTable ivjScrollPaneTable = null; private javax.swing.table.TableColumn ivjTableColumn1 = null; private javax.swing.table.TableColumn ivjTableColumn2 = null; private javax.swing.table.TableColumn ivjTableColumn3 = null; private javax.swing.table.TableColumn ivjTableColumn4 = null; private javax.swing.table.TableColumn ivjTableColumn5 = null;
class IvjEventHandler implements java.awt.event.ActionListener { public void actionPerformed(java.awt.event.ActionEvent e) { if (e.getSource() == TestTable.this.getJButton1()) connEtoC1(e); }; }; /** * TestTable constructor comment. */ public TestTable() { super(); initialize(); } /** * TestTable constructor comment. * @param title java.lang.String */ public TestTable(String title) { super(title); } /** * connEtoC1: (JButton1.action.actionPerformed(java.awt.event.ActionEvent) --> TestTable.jButton1_ActionPerformed(Ljava.awt.event.ActionEvent;)V) * @param arg1 java.awt.event.ActionEvent */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private void connEtoC1(java.awt.event.ActionEvent arg1) { try { this.jButton1_ActionPerformed(arg1); } catch (java.lang.Throwable ivjExc) { handleException(ivjExc); } } /** * connPtoP1SetTarget: (DefaultTableModel1.this <--> ScrollPaneTable.model) */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private void connPtoP1SetTarget() { /* Set the target from the source */ try { getScrollPaneTable().setModel(getDefaultTableModel1()); } catch (java.lang.Throwable ivjExc) { handleException(ivjExc); } }
/** * Return the DefaultTableModel1 property value. * @return javax.swing.table.DefaultTableModel */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private javax.swing.table.DefaultTableModel getDefaultTableModel1() { if (ivjDefaultTableModel1 == null) { try { ivjDefaultTableModel1 = new javax.swing.table.DefaultTableModel(5,5); } catch (java.lang.Throwable ivjExc) { handleException(ivjExc); } } return ivjDefaultTableModel1; } /** * Return the JButton1 property value. * @return javax.swing.JButton */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private javax.swing.JButton getJButton1() { if (ivjJButton1 == null) { try { ivjJButton1 = new javax.swing.JButton(); ivjJButton1.setName("JButton1"); ivjJButton1.setText("View"); ivjJButton1.setBounds(218, 325, 85, 25); } catch (java.lang.Throwable ivjExc) { handleException(ivjExc); } } return ivjJButton1; } /** * Return the JFrameContentPane property value. * @return javax.swing.JPanel */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private javax.swing.JPanel getJFrameContentPane() { if (ivjJFrameContentPane == null) { try { ivjJFrameContentPane = new javax.swing.JPanel(); ivjJFrameContentPane.setName("JFrameContentPane"); ivjJFrameContentPane.setLayout(null); getJFrameContentPane().add(getJButton1(), getJButton1().getName()); getJFrameContentPane().add(getJLabel1(), getJLabel1().getName()); getJFrameContentPane().add(getJLabel2(), getJLabel2().getName()); getJFrameContentPane().add(getJLabel3(), getJLabel3().getName()); getJFrameContentPane().add(getJLabel4(), getJLabel4().getName()); getJFrameContentPane().add(getJScrollPane1(), getJScrollPane1().getName()); // user code begin {1} ivjJFrameContentPane.setVisible(true); // user code end } catch (java.lang.Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } } return ivjJFrameContentPane; } /** * Return the JScrollPane1 property value. * @return javax.swing.JScrollPane */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private javax.swing.JScrollPane getJScrollPane1() { if (ivjJScrollPane1 == null) { try { ivjJScrollPane1 = new javax.swing.JScrollPane(); ivjJScrollPane1.setName("JScrollPane1"); ivjJScrollPane1.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); ivjJScrollPane1.setHorizontalScrollBarPolicy(javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); ivjJScrollPane1.setBounds(27, 88, 468, 199); getJScrollPane1().setViewportView(getScrollPaneTable()); ivjJScrollPane1.setVisible(true); } catch (java.lang.Throwable ivjExc) { handleException(ivjExc); } } return ivjJScrollPane1; } /** * Return the ScrollPaneTable property value. * @return javax.swing.JTable */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private javax.swing.JTable getScrollPaneTable() { if (ivjScrollPaneTable == null) { try { ivjScrollPaneTable = new javax.swing.JTable(){ public boolean isCellEditable(int rowIndex, int vColIndex) { return false; } };
ivjScrollPaneTable.setName("ScrollPaneTable"); getJScrollPane1().setColumnHeaderView(ivjScrollPaneTable.getTableHeader()); getJScrollPane1().getViewport().setBackingStoreEnabled(true); ivjScrollPaneTable.setBounds(0, 0, 200, 200); ivjScrollPaneTable.setAutoCreateColumnsFromModel(false); ivjScrollPaneTable.addColumn(getTableColumn1()); ivjScrollPaneTable.addColumn(getTableColumn2()); ivjScrollPaneTable.addColumn(getTableColumn3()); ivjScrollPaneTable.addColumn(getTableColumn4()); ivjScrollPaneTable.addColumn(getTableColumn5());
getDefaultTableModel1().addColumn(getTableColumn1()); getDefaultTableModel1().addColumn(getTableColumn2()); getDefaultTableModel1().addColumn(getTableColumn3()); getDefaultTableModel1().addColumn(getTableColumn4()); getDefaultTableModel1().addColumn(getTableColumn5()); ivjScrollPaneTable.setVisible(true); } catch (java.lang.Throwable ivjExc) { handleException(ivjExc); } } return ivjScrollPaneTable; } /** * Return the TableColumn1 property value. * @return javax.swing.table.TableColumn */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private javax.swing.table.TableColumn getTableColumn1() { if (ivjTableColumn1 == null) { try { ivjTableColumn1 = new javax.swing.table.TableColumn(); ivjTableColumn1.setHeaderValue("Cassette"); } catch (java.lang.Throwable ivjExc) { handleException(ivjExc); } } return ivjTableColumn1; } /** * Return the TableColumn2 property value. * @return javax.swing.table.TableColumn */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private javax.swing.table.TableColumn getTableColumn2() { if (ivjTableColumn2 == null) { try { ivjTableColumn2 = new javax.swing.table.TableColumn(); ivjTableColumn2.setHeaderValue("Loaded"); } catch (java.lang.Throwable ivjExc) { handleException(ivjExc); } } return ivjTableColumn2; } /** * Return the TableColumn3 property value. * @return javax.swing.table.TableColumn */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private javax.swing.table.TableColumn getTableColumn3() { if (ivjTableColumn3 == null) { try { ivjTableColumn3 = new javax.swing.table.TableColumn(); ivjTableColumn3.setHeaderValue("Dispensed"); } catch (java.lang.Throwable ivjExc) { handleException(ivjExc); } } return ivjTableColumn3; } /** * Return the TableColumn4 property value. * @return javax.swing.table.TableColumn */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private javax.swing.table.TableColumn getTableColumn4() { if (ivjTableColumn4 == null) { try { ivjTableColumn4 = new javax.swing.table.TableColumn(); ivjTableColumn4.setHeaderValue("Rejected"); } catch (java.lang.Throwable ivjExc) { handleException(ivjExc); } } return ivjTableColumn4; } /** * Return the TableColumn5 property value. * @return javax.swing.table.TableColumn */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private javax.swing.table.TableColumn getTableColumn5() { if (ivjTableColumn5 == null) { try { ivjTableColumn5 = new javax.swing.table.TableColumn(); ivjTableColumn5.setHeaderValue("Remaining"); } catch (java.lang.Throwable ivjExc) { handleException(ivjExc); } } return ivjTableColumn5; } /** * Called whenever the part throws an exception. * @param exception java.lang.Throwable */ private void handleException(java.lang.Throwable exception) {
/* Uncomment the following lines to print uncaught exceptions to stdout */ System.out.println("--------- UNCAUGHT EXCEPTION ---------"); exception.printStackTrace(System.out); } /** * Initializes connections * @exception java.lang.Exception The exception description. */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private void initConnections() throws java.lang.Exception { // user code begin {1} // user code end getJButton1().addActionListener(ivjEventHandler); connPtoP1SetTarget(); } /** * Initialize the class. */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private void initialize() { try { setName("TestTable"); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setSize(522, 375); setTitle("ATM Counters"); setContentPane(getJFrameContentPane()); initConnections(); } catch (java.lang.Throwable ivjExc) { handleException(ivjExc); } } /** * Comment */ public void jButton1_ActionPerformed(java.awt.event.ActionEvent actionEvent) {
ivjScrollPaneTable.setValueAt("nosilac virusa gripa",0,4); ivjScrollPaneTable.setValueAt("glupak",0,3); ivjScrollPaneTable.setValueAt("budala",0,2); ivjScrollPaneTable.setValueAt("svinja",0,1); ivjScrollPaneTable.setValueAt("draginja",0,0); System.out.println("setValue Tabela: "); System.out.println("(0,0): "+ivjScrollPaneTable.getValueAt(0,0)); System.out.println("(0,1): "+ivjScrollPaneTable.getValueAt(0,1)); System.out.println("(0,2): "+ivjScrollPaneTable.getValueAt(0,2)); System.out.println("(0,3): "+ivjScrollPaneTable.getValueAt(0,3)); System.out.println("(0,4): "+ivjScrollPaneTable.getValueAt(0,4));
/*Vector data = new Vector(); data.add(0,"draginja"); data.add(1,"svinja"); data.add(2,"budala"); data.add(3,"glupak"); data.add(4,"nosilac virusa gripa"); System.out.println("Vektor sa podacima: "); System.out.println(data); ivjDefaultTableModel1.insertRow(0,data); System.out.println("insertRow Tabela: ");*/ System.out.println("(0,0): "+ivjScrollPaneTable.getValueAt(0,0)); System.out.println("(0,1): "+ivjScrollPaneTable.getValueAt(0,1)); System.out.println("(0,2): "+ivjScrollPaneTable.getValueAt(0,2)); System.out.println("(0,3): "+ivjScrollPaneTable.getValueAt(0,3)); System.out.println("(0,4): "+ivjScrollPaneTable.getValueAt(0,4));*/ return; } /** * main entrypoint - starts the part when it is run as an application * @param args java.lang.String[] */ public static void main(java.lang.String[] args) { try { TestTable aTestTable; aTestTable = new TestTable(); aTestTable.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent e) { System.exit(0); }; }); aTestTable.show(); java.awt.Insets insets = aTestTable.getInsets(); aTestTable.setSize(aTestTable.getWidth() + insets.left + insets.right, aTestTable.getHeight() + insets.top + insets.bottom); aTestTable.setVisible(true); } catch (Throwable exception) { System.err.println("Exception occurred in main() of javax.swing.JFrame"); exception.printStackTrace(System.out); } } }[\code]
hiwa - 26 Oct 2006 03:52 GMT Your code is the worst Java code I've ever seen past more than ten years. It is worst because you have never learnt the basics of Java GUI programming from authoritative sources. I heartily recommend you not to write any application code until you done the basics learning. There are good Swing books and online tutorials.
Well, anyway, try and study this code: -------------------------------------------------------------------- import javax.swing.*; import javax.swing.table.*; import java.awt.*; import java.awt.event.*; import java.util.*;
public class GinjasVinjaTable{ private JFrame frame; private Container con; private JButton viewButton; private DefaultTableModel model; private ViewButtonHandler vbh; private JScrollPane jsc; private JTable table; private Vector<String> colNames; private Vector<Vector<String>> tableData; private int colCount;
public GinjasVinjaTable(){ frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); con = frame.getContentPane(); con.setLayout(null); // real Java programmer should // never use null layout!!! colCount = 0;
colNames = new Vector<String>(); colNames.add("Cassette"); ++colCount; colNames.add("Loaded"); ++colCount; colNames.add("Dispensed"); ++colCount; colNames.add("Rejected"); ++colCount; colNames.add("Remaining"); ++colCount;
tableData = new Vector<Vector<String>>(); for (int i = 0; i < colCount; ++i){ tableData.add(new Vector<String>()); }
vbh = new ViewButtonHandler();
prepareGUI();
frame.setSize(550, 400); frame.setVisible(true); }
void prepareGUI(){ model = new DefaultTableModel(tableData, colNames); table = new JTable(model); jsc = new JScrollPane(table); jsc.setBounds(27, 88, 468, 199); con.add(jsc);
viewButton = new JButton("View"); viewButton.setBounds(218, 325, 85, 25); con.add(viewButton);
viewButton.addActionListener(vbh); }
class ViewButtonHandler implements ActionListener{ public void actionPerformed(ActionEvent e){ viewButton.setEnabled(false);
table.setValueAt("nosilac virusa gripa", 0, 4); table.setValueAt("glupak", 0, 3); table.setValueAt("budala", 0, 2); table.setValueAt("svinja", 0, 1); table.setValueAt("draginja", 0, 0);
System.out.println("setValue Tabela: "); System.out.println("(0,0): " + table.getValueAt(0, 0)); System.out.println("(0,1): " + table.getValueAt(0, 1)); System.out.println("(0,2): " + table.getValueAt(0, 2)); System.out.println("(0,3): " + table.getValueAt(0, 3)); System.out.println("(0,4): " + table.getValueAt(0, 4)); } }
public static void main(String[] args){ new GinjasVinjaTable(); } } ------------------------------------------------------------------------------------
Luc The Perverse - 26 Oct 2006 04:11 GMT > Your code is the worst Java code I've ever seen past more than ten > years. It is worst because you have never learnt the basics of Java [quoted text clipped - 3 lines] > > Well, anyway, try and study this code: Wasn't his code autogenerated?
-- LTP
:) ginjasvinja - 26 Oct 2006 08:18 GMT > > Your code is the worst Java code I've ever seen past more than ten > > years. It is worst because you have never learnt the basics of Java [quoted text clipped - 10 lines] > > :) Yes, it is. This code is made in Visual Age for Java, and from Visual Composition. I have to use Visual Age and the most of my problems is about that tool. It is too complicate when you have to make some simple thing (like table). hiwa, thanks for your time and advices. I read every tutorial about swing and searched every forum that I could find. But I still can't find any helpfull thing about Visual Age :(
hiwa - 26 Oct 2006 10:12 GMT > > > Your code is the worst Java code I've ever seen past more than ten > > > years. It is worst because you have never learnt the basics of Java [quoted text clipped - 18 lines] > swing and searched every forum that I could find. But I still can't > find any helpfull thing about Visual Age :( The table model part of your code, which is the most vital part of Swing table application, is not, can't be, the fault of any tool, Visual Age or whatever. Don't blame tools.
ginjasvinja - 26 Oct 2006 10:37 GMT I do not blame any tool, I am just not sure if I am using it right. And that's why I asked for help.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|