Solved

TableCellRenderer + TableModel

Posted on 2000-04-18
7
320 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

705 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