[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Cell selection color

Posted on 2004-11-05
35
Medium Priority
?
283 Views
Last Modified: 2010-03-31
I want to do something like this:

// init table and plug-in!
    table = new JTable(model)
    {
      public Color getSelectionBackground()
      {
        if(frame.hasFocus())
        {
          System.out.println("Explorer table HAS focus");
          return Configuration.SELECTION_IN_FOCUS;
        }
        else
        {
          System.out.println("Explorer table has NOT focus");
          return Configuration.SELECTION_OUT_FOCUS;
        }
       
      }
    };

over-ride the getSelectionBackground() method so that the selection color will change when the user clicks onto another frame.
but if the JTable has already been initialised elsewhere, then how do i over-ride these methods
i.e.
JTable table = anotherClass.getTable();

** now over-ride??

Also is this a good way to handle such things??  and what about look&feel - here i have hard coded colors!!

Thanks
0
Comment
Question by:cathalmchale
  • 12
  • 8
  • 7
  • +4
35 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12503084
You should use a custom CellEditor
0
 
LVL 6

Expert Comment

by:expertmb
ID: 12503086
JTable table = anotherClass.getTable();

** now over-ride??
dont think so , you cant override it.
0
 

Author Comment

by:cathalmchale
ID: 12503088
>> CellEditor
why ever so?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:expertmb
ID: 12503099
override the TableCellEditor
getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12503114
Just reading your q again. If you're not talking about when editing, then the renderer can be considered:

http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#editrender
0
 

Author Comment

by:cathalmchale
ID: 12503115
But I dont want to edit cells!
0
 

Author Comment

by:cathalmchale
ID: 12503125
>>  Just reading your q again. If you're not talking about when editing, then the renderer can be considered:

yeah i guess so, but when it comes down to it would a default renderer not be calling the JTable's getSelectionBackground anyway!?
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12503126
try
JTable.isFocusOwner()
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12503134
>>would a default renderer not be calling the JTable's getSelectionBackground anyway!?

Probably not. I've never seen that in Sun's renderer examples
0
 

Author Comment

by:cathalmchale
ID: 12503440
I tried this:

table = new JTable(model)
    {
      public Color getSelectionBackground()
      {
        if(this.isFocusOwner())
        {
          System.out.println("Explorer table HAS focus");
          return Configuration.SELECTION_IN_FOCUS;
        }
        else
        {
          System.out.println("Explorer table has NOT focus");
          return Configuration.SELECTION_OUT_FOCUS;
        }
       
      }
    };

and it "sort of" works, but for some reason, when i click on a different frame (so the JTable goes out of focus) the actual cell which is selected gets the SELECTION_OUT_FOCUS color, but the rest of the row appears selected with SELECTION_IN_FOCUS color.
  Any idea why??
0
 

Author Comment

by:cathalmchale
ID: 12503737
strangely i get the same funny results when i do:

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
  {
    int min = table.getSelectedRow(), max = table.getSelectedRow() + table.getSelectedRowCount() - 1;
    // set the text and icon on the JLabel (the super class) and return this component
    this.setText(value.toString());
    // determine selection color
    if (row >= min && row <= max)
    {
      if(table.isFocusOwner())
      {
        this.setForeground(table.getSelectionForeground());
        this.setBackground(table.getSelectionBackground());
      }
      else
      {
        this.setBackground(SELECTION_OUT_FOCUS);
      }
    }
    else
    {
      this.setForeground(table.getForeground());
      this.setBackground(table.getBackground());
    }

    return this;
  }

??
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12503822
'funny'?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12503846
>> Any idea why??
I think because you don't control when getSelectionBackground()/getTableCellRendererComponent() is called.

Maybe you could - whenever the focus is lost - force it by calling fireTableRowsUpdated() on the table's model.
0
 

Author Comment

by:cathalmchale
ID: 12503854
>> funny = the post above:
and it "sort of" works, but for some reason, when i click on a different frame (so the JTable goes out of focus) the actual cell which is selected gets the SELECTION_OUT_FOCUS color, but the rest of the row appears selected with SELECTION_IN_FOCUS color.

also when i click on a cell for the first time (when the table has been out of focus) the result for
if( table.isFocusOwner )  is false  so thats an anomoly!
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12503863
>>whenever the focus is lost
by adding a WindowListener to your frame.
Then fire in the function
     void windowDeactivated(WindowEvent e)
0
 

Author Comment

by:cathalmchale
ID: 12504853
i can get things to work now using a table renderer in combination with a focus listener (which just calls table.repaint();)
but its difficult to make things generic and i cant help but feel there must be some more standard way for highlighting component focus for what is being worked on at that instance?

perhaps not
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12504953
>> which just calls table.repaint();
OK, that's the "raw" implementation of my idea ;°)
Finer would be: only update what's needed: the selected row using fireTableRowsUpdated()
0
 

Author Comment

by:cathalmchale
ID: 12504973
>> fireTableRowsUpdated()

i actually tried that, and i dont know why but it didnt work!
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12505057
Did you call it like
     fireTableRowsUpdated( theTable.getSelectedRow(), theTable.getSelectedRow() );

?
0
 

Author Comment

by:cathalmchale
ID: 12505065
no actually just called
tableModel.fireRowDataChanged();
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12505142
You mean fireTableDataChanged()?
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 12508843
You don't need a custom renderer, or fire any events to implement what you need.
You should just need a FocusListener that sets the selection background

public class SelectionColorFocusListener
{
   public void focusGained(FocusEvent event)
   {
      JTable table = (JTable) event.getSource();
      table.setSelectionBackground(Configuration.SELECTION_IN_FOCUS);
      table.repaint();  // you may not need this
   }

   public void focusLost(FocusEvent event)
   {
      JTable table = (JTable) event.getSource();
      table.setSelectionBackground(Configuration.SELECTION_OUT_FOCUS);
      table.repaint();  // you may not need this
   }
}
0
 

Author Comment

by:cathalmchale
ID: 12521312
 i have got things working here now, but i want to do the same for a JTree!?  there is only a setSelectionBackground and not foreGround (as far as I can see!) - how do I work things in this case?
0
 
LVL 92

Expert Comment

by:objects
ID: 12521331
A FocusListener as I posted above should also work fine for a tree.
You could even change it slightly so the same class worked for both trees and tables.

0
 

Author Comment

by:cathalmchale
ID: 12521372
>> http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JTable.html#setSelectionForeground(java.awt.Color)

but thats for JTable not JTree!

.. >>same for a JTree!?  there is only a setSelectionBackground and not foreGround (as far as I can see!) -
?
0
 
LVL 92

Expert Comment

by:objects
ID: 12521412
Sorry, getting late in the day here :)

For a tree you'll need to adjust the colour using the renderer

http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/tree/DefaultTreeCellRenderer.html#setTextSelectionColor(java.awt.Color)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12521456
So cathalmchale, if you might wonder how to use that for your needs:

     ((DefaultTreeCellRenderer)yourTree.getCellRenderer()).setTextSelectionColor( ... );
and
     ((DefaultTreeCellRenderer)yourTree.getCellRenderer()).setSelectionColor( ... );
0
 
LVL 92

Expert Comment

by:objects
ID: 12521460
thats what I just said??
0
 
LVL 92

Expert Comment

by:objects
ID: 12521471
And you'd be better off creating your renderer instance instead of assuming that DefaultTreeCellRendereris being used. A lot safer that way.
0
 

Author Comment

by:cathalmchale
ID: 12521485
>>  Sorry, getting late in the day here :)
I wish I could say the same!! ;)

Thanks, this is all sorted now!
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12521488
>>You don't need a custom renderer
So cathalmchale, the renderer idea is back ;°)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12521497
>> Thanks, this is all sorted now!
Good to hear.
0
 
LVL 92

Expert Comment

by:objects
ID: 12521516
(:
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month17 days, 18 hours left to enroll

830 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