2016-11-06 39 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

相关问题