Link to home
Start Free TrialLog in
Avatar of Super26
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  
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Override isFocusTraversible() to return false;
Avatar of Super26
Super26

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;
   }
}
Avatar of Super26

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?
Can you post your table class, and also where you are using it?
Avatar of Super26

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.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
tableVideoRentalList.setPreferredScrollableViewportSize(new Dimension(380, 80));
tableVideoRentalList.getSelectionModel().addListSelectionListener(this);
        tableVideoRentalList
            .getColumnModel()
            .getSelectionModel()
            .addListSelectionListener(this);
        panelVideoRentalList.add(new JScrollPane(tableVideoRentalList));
You have to subclass JTable as I suggested above.
afaik it is the only way.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

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
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.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
tableVideoRentalList.setPreferredScrollableViewportSize(new Dimension(380, 80));
tableVideoRentalList.getSelectionModel().addListSelectionListener(this);
       tableVideoRentalList
           .getColumnModel()
           .getSelectionModel()
           .addListSelectionListener(this);
       panelVideoRentalList.add(new JScrollPane(tableVideoRentalList));
Avatar of Super26

ASKER

Thanks so much guys!!  I tried overriding it inline and it worked perfectly!  Thanks so much.  
Avatar of Super26

ASKER

Thanks Objects and Conick.  You've saved me hours of work!