2014-09-05 84 views
1

所以我有2個組件在我的框架,表和文本字段。但是當 表具有焦點並且我點擊了tab鍵時,焦點不會進入文字 字段。爲什麼發生這種情況?tab鍵不能轉移焦點爲JTable

public static void main(String[] args) { 
    SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      JFrame frame = new JFrame(); 
      String[][] data = { { "Data" } }; 
      String[] cols = { "COlo" }; 
      JTable table = new JTable(data, cols); 
      table.addFocusListener(new FocusListener() { 

       public void focusLost(FocusEvent arg0) { 
        System.out.println("focus lost"); 
       } 

       public void focusGained(FocusEvent arg0) { 
        System.out.println("gained"); 
       } 
      }); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      frame.add(table, BorderLayout.NORTH); 
      frame.add(new JTextField(), BorderLayout.SOUTH); 
      frame.setVisible(true); 
      table.requestFocus(); 

     } 
    }); 
} 
+1

這是一篇舊文章,但它可能爲您所看到的內容提供解釋。 https://www.java.net/node/651087。另外,請嘗試在StackOverflow中搜索「JTable focustraversalpolicy」 – terrywb 2014-09-05 23:12:00

回答