Jtable and cell

I need some help for color a single cell in a jtable. I have a Integer matrix (Int [ ][ ]) and based on the content of this matrix (0s or 1s) i have to color a cell. It's for the design of a maze. i've alredy tried to implement the Defaulttablecellrendered but it colors the whole column. Plz help me!

[310 byte] By [fyre3a] at [2007-11-27 7:40:57]
# 1

public class MyTableCellRenderer extends DefaultTableCellRenderer {

@Override

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

final Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

if (matrix[row][column] == 1) {

cell.setBackground(Color.BLACK);

}

else {

cell.setBackground(Color.WHITE);

}

return cell;

}

}

Of course MyTableCellRenderer needs to know your matrix array.

java_knighta at 2007-7-12 19:21:30 > top of Java-index,Desktop,Core GUI APIs...
# 2

thank you. i works perfectly, but i have another question:

i need to call this method different time in my program, also with the same column to change the background during the execution.

So I pass each time a modified matrix (only the int parameters ) from the main program, but after the first time the cells backgrounds don't change. It seems a problem of updating.....

How can i do?

fyre3a at 2007-7-12 19:21:30 > top of Java-index,Desktop,Core GUI APIs...
# 3

You don't need to call getTableCellRendererComponent() directly.

Let's say that your JTable uses MyTableModel (extends AbstractTableModel) as table model.

Just modify the content of your matrix array, then call MyTableModel.fireTableDataChanged() and the cells will automagically update.

java_knighta at 2007-7-12 19:21:30 > top of Java-index,Desktop,Core GUI APIs...
# 4
Hi I have a similar kind of problem.I want change the color, text color of last row of jTable, how can i do that. is there any kind of method likejTable1.setRowColor(int rowNo, Color color); Thanks
jawadaha at 2007-7-12 19:21:30 > top of Java-index,Desktop,Core GUI APIs...
# 5

Great. It works well, but when the instruction fireTableDataChange is inserted in a for cycle, the table updates only at ended cycle or if i place into this cycle a messagedialog.This is an example( i use a DefaultTableModel dtm and rightWay is a Linked list of cases) :

for(i=0;i<RightWay.size();i++){

row=RightWay.get(a).getRow();

column=RightWay.get(a).getColumn();

matrix[row][column]=1;

try {Thread.sleep(500);}

catch(InterruptedException ex) {}

}

dtm.fireTableDataChanged();

}

This is used to show the step to resolve the maze but it update the table only at ended cycle.

While if i put :JOptionPane.showMessageDialog(...) before the fireTableDataChanges instruction, it works...>

fyre3a at 2007-7-12 19:21:30 > top of Java-index,Desktop,Core GUI APIs...
# 6

> I have a similar kind of problem.

> I want change the color, text color of last row of jTable, how can i do that.

That's pretty much the same way.

public class MyOtherTableCellRenderer extends DefaultTableCellRenderer {

@Override

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

final Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

if (row == lastRow) {

cell.setForeground(Color.RED);

}

return cell;

}

}

java_knighta at 2007-7-12 19:21:30 > top of Java-index,Desktop,Core GUI APIs...
# 7
thank you very much, JavaKnightbut I need to update instantaneously this jTable, but it updates (with the function fireTableDataChange) only when a event-function ends (like ButtonActionPerformed) or when i call a dialog. What can i do? Please help, it's for my exam
fyre3a at 2007-7-12 19:21:30 > top of Java-index,Desktop,Core GUI APIs...
# 8

> thank you very much, JavaKnight

> but I need to update instantaneously this jTable, but

> it updates (with the function fireTableDataChange)

> only when a event-function ends (like

> ButtonActionPerformed) or when i call a dialog. What

> can i do? Please help, it's for my exam

Every time you want to update the table, call fireTableDataChanged().

If you need to update it at a given intervals of time, you can use a javax.swing.Timer, I suppose.

java_knighta at 2007-7-12 19:21:30 > top of Java-index,Desktop,Core GUI APIs...
# 9

> but it updates (with the function fireTableDataChange) only when a event-function ends

That correct because you code is executing in the GUI Event DispatchThread, which means the GUI can't repaint itself until the code is finished executing. So you Thread.sleep() cause the EDT to sleep.

The above suggestion of using a Timer to schedule event will work or you can also create a separate Thread for the "looping code".

camickra at 2007-7-12 19:21:30 > top of Java-index,Desktop,Core GUI APIs...
# 10

> I have a similar kind of problem.

In the future create your own posting. Having two discussions in the same thread is confusing. Check out my reply in this posting. It will show how you can override the background color of an individual row:

http://forum.java.sun.com/thread.jspa?forumID=57&threadID=613783

camickra at 2007-7-12 19:21:30 > top of Java-index,Desktop,Core GUI APIs...
# 11
well, i try to use timers but how i don't succeed to stop the main tread and enable the timers.... i try to sleep the thread while i'm fire the Table, but it don't works...thanks to everyone
fyre3a at 2007-7-12 19:21:30 > top of Java-index,Desktop,Core GUI APIs...