2016-11-06 40 views
0

有什麼辦法可以強制JTable專注於整個行而不是單個單元格嗎?我不是在談論行選擇的亮點,只是關於我想在所關注的行中包含所有單元格的焦點邊界。JTable專注於整個行而不是單元格

UPDATE:

表不可編輯的細胞和刪除行的功能:

enter image description here

「:

public class TableTest { 

    private static void createAndShowGUI() { 

     int rows = 20; 
     int cols = 2; 
     String[] headers = { "Column 1", "Column 2" }; 
     String[][] data = new String[rows][cols]; 

     for (int j = 0; j < rows; j++) 
      for (int k = 0; k < cols; k++) 
       data[j][k] = "item " + (j * cols + k + 1); 

     JTable table = new JTable(); 

     DefaultTableModel tableModel = new DefaultTableModel(data, headers) { 
      // Disable editing of the cells 
      @Override 
      public boolean isCellEditable(int row, int column) { 
       return false; 
      } 
     }; 
     table.setModel(tableModel); 
     table.setShowGrid(false); 
     table.setIntercellSpacing(new Dimension(0,0)); 

     // key binding to remove rows 
     InputMap inputMap = table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); 
     ActionMap actionMap = table.getActionMap(); 
     inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "REMOVE"); 
     actionMap.put("REMOVE", new AbstractAction() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       int[] rows = table.getSelectedRows();  
       for (int i = rows.length - 1; i >= 0; i--) { 
        tableModel.removeRow(rows[i]); 
       } 
      } 
     }); 

     JFrame f = new JFrame(); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.getContentPane().add(new JScrollPane(table)); 
     f.setSize(500, 500); 
     f.setVisible(true); 
    } 

    public static void main(String[] args) { 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
        //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {} 
       createAndShowGUI(); 
      } 
     }); 
    } 
} 

上選擇行 」聚焦邊境「焦點邊框「,沒有選擇行:

在Windows LAF

enter image description here

「聚焦邊界」:

enter image description here

我想這個 「邊界集中」 到(可見時),包括該行的所有單元格。

下面是相同的代碼,但我已經添加的@camickr的部分代碼:

public class TableTest { 

    private static void createAndShowGUI() { 

     int rows = 20; 
     int cols = 2; 
     String[] headers = { "Column 1", "Column 2" }; 
     String[][] data = new String[rows][cols]; 

     for (int j = 0; j < rows; j++) 
      for (int k = 0; k < cols; k++) 
       data[j][k] = "item " + (j * cols + k + 1); 


     // ADDED CODE 
     JTable table = new JTable() { 

      private Border outside = new MatteBorder(1, 0, 1, 0, Color.BLACK); 
      private Border inside = new EmptyBorder(0, 1, 0, 1); 
      private Border border = new CompoundBorder(outside, inside); 

      @Override 
      public Component prepareRenderer(TableCellRenderer renderer, int row, int column) { 
       Component c = super.prepareRenderer(renderer, row, column); 
       JComponent jc = (JComponent) c; 
       // Add a border to the selected row 
       if (isRowSelected(row)) jc.setBorder(border); 
       return c; 
      } 
     }; 


     DefaultTableModel tableModel = new DefaultTableModel(data, headers) { 
      // Disable editing of the cells 
      @Override 
      public boolean isCellEditable(int row, int column) { 
       return false; 
      } 
     }; 
     table.setModel(tableModel); 
     table.setShowGrid(false); 
     table.setIntercellSpacing(new Dimension(0,0)); 

     // key binding to remove rows 
     InputMap inputMap = table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); 
     ActionMap actionMap = table.getActionMap(); 
     inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "REMOVE"); 
     actionMap.put("REMOVE", new AbstractAction() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       int[] rows = table.getSelectedRows();  
       for (int i = rows.length - 1; i >= 0; i--) { 
        tableModel.removeRow(rows[i]); 
       } 
      } 
     }); 

     JFrame f = new JFrame(); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.getContentPane().add(new JScrollPane(table)); 
     f.setSize(500, 500); 
     f.setVisible(true); 
    } 

    public static void main(String[] args) { 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
        //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {} 
       createAndShowGUI(); 
      } 
     }); 
    } 
} 

這增加了整個行周圍的邊框(但沒有左/右插圖包括所有細胞) 。這不是「焦點邊框/矩形」,它是僅出現在選定行周圍的邊框。當我刪除一行時,所選行被清除並且焦點邊框重新出現。

請注意,我想隱藏對焦邊界(我知道如何做到這一點),我要保持它的功能一樣,但包括該行,而不是一個小區的所有細胞,當它變得可見。

+0

整個行焦點的意圖是什麼?鍵盤輸入應該去哪裏?或者這只是一個顯示問題,你想要在整個行周圍顯示'FocusBorder'? – kgeorgiy

+0

我只想在整行上顯示FocusBorder。這些單元格是不可編輯的。 – AndroidX

+0

不需要。重點在於顯示開始鍵入時鍵盤輸入的位置。做你想要的東西違背慣例,而不是被設計成框架。唯一的選擇是在每個列的自定義單元格渲染器中自己繪製焦點邊框。在一般情況下,由於列可以由用戶重新排列,所以這很棘手。更簡單的解決方案是根本不使用JTable,而是使用帶有單個定製單元格渲染器的JList。 –

回答

1

我只想在整行上顯示FocusBorder。

看看Table Row Rendering

它顯示了一種方法來在行上放置邊框而不是單個單元格。

+0

感謝您的建議,但它不是我正在尋找的,因爲這不是實際的焦點邊界。我試過了,它只適用於選定的行。如果沒有選擇行,但焦點邊框可見,則它仍佔用一個單元而不是全部。 – AndroidX

+0

@AndroidX,'它只適用於選定的行。' - 實際上只有一行可以同時接受輸入,所以邊界只會在單行上。我沒有看到我提出的代碼在單個單元格上的邊框。一行中沒有「FocusBorder」這樣的東西。如前所述,只有一個單元格可以接受輸入,因此只有一個單元格可以具有看起來像虛線的邊框。您需要通過發佈適當的[SSCCE](http://sscce.org/)來闡明您的問題,以證明您想要更改的行爲。 – camickr

+0

如果你不喜歡「紅色邊框」,那麼你提供自己的「焦點邊框」。我剛給你看了一個概念。您可以定製概念以滿足您的實際要求。 – camickr

相關問題