• Post Reply Bookmark Topic Watch Topic
  • New Topic

jTable, Table Models, Data : Can anyone explain how to use them?  RSS feed

 
Darren Wilkinson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello :-)

I have a class which constructs Income Record objects. They have the following fields: incomeName, monthlyAmount.

I have a jTable which needs to show Income Name and Monthly Amount as the column headers. The jTable needs to start with 1 row, empty of data.

What I want is for the user to be able to create a new Income Record object by entering the data into a row on the jTable, then pressing tab, which adds a new blank row to the table and constructs an Income Record object (whose fields contain the entered data).

I admit to being very confused by reading the API on table models, plus the examples on using / creating table models all seems to use data (arrays or vectors) that already exist.

Can anyone explain how jTables, table models and data interact. I am sure there is just some aspect of this that simply hasn't clicked yet. Once it does, then the rest will fall into place.

Thanks for any help :-)

Darren
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at the tutorial on How To Use Tables on Sun's site. It's really good and got me up and running in no time!

Cheers,
Rachel
 
Darren Wilkinson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rachel

I have been using that tutorial but I don't find it explains the subject very clearly.

I have altered my program and have at least got something working although I am sure my use of jTable1.updateUI(); is wrong (it works but it must be the wrong thing to use)

Maybe you could look at the code below and tell me what I should be doing

 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there

OK, to answer your question about Tables...

It's fine to make the table the way that you have, fine for basic tables with static input. When it comes to tables that you're going to be changing, there is a much easier way to make them.

There are two things that you need
1. A class that extends the AbstractTableModel
2. A table created from an instance of this class.

The AbstractTableModel first.
Extending it means that you don't have to make use of all of the methods in your implementation. You should get by using these methods. You can have more methods of course, this is the basic - have a look at the AbstractTableModel for more info;

1. getColumnCount
2. getColumnName
3. getRowCount
4. getValueAt

import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.table.*;

public class MyTableModel extends AbstractTableModel{

Vector rows;
int colCount = 3; //the number of columns you want
String[] headers;

public MyTableModel(){
rows = new Vector();
headers = new String[3];
headers[0] = "column header 1";
headers[1] = "column header 2";
headers[2] = "column header 3";
//etc as many headers as you need
//you can also build this dynamically, just call another method to do it

}

public String getColumnName(int i){return headers[i];}
public int getColumnCount(){return colCount;}
public int getRowCount(){return rows.size();}

public Object getValueAt(int row, int col){
return ((String[])rows.elementAt(row))[col];
}


public void addRow(String one, String two){

String[] record = new String[colCount];

record[0] = one;
record[1] = two;

rows.add(record);

fireTableChanged(null);
}

}

You will see here that I also use a method addRows(). What this does is takes the argument and adds it to the row vector. (Adding a row in the table) and then it calls the fireTableChanged(null). This tells the table that there has been a change and that it needs to show the changed. (I think that this will take care of the updateUI() that you tried). There are a couple of fire() methods to look at. For example, if you changed the layout, or the headers or whatever.

Now the table itself...

MyTableModel edm = new MyTableModel();
JTable myTable = new JTable(edm);
//add the myTable to your container or component and it's ready to use.

Obviously you will need some way to call the addRow method. Example, using a button and in the button's actionPerformed you call edm.addRow();

So how the table sticks together is that the AbstractTableModel takes are of the data inside the table and the jtable takes are of displaying it.

That's the basics of it. If you want to get into cell editing, rendering cells as comboboxes or whatever, you'll have to have gone through this first.

Cool, I hope that that helps. Just give a shout if you need more info!

Cheers,
Rachel


PS - watch your exception handling.
 
Darren Wilkinson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rachel... you are an angel

Have you ever considered writing tutorials? Your explanation has clarified quite a few things for me.

Thank you for taking the time to explain this to me.

I was but now I am

Darren
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Darren! You've made my day!

I hope your coding goes well!

Cheers,
Rachel

 
Darren Wilkinson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Rachel :-)

I have been modifying the code and have run into the following snag. The message is as follows;

java.lang.ArrayIndexOutOfBoundsException: 1 >= 1
at java.util.Vector.elementAt(Vector.java:431)
at jtable_test.MyTableModel.getValueAt(MyTableModel.java:48)
at jtable_test.MyTableModel.getColumnClass(MyTableModel.java:69)
at javax.swing.JTable.getColumnClass(JTable.java:1752)
at javax.swing.JTable.getCellRenderer(JTable.java:3700)
at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:1148)
at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:1051)
at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:974)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java:142)
at javax.swing.JComponent.paintComponent(JComponent.java:541)
at javax.swing.JComponent.paint(JComponent.java:808)
at javax.swing.JComponent.paintWithOffscreenBuffer(JComponent.java:4795)
at javax.swing.JComponent.paintDoubleBuffered(JComponent.java:4748)
at javax.swing.JComponent._paintImmediately(JComponent.java:4692)
at javax.swing.JComponent.paintImmediately(JComponent.java:4495)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:410)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:117)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:178)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:454)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)[d, 120.0, 12.0, 10.0]
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)

The code for MyTableModel is as follows;



Any idea where I am going wrong?

Thanks in advance
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This :


Does not agree with this :



I think you want :



make sense ?
D.
 
Darren Wilkinson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks don

the change worked but i dont understand why ?

darren
 
eric moon
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I think you want :



the change worked but i dont understand why ?


This is why you never name a variable "v":




make more sense?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!