Problem in editing JTable fields.....

Hi there!!

I have got 2 classes in my package,one contains a public static JTable while the other has got some variables whose values I want the JTable's fields to populate with.Now the problem I have is that I can't populate the JTable's fields after making it's class's object and accessing the JTable using that object...Can anybody help plz?

Thanks...

[379 byte] By [Kami_Pakistana] at [2007-11-27 1:45:52]
# 1
Wht is the model of table u r using....Try using DefaultTableModel instead of AbstractTableModel or just TableModel.
vandana547a at 2007-7-12 1:06:45 > top of Java-index,Desktop,Core GUI APIs...
# 2
Hi vandana!It's DeafultTableModel,,don't know what's causing me the problem,,don't think a public static JTable declaration could cause any problems,,,
Kami_Pakistana at 2007-7-12 1:06:45 > top of Java-index,Desktop,Core GUI APIs...
# 3

I am not sure I understand you problem, the two classes that you are taking about is one of them the TableModel.The class which has all the data to be populated into JTable ideally should be your TableModel.

If you still think you need help go ahead and post you code. Make sure that you format your code with tags while posting it.

cantrya at 2007-7-12 1:06:45 > top of Java-index,Desktop,Core GUI APIs...
# 4

Hi Cantry!!

I'm embedding my code in this post,,had to simplify the code as my application's code may not have been understandable,,,

Here's the code

//THIS IS THE CLASS CONTAINING JTable

import java.sql.*;

import java.util.StringTokenizer;

import javax.swing.JOptionPane;

import javax.swing.JTextField;

import javax.swing.*;

public class Test_GUI extends javax.swing.JFrame {

/** Creates new form Test_GUI */

public Test_GUI() {

initComponents();

}

// <editor-fold defaultstate="collapsed" desc=" Generated Code ">

private void initComponents() {

jScrollPane1 = new javax.swing.JScrollPane();

jTable1 = new javax.swing.JTable();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

setTitle("New Allocations");

jTable1.setModel(new javax.swing.table.DefaultTableModel(

new Object [][] {

{null, null, null, null},

{null, null, null, null},

{null, null, null, null},

{null, null, null, null}

},

new String [] {

"Title 1", "Title 2", "Title 3", "Title 4"

}

));

jScrollPane1.setViewportView(jTable1);

org.jdesktop.layout.GroupLayout layout = new

org.jdesktop.layout.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(layout.createSequentialGroup()

.add(57, 57, 57)

.add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 344,

org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)

.addContainerGap(106, Short.MAX_VALUE))

);

layout.setVerticalGroup(

layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(layout.createSequentialGroup()

.add(55, 55, 55)

.add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 143,

org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)

.addContainerGap(247, Short.MAX_VALUE))

);

pack();

}// </editor-fold>

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

Test_GUI tg2=new Test_GUI();

tg2.SetTableValues();

/*

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new Test_GUI().setVisible(true);

}

});

*/

try

{

javax.swing.UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

}

catch(Exception e)

{

e.printStackTrace();

}

Test_GUI tg=new Test_GUI();

tg.setVisible(true);

}

// Variables declaration - do not modify

private javax.swing.JScrollPane jScrollPane1;

public static javax.swing.JTable jTable1;

// End of variables declaration

private javax.swing.JTextField jTextField6;

public static void SetTableValues()

{

System.out.println("here");

Data_Class dc=new Data_Class();

jTable1.setValueAt(dc.Test_data,0,0);

}

}

and code from the other calss,,,

//CLASS FROM WHERE I'M FETCHING THE DATA

public class Data_Class {

static String Test_data="testdata";

/** Creates a new instance of Data_Class */

public Data_Class() {

}

}

thanks in advance...

Kami_Pakistana at 2007-7-12 1:06:45 > top of Java-index,Desktop,Core GUI APIs...
# 5
I have attached the code with my last post,hope somebody could help..thanks
Kami_Pakistana at 2007-7-12 1:06:45 > top of Java-index,Desktop,Core GUI APIs...
# 6

cantry is right, you want your Data_Class to implement the TableModel interface. And then you would set that TableModel as the TableModel that your JTable uses.

So something like

public class Data_Class implements TableModel

or

public class Data_Class extends AbstractTableModel

And then your table, you would set the model, something similar to this:

Data_Class myTableMode = new Data_Class();

JTable myJTable = new JTable (myTableModel);

The Sun tutorial has examples, look at the section on creating a TableModel:

http://java.sun.com/docs/books/tutorial/uiswing/components/table.html

gracklemanna at 2007-7-12 1:06:45 > top of Java-index,Desktop,Core GUI APIs...
# 7
Thanks a lot Cantry and Gracklemann,it's working now,you both have been of great help....Thanks bundles...
Kami_Pakistana at 2007-7-12 1:06:45 > top of Java-index,Desktop,Core GUI APIs...