Making a JTable not focusable

Super26
Super26 used Ask the Experts™
on
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  
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mick BarryJava Developer
Top Expert 2010

Commented:
Override isFocusTraversible() to return false;

Author

Commented:
I am sorry, for this is going to be a stupid question.  How do we exactly override isFocusTraversible()?  Thanks.
Mick BarryJava Developer
Top Expert 2010

Commented:
public class MyTable extends JTable
{
   ...

   public boolean isFocusTransferable()
   {
      return false;
   }
}
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Author

Commented:
I still can't seem to get it to work.  Is there any other way?  
Mick BarryJava Developer
Top Expert 2010

Commented:
Is the MyTable instance still getting focus?
What Java version you using?
Mick BarryJava Developer
Top Expert 2010

Commented:
Can you post your table class, and also where you are using it?

Author

Commented:
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));
Mick BarryJava Developer
Top Expert 2010

Commented:
You have to subclass JTable as I suggested above.
Mick BarryJava Developer
Top Expert 2010

Commented:
afaik it is the only way.
Java Developer
Top Expert 2010
Commented:
complete table subclass as needed:

public class MyTable extends JTable
{
  public MyTable(TableModel model)
  {
    super(model);
  }

  public boolean isFocusTransferable()
  {
     return false;
  }
}

Commented:
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));

Author

Commented:
Thanks so much guys!!  I tried overriding it inline and it worked perfectly!  Thanks so much.  

Author

Commented:
Thanks Objects and Conick.  You've saved me hours of work!
Mick BarryJava Developer
Top Expert 2010

Commented:
Happy to help :)

http://www.objects.com.au/staff/mick
Brainbench MVP for Java 1
http://www.brainbench.com

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial