Win a copy of Java by Comparison (eBook) this week in the Java in General forum!

Julie Stella

Greenhorn
+ Follow
since Jan 12, 2001
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Julie Stella

From what event are you calling getSelectedItem()? If you are calling it from an action event, the event is fired twice...once when the combo box is selected and once when a new value is selected from the drop down of the combo box.
HTH
stella
17 years ago
Here's the code...hope I am getting back to you in time. This code has a bunch of other stuff in it...I'll trust that you are able to filter it out. What you need to do is to override getTableCellRendererComponent() then get the renderer and set the background/forground. In my code, we were checking all sorts of conditions before setting the color of the row so ignore all that. Just get the renderer and setBackground/Foreground.
Hope this helps!
Stella
package securecomputing.troller.gui;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import securecomputing.troller.gui.shared.SBResources;
public class PolicyTableCellRenderer extends DefaultTableCellRenderer
implements IPolicyConstants {
public Component getTableCellRendererComponent( JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column) {
Component renderer = null;
if (!((RuleTable)table).isActionApplicable(row, column)) {
try {
String newValue = (String)value + " " + SBResources.getString(
SBResources.fields, "notApplicable.abbreviation");
value = newValue;
}
catch (ClassCastException e) {}
}
if (table == null) {
return super.getTableCellRendererComponent(table,
value,
isSelected,
hasFocus,
row,
column);
}
if (value instanceof Boolean){
TableCellEditor editor = table.getDefaultEditor(value.getClass());
renderer = editor.getTableCellEditorComponent(table,
value,
isSelected,
row,
column);
} else {
renderer = super.getTableCellRendererComponent(table,
value,
isSelected,
hasFocus,
row,
column);
}
((JLabel)renderer).setOpaque(true);
if (isSelected) {
// All selected rules are rendered in the same color.
renderer.setBackground(SELECTED_RULE_BACKGROUND_COLOR);
renderer.setForeground(Color.white);
}
else if (table.getModel() instanceof PolicyTableModel &&
((PolicyTableModel)table.getModel()).isRuleSet(row)) {
// An unselected rule set or its subrule.
renderer.setBackground(RULE_SET_BACKGROUND_COLOR);
renderer.setForeground(Color.white);
}
else if(((RuleSetTableModel)table.getModel()).isRowEnabled(row)) {
// An unselected, enabled regular rule
renderer.setBackground(Color.white);
renderer.setForeground(Color.black);
}
else {
// An unselected, disabled regular rule
renderer.setBackground(DISABLED_RULE_BACKGROUND_COLOR);
renderer.setForeground(Color.black);
}
return renderer;
}
}
17 years ago
This is something like what you want...you'll have to edit to fit your needs but the basic deal is that you need a button renderer AND an editor.
Also...about capturing a double-click on a row--I'd advise against it. I tried it for awhile and noticed that JTable usually tries to interpret it as two separate single-clicks. You can put a timer in there to measure the time between clicks and use that to determine if it is a double or single click but it is very unreliable and hard to test in an IDE.
Hope this helps...
Stella
ButtonRender.java
-----------------
public class ButtonRenderer extends JButton implements TableCellRenderer {
public ButtonRenderer() {
super();
this.setEnabled(true);
this.setFont(new Font("Dialog", Font.ITALIC, 10));
this.setBackground(Color.orange);
}
public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
setText( (value ==null) ? "" : ((JButton)value).getText() );
return this;
}
}
ButtonEditor.java
-----------------
public class ButtonEditor extends DefaultCellEditor {
protected JButton button;
private String label;
private boolean isPushed;
private JTable table;
private TableModel model;
private int row;
private int col;
public ButtonEditor(JCheckBox checkBox, JTable table) {
super(checkBox);
button = new JButton();
button.setOpaque(true);
button.setFont(new Font("Dialog", Font.ITALIC, 10));
button.setBackground(Color.orange);
this.table = table;
model = table.getModel();
this.clickCountToStart = 1;
}
public Component getTableCellEditorComponent(JTable table, Object value,boolean isSelected, int row, int column) {
button = (JButton)value;
button.setFont(new Font("Dialog", Font.ITALIC, 10));
button.setBackground(Color.orange);
this.editorComponent = button;
return button;
}
public Object getCellEditorValue() {
button = (JButton)model.getValueAt(row, col);
return button;
}
public boolean shouldSelectCell(EventObject e) {
row = ((JTable)e.getSource()).getEditingRow();
col = ((JTable)e.getSource()).getEditingColumn();
return true;
}
public boolean stopCellEditing() {
return super.stopCellEditing();
}
protected void fireEditingStopped() {
super.fireEditingStopped();
}
}
17 years ago
Try calling invalidate() first and then repaint(). invalidate() will the container and all parents above it as needing to be laid out and, therefore, repainted.
Hope this helps,
Stella
17 years ago
This is an older post...did you get it resolved? If not, you will want to create a custom TableCellRenderer that assigns color to a specific row before painting. Just put your code in getTableCellRendererComponent.
I have examples but not with me today. If you reply to this post, I will post them for you.
If you've already solved your problem, good for you!
Stella
17 years ago