Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

TableCellRenderer + TableModel

Posted on 2000-04-18
7
Medium Priority
?
327 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

636 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