Link to home
Start Free TrialLog in
Avatar of tinav
tinav

asked on

side effect of using setRowSelectionAllowed(false): loss of highlight in selected row

I have defined: JTable dataTable;
and I am using: TableModel tableModel;

to display table rows in the user interface.  When user selects a row, I use:

dataTable.setRowSelectionAllowed(false); to disable selection of any rows in the table and it works.

The problem I have is that as soon as this is executed, the selected row is not highlighted anymore.

disable() method works however, it is deprecated and I don't wnat to use that instead.
Avatar of Neutron
Neutron

You say that disable() does it for you?
Try setEnabled( false ) instead...

If this is what you need, it's for free, just delete the question :o)

Greetings,
    Ntr:)
Avatar of tinav

ASKER

Thanks, it works.  the only problem with this is the row selected is highlighted but, the user can go in the table modle and click on other rows even though the table is now disabled.  I am displaying some data based on the selected row and this way if the user can click on something else, the wrong row is highlighted for the data that is displayed.

To disable selection you can subclass JTable to add some new functionality for disabling selections.

So, you don't add instance of JTable to your panel, but add instance of, let's call it TinavJPanel:

-------8<-------file TinavJPanel.java------cut-here-----

import javax.swing.JPanel;

public class TinavJPanel extends JPanel{

private boolean specialDisable=false;

public void changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend) {
  if (!specialDisable)
      super.changeSelection( rowIndex, columnIndex, toggle, extend );

public void setSpecialDisable( boolean state ){
  specialDisable=state;
}

}

-------8<-------eof-----------------cut-here-------

In your code where you add JTable to a panel, instantiate a TinavJTable instead using the same parameters in constructor which you were using with JTable.

This might even work, but I didn't test this.

This method 'changeSelection' that is overridden is the only one in JTable that receives "cooked" mouse events for selecting, and implemented like this it is being executed only when you call tab.setSpecialDisable( true ), and not when you set it to false (naturally).

Greetings,
    Ntr:)
ASKER CERTIFIED SOLUTION
Avatar of Neutron
Neutron

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Another way to deal with this is to add a selection listener to selection model (with selection MODE set to SINGLE_SELECTION, of course) so you can track when selection has changed, so you can display data of newly selected row...
Avatar of tinav

ASKER

For my project, when the user is in add mode I am not supposed to allow the user click on the other rows.  So this may work but, not for me.  

There is a way to place a glass pane on the model to keep all mouse events away from the model.  The user will see what is there but, nothing happens when you click ona  row.

Do you know how to do that?
Hi,

GlassPane is easy to use when you have it available (like in JFrame). Just call frame.getGlassPane().setVisible(true). The problem is that glassPane covers whole JFrame surface, so all your controls will be unavailable to user. If you plan custom glassPane solution, make sure that glassPane is transparent and that it takes mouseEvents first (it's first in containing container's list of children).

Regards,
Igor Bazarny
Avatar of tinav

ASKER

My frame has four panels.  I need to place the Glass Pane on the first and second panel only.  Do you know how I can do that?  I know you said customize glassPane but, I don't know how to make it cover part of the frame.  Do you have code examples?
Well, it was kind of brainstorming. You can't take existing GlassPane and resize it to cover part of frame, I think. You need to create another container with glassPane/contentPane and use it. I will try to write some code to show that. Or will try to find existing solution.

Regards,
Igor Bazarny