Solved

TableCellRenderer + TableModel

Posted on 2000-04-18
7
309 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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 about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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.

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now