• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Refreshing JTable

 
Josh Lanierd
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am experiencing some behavior that I can not figure out with a JTable. I have one large JTable(mainTable) and two small JTables (table1 and table2). When I select an object from one of the two small tables by double-clicking they fill mainTable. table1 and table2 have a different color associated with them to tell them apart. So table1 is shown in red in mainTable and table2 is shown in blue in mainTable.
The problem arises because there are two methods to populate the mainTable. You can double-click or you can right click and choose from a JPopupMenu. When I select the option from the popup the entire mainTable refreshes it's color coding. So if I had 3 objects that were red and two that were blue in mainTable and right clicked on table1 all objects in mainTable would refresh and be shown as red. This same behavior happens if I start the program and change the active window to another window and come back to the program. For example if I start the program and the last object I selected was blue and I go look at something on the internet and come back mainTable refreshes everything as blue.
Can someone please give me some insight into how to fix this. I know there is an onActive or something similar, but how can I set it to not refresh the color? I am sure I am missing something simple, but I would appreciate any help you can give me.
Thanks,
Josh
 
clio katz
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
without code to look at i might suggest checking the
(1) cellrenderer (if custom),
(2) getValueAt/setValue for the jtable cell rendering, and then
(3) whether the code to populate the target table from the source tables is possibly re-using a component (for example, a single jlabel that might be being assigned to multiple cells ... then later subject to setForeground)
hth
 
Josh Lanierd
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hopefully this clarifies some things. I have a custom CellRenderer that allows me to fill the main table with multiple lines.
class TextAreaRenderer extends JTextArea implements TableCellRenderer {
public TextAreaRenderer() {
setLineWrap(true);
setWrapStyleWord(true);
}
public Component getTableCellRendererComponent(JTable jTable,
Object obj, boolean isSelected, boolean hasFocus, int row,
int column) {
setText((String)obj);
return this;
}
}
I set the CellRenderer with the for loop listed below
for(int i=0; i<mainTable.getColumnCount() ; i++) {
mainTable.getColumnModel().getColumn(i).setCellRenderer(t);
}

I set the values with the following function. I make the call with the product and the Table it came from. Then I set the color and the display it.
private void fillTable(String s, JTable table){
String temp = new String();
if (table == table1) {
label1.setText("SW " + product_num);
t.setForeground(Color.RED);
temp = "(SW" + quantity + ',' + "test" + ')';
}
else if (table == table2) {
label2.setText("HW " + product_num);
t.setForeground(Color.BLUE);
temp = "(HW" + quantity + ',' + "test" + ')';
}
// prints out the multi-line row
mainTable.setValueAt(s+'\n'+temp,row,column);
}
I do a couple of things you mention...but what should I look for. Is there a value I need to set in my custom cell renderer to make it not refresh like that?
Thanks, Josh
 
Josh Lanierd
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I apologize...the first post took the indentions out of the code.
class TextAreaRenderer extends JTextArea implements TableCellRenderer {
public TextAreaRenderer() {
setLineWrap(true);
setWrapStyleWord(true);
}
public Component getTableCellRendererComponent(JTable jTable,
Object obj, boolean isSelected, boolean hasFocus, int row,
int column) {
setText((String)obj);
return this;
}
}

for(int i=0; i<mainTable.getColumnCount() ; i++) {
mainTable.getColumnModel().getColumn(i).setCellRenderer(t);
}

private void fillTable(String s, JTable table){
String temp = new String();
if (table == table1) {
label1.setText("SW " + product_num);
t.setForeground(Color.RED);
temp = "(SW" + quantity + ',' + "test" + ')';
}
else if (table == table2) {
label2.setText("HW " + product_num);
t.setForeground(Color.BLUE);
temp = "(HW" + quantity + ',' + "test" + ')';
}

// prints out the multi-line row
mainTable.setValueAt(s+'\n'+temp,row,column);
}
 
clio katz
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry - just getting back to the forum.

if this problem is still occurring for you, i have a
couple of suggestions that i hope would help:

(1) try setting appropriate font color in getTableCellRendererComponent

You may have an indicator that you can use (such as "SW" vs "HW" substring) to do this ... or be able to construct one ... or perhaps use the prior color ie.




(2) other misc notes :

. not sure what 't' var is in 'fillTable' method. this appears to
be the component that you're color-izing - perhaps the main table?

. you're subclassing jtextarea for cell rendering, though cells appear to
be added as plain text (i.e. could stick with a jlabel renderer?)

. there appears to be a mix of locals and (perhaps?) globals in
the 'fillTable' method... which could
produce display/integrity issues (e.g. appears that "quantity" var gets
added to every cell, but can't tell whether "quantity" is an instance
var, class var, primative-in-scope etc)

. won't hurt to have a 'super()' call in your cellrenderer constructor
(esp. if you go to 'jlabel')

. finally ... completely unrelated ... i notice that you don't do anything special if cell is selected. no worries if main table is not selectable.


sorry again for the late response tho

hth
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic