?
Solved

TableCellRenderer + TableModel

Posted on 2000-04-18
7
Medium Priority
?
328 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
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 7

Accepted Solution

by:
conick earned 400 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month16 days, 19 hours left to enroll

864 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