Solved

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

Posted on 2001-07-16
9
344 Views
Last Modified: 2011-09-20
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.
0
Comment
Question by:tinav
  • 4
  • 3
  • 2
9 Comments
 
LVL 4

Expert Comment

by:Neutron
ID: 6287034
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:)
0
 

Author Comment

by:tinav
ID: 6287132
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.
0
 
LVL 4

Expert Comment

by:Neutron
ID: 6287237

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:)
0
 
LVL 4

Accepted Solution

by:
Neutron earned 200 total points
ID: 6287269
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...
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 4

Expert Comment

by:Neutron
ID: 6287270
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...
0
 

Author Comment

by:tinav
ID: 6287562
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?
0
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 6287731
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
0
 

Author Comment

by:tinav
ID: 6294467
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?
0
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 6295506
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
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now