• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Hard JTABLE Question

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all!,
I am coding a contact manager, and I am having trouble making the contacts that the user inputs be written to a JTable.
Let me back up. When a user clicks on the "new" button to add a new contact, an object gets created for this special purpose. A vector gets thrown to its constructor, so that it can hold "contact" objects that the user will create/input.
After a user inputs their data for the contact, they click on a button to "add contact", and this contact gets placed in to a "Contact" object, which then gets stored in a 'Vector'. I want to then "place" the objects within that Vector in to a JTable.
Reading up on Sun's tutorials on JTables, I noted that "If you have your data in a set of Vector objects or Object arrays, you can pass that off to the JTable constructor.". I attempted to do that with the following code:
<code>
contactList = new Vector();
columns = new Vector();
String[] columnNames = {"Last Name", "First Name"};
columns.add(columnNames);
jtable = new JTable(contactList, columns);
JScrollPane scrollPane = new JScrollPane(jtable);
</code>
I then get an error in displaying the JTable, in that the JTable's "top" bar displays the following information:
[LJava,Lang.String;@1ed9e
The only way I can get my JTable to display data correctly is by using the following code:
<code>
String[] columnNames = {"Last Name", "First Name"};
Object[][] data = {{"Rodriguez", "Ricardo"}};
jtable = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane(jtable);
</code>
This, of course, does not help the intent of my program. I would need to add information to the JTable from the already existing Vector that is having Objects being added to it, by the user's input/actions.
I know that this is an involved question, but "How can I make this happen?" Would I need to involve/create a whole other class that extends AbstractTableModel?
Please advise.
 
Rick Rodriguez
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I actually solved my own problem above. I created a class called "MyTableModel" that extends the AbstractTableModel, and I utilize it to update my JTable.
Everything works fine except for 2 items. One, when I tell the MyTableModel to update the JTable with the following code:
<code>
public void updateName(Contact contact)
{
Vector row = new Vector(2);
row.add(contact.getName()); // Gets Contact name
row.add(contact.getEmail()); // Gets Contact e-mail
data.add(row); // Adds them to the vector to upd table
row=null;
contacts.add(contact);
fireTableRowsInserted(data.size()-1, data.size ());
}
</code>
When I do this, it does not "separate" the "Name" and "Email" information in to the appropriate columns. It, instead, places both of the data in both columns. Why is this, and what code to I need to not make the data "sort" to the appropriate columns?
Also, how could I set up an "ActionListener" to look for the user "double-clicking" on a JTable row, in order to look up the contact information that they entered?
I hope there's someone out there that knows the answer (on my knees and praying).
I'd appreciate any feedback that you could give me.
--Rick
 
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,
I've been struggling with flexible JTables in my new job so I think I can help.
// your code:
row.add(contact.getName()); // Gets Contact name
row.add(contact.getEmail()); // Gets Contact e-mail
// should be:
row.add(0 , contact.getName()); // Gets Contact name
row.add(1 , contact.getEmail()); // Gets Contact e-mail
SUGGESTION:
Its better to create your own row-data holder so you can handle different data types elegantly. Here's an example:
First create a data holder class:

Next include the get methods in the datamodel methods. Here is a snippet from a data model. you need to include some more methods to make it work properly.

As for the action listener you can create a JTable and then add an MouseAdapter that checks the number of clicks and retrieves the data from your row objects using JTable.getSelectedRow() and your "RowObject" getXXX() methods.
All this coding takes more time than a simple JTable but the results are VERY flexible.
Hope this ( long answer ) helps,
Terry
[This message has been edited by Terence Doyle (edited December 02, 2001).]
 
Rick Rodriguez
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Terence,
I'm glad that you saw my post, given your experience. I have already tried what you suggested. Here is what I have for my "MyTableData" class:
<code>
public class MyTableModel extends AbstractTableModel
{
static final public String Name = "Name";
static final public String Email = "Email";
protected String[] columnNames={};
protected Vector data = null;
protected Vector contacts = null;
public MyTableModel()
{
super();
data = new Vector();
contacts = new Vector();
columnNames = new String[2];
columnNames[0] = new String("Name");
columnNames[1] = new String("Email");
}
public String getColumnName(int column)
{
if (columnNames[column] != null)
return columnNames[column];
else
return "";
}
public int getColumnCount()
{
return columnNames.length;
}
public int getRowCount()
{
return data.size();
}
public Object getValueAt(int row, int col)
{
return data.elementAt(row);
}
public Contact getContact(int index){
return ((Contact)contacts.get(index));
}
public void removeRow(int row)
{
data.remove(row);
contacts.remove(row);
fireTableDataChanged();
}
public void updateName(Contact contact)
{
Vector row = new Vector(2);
row.add(0, contact.getName());
row.add(1, contact.getEmail());
data.add(row);
row=null;
contacts.add(contact);
fireTableRowsInserted(data.size()-1, data.size());
}
public void clear()
{
contacts.removeAllElements();
data.removeAllElements();
fireTableDataChanged();
}
public void setContacts(Vector v)
{
Contact cnt;
Iterator allContacts = v.iterator();
while (allContacts.hasNext())
{
Vector row = new Vector();
cnt = ((Contact)allContacts.next());
row.addElement(cnt.getName());
row.addElement(cnt.getEmail());
data.addElement(row);
row = null;
}
contacts = v;
fireTableDataChanged();
cnt = null;
}
public Vector getContacts()
{
return contacts;
}
}
</code>
As you can see, I have the "row.add" statements as you suggested, and it still placed both pieces of data in both of the columns. The interesting this is that I do indeed have a "data holder class". It's my "Contacts" object, which holds the data I am attempting to place in the JTable.
What else can I check? Given your experience, I'd appreciate any feedback that you could give me. Thanks Terence.
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rick,
You are facing this problem because you are adding another vector (say temp) to your data Vector but in getValueAt method you are retrieving only the temp vector not the element at "col" position in the temp vector.

change this code

to

Vijay

[This message has been edited by vijay kashyap (edited December 03, 2001).]
 
Rick Rodriguez
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vijay,
Thank you so much!! That worked beautifully!! Thank you so much!!
I'm going to have to buy a JTable "book". I am working on a project for my Advanced Java class (my teacher allowed me to get help for the JTable piece), and since I am working on a contact manager, I knew that I needed to use a JTable, in order to make it more realistic.
Would you know of a good JTable resource? A book that you could recommend would be greatly appreciated. I grabbed my information from the Sun tutorials, but they only had JTable examples with "hard-coded" data, hence my creating that bad "getValueAt()" method.
The only thing I need to do now is to get data from a row, when a user "double-clicks" on it. Would you know of a good online resource to be able to get this information?
I know that you have to "addTableModelListener(JTable variable)", but how would I then call up a "row" with an actionListener. I am so use to using the statement "<component>.addActionListener(this);", for instance. I don't know how to utilize the TableModelListener to get the row data and use that to search my vector.
Any feedback would be greatly appreciated. Thank you again for all of your help!!!
 
A wop bop a lu bop a womp bam boom! Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic