JTable专注于整行而不是单元格

时间:2016-11-06 00:36:58

标签: java swing jtable

有没有办法强制JTable专注于整个行而不是单个单元格?我不是在谈论行选择突出显示,而是关于我希望将所有单元格包含在聚焦行上的焦点边界。

更新

包含不可编辑单元格和删除行功能的表格:

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();
            }
        });
    }
}

&#34;聚焦边界&#34;在选定的行上:

enter image description here

&#34;聚焦边界&#34;未选择任何行:

enter image description here

&#34;聚焦边界&#34;在Windows LAF中:

enter image description here

我想要这个&#34;关注边界&#34;包括行的所有单元格(当可见时)。

下面是相同的代码,但我已经添加了@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();
            }
        });
    }
}

这会在整个行周围添加边框(但没有左/右插图以包含所有单元格)。这不是&#34;焦点边框/矩形&#34;,它是仅在选定行周围显示的边框。当我删除一行时,清除所选行并重新出现焦点边框。

请注意,我想要隐藏焦点边框(我知道该怎么做),我想保持它的功能,但要包括行的所有单元格而不是一个单元格当它变得可见时。

1 个答案:

答案 0 :(得分:1)

  

我只想在整行显示FocusBorder。

看看Table Row Rendering

它显示了一种将Border放在行而不是单个单元格上的方法。