Super26
asked on
Making a JTable not focusable
I have a problem. Initially when my program starts up, I display a JTable that has no rows. The user can then add rows to the table. My problem is when I run the program initially, and I tab through all the text fields and buttons in the panel, once I tab in the JTable, the program blows up because I guess I am trying to select the cells of the table when there are no rows to begin with.
My question is, is there any way that we can remove the focus of the JTable when we use the keyboard tab key? I looked at the table.setFocusable() thing, but that method doesn't exist for some reason. I would appreciate any help.
Thanks
My question is, is there any way that we can remove the focus of the JTable when we use the keyboard tab key? I looked at the table.setFocusable() thing, but that method doesn't exist for some reason. I would appreciate any help.
Thanks
Override isFocusTraversible() to return false;
ASKER
I am sorry, for this is going to be a stupid question. How do we exactly override isFocusTraversible()? Thanks.
public class MyTable extends JTable
{
...
public boolean isFocusTransferable()
{
return false;
}
}
{
...
public boolean isFocusTransferable()
{
return false;
}
}
ASKER
I still can't seem to get it to work. Is there any other way?
Is the MyTable instance still getting focus?
What Java version you using?
What Java version you using?
Can you post your table class, and also where you are using it?
ASKER
I am actually not using a seperate class for my JTable. I am not extending it. I basically just have this:
JTable tableVideoRentalList = new JTable(modelVideoRental);
tableVideoRentalList.setSe lectionMod e(ListSele ctionModel .SINGLE_SE LECTION);
tableVideoRentalList.setPr eferredScr ollableVie wportSize( new Dimension(380, 80));
tableVideoRentalList.getSe lectionMod el().addLi stSelectio nListener( this);
tableVideoRentalList
.getColumnModel()
.getSelectionModel()
.addListSelectionListener( this);
panelVideoRentalList.add(n ew JScrollPane(tableVideoRent alList));
JTable tableVideoRentalList = new JTable(modelVideoRental);
tableVideoRentalList.setSe
tableVideoRentalList.setPr
tableVideoRentalList.getSe
tableVideoRentalList
.getColumnModel()
.getSelectionModel()
.addListSelectionListener(
panelVideoRentalList.add(n
You have to subclass JTable as I suggested above.
afaik it is the only way.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can override the method inline if you don't want to explicitly override JTable. The following is functionally equivalent. (Points should still go to objects because its the same thing).
JTable tableVideoRentalList = new JTable(modelVideoRental) {
public boolean isFocusTraversable() {
return false;
}
};
tableVideoRentalList.setSe lectionMod e(ListSele ctionModel .SINGLE_SE LECTION);
tableVideoRentalList.setPr eferredScr ollableVie wportSize( new Dimension(380, 80));
tableVideoRentalList.getSe lectionMod el().addLi stSelectio nListener( this);
tableVideoRentalList
.getColumnModel()
.getSelectionModel()
.addListSelectionListener( this);
panelVideoRentalList.add(n ew JScrollPane(tableVideoRent alList));
JTable tableVideoRentalList = new JTable(modelVideoRental) {
public boolean isFocusTraversable() {
return false;
}
};
tableVideoRentalList.setSe
tableVideoRentalList.setPr
tableVideoRentalList.getSe
tableVideoRentalList
.getColumnModel()
.getSelectionModel()
.addListSelectionListener(
panelVideoRentalList.add(n
ASKER
Thanks so much guys!! I tried overriding it inline and it worked perfectly! Thanks so much.
ASKER
Thanks Objects and Conick. You've saved me hours of work!
Happy to help :)
http://www.objects.com.au/staff/mick
Brainbench MVP for Java 1
http://www.brainbench.com
http://www.objects.com.au/staff/mick
Brainbench MVP for Java 1
http://www.brainbench.com