Solved

TableCellRenderer + TableModel

Posted on 2000-04-18
7
318 Views
Last Modified: 2008-03-06
Hi,

I'm using a JTable with a DefaultTableCellRenderer, so I can put an Image and a String together in a cell. I want to use also a DefaultTableModel, so I can add a new row and controlling the editing of a cell (I don't want the cell to be edited).
But when I set the model to the table, I don't see the data (which was added by the DefaultTableCellRenderer) in the table.

Please help...
0
Comment
Question by:s_lavie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 3

Expert Comment

by:ovidiucraciun
ID: 2726452
post your code simplified
0
 
LVL 2

Author Comment

by:s_lavie
ID: 2726774
Here the relevant part of the code:

JTable table;
DefaultTableModel tabelModel = new DefaultTableModel();

public void buildTable(Object[][] data, String[] colsTitle)
{
table = new JTable(data, colsTitle);

table.setDefaultRenderer(Object.class, new MyDefaultTableCellRenderer());

// so far so good

/* this line makes the output problem, but I need it for the addNewLine method */
table.setModel(tabelModel);
}

public void addNewLine(Object[] data)
{
tabelModel.addRow(data);
}

What do I do wrong?
 
0
 
LVL 3

Expert Comment

by:ovidiucraciun
ID: 2726794
I don't think the problem is related with line pointed by you.
The model and the renderer shouldn't creat problems one to the other.
So my opinion is that you dfo something wrong in the rendered (MyDefaultTableCellRenderer)
can you post that code?

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Accepted Solution

by:
conick earned 100 total points
ID: 2726843
JTable is based on a MVC (Model-View-Controller) architecture.  The model holds the data, and the view displays it in a pleasing (hopefully) way.
The JTable is the view.  
The DefaultTableModel is the model.

So, first you want to add your data to the model, and then add your model to the view.  
Try this:
Object[][] data= {...};
Object[] colTitle= {...};
DefaultTableModel model= new DefaultTableModel(data,colTitle);
JTable table= new JTable(model);
table.setDefaultRenderer(...);

The renderer has to do with the view.  So you add the renderer to the JTable.
Adding lines changes the data. So to add a line you modify the model.

Everything looks ok except your model doesnt contain any data.  The JTable constructor you use creates a DefaultTableModel (implicitly) and adds you data to it.  You then create a new DefaultTableModel and replace the existing one (the one that has your data) with one containing no data.

Good Luck.
0
 
LVL 7

Expert Comment

by:conick
ID: 2726880
When you change the model, to update the view I believe you need to call:
model.fireTableStructureChanged();
This will tell the view that the model has changed.
0
 
LVL 2

Author Comment

by:s_lavie
ID: 2727080
Adjusted points from 75 to 100
0
 
LVL 2

Author Comment

by:s_lavie
ID: 2727081
Thanks conick :-)
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
hibernate example using maven 12 82
jsp error 6 46
Using jdbcTemplate.batchUpdate to improve INSERT performance 6 27
What browser will run Java? 7 127
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question