设置JTable的特定行的边距和自定义边框

时间:2018-09-18 03:29:37

标签: java swing jtable border

如何通过行号设置JTable中特定行的边距?

请参考下面我使用excel制作的插图。

enter image description here


我还想要粗边框和不可见边框

2 个答案:

答案 0 :(得分:1)

JTable具有方法setRowHeight(...),该方法可让您增加特定行的高度。

关于边界,您可以签出Table Row Rendering。它演示了如何覆盖prepareRenderer(...)的{​​{1}}方法以使用自定义JTable

答案 1 :(得分:1)

这是带有自定义行边框的JTable的示例-用于特定行。这些特定的行由表行中的数据确定。

该示例将JTable与自定义渲染器-CustomBorderRenderer类一起使用,该类扩展了javax.swing.table.DefaultTableCellRenderer。此示例应用程序中有两个类:TableCellRendererTester.javaCustomBorderRenderer.java

以下是具有特定行的自定义边框的表的图像-“年份”值为“ 2012”。这两个类的源代码如下。

enter image description here


示例代码

import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class TableCellRendererTester {

    private static final Object[] TABLE_COLUMNS = {"Book", "Author", "Year"};
    private static final Object [][] TABLE_DATA = {
        {"Book 1", "author 1", "1972"}, {"Book 2", "author 1", "1945"},
        {"Book 3", "author 2", "2012"}, {"Book 4", "author 3", "1999"}
    };

    public static void main(String [] args) {
        new TableCellRendererTester().createAndShowGUI();
    }

    public void createAndShowGUI() {
        JFrame frame = new JFrame("Table Custom Row Border Tester");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(getTablePanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private JPanel getTablePanel() {
        DefaultTableModel model = new DefaultTableModel(TABLE_DATA, TABLE_COLUMNS);
        JTable table = new JTable(model);
        table.setRowHeight(30);
        CustomBorderRenderer renderer = new CustomBorderRenderer();
        table.setDefaultRenderer(Object.class, renderer);
        JScrollPane scrollpane = new JScrollPane(table);
        scrollpane.setPreferredSize(new Dimension(400, 150));
        scrollpane.setViewportView(table);
        JPanel panel = new JPanel();
        panel.add(scrollpane);
        return panel;
    }
}


渲染器类

import java.awt.Color;
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.JComponent;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import javax.swing.table.TableModel;
import javax.swing.table.DefaultTableCellRenderer;

public class CustomBorderRenderer extends DefaultTableCellRenderer {

    private int prevRow;
    private boolean customBorderFlag;

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {

        JComponent c = (JComponent) super.getTableCellRendererComponent(table,
                value, isSelected, hasFocus, row, column);

        if (row != prevRow) {
            // Row has changed.
            // Store it so that all the columns can be rendered 
            // with the custom border - if condition is met.
            customBorderFlag = false;
            prevRow = row;
            TableModel m = table.getModel();
            String s = (String) m.getValueAt(row, 2);
            // Check if the row needs to be rendered with a custom border.
            if (s.equals("2012")) {
                customBorderFlag = true;
            }
        }

        if (customBorderFlag) {
            // Set custom border for all column cells - for a row.
            c.setBorder(getCustomBorder());
        }

        return c;
    }

    /*
     * Returns a custom border with:
     * 1. Thick border
     * 2. A margin between cell content (text) and the border.
     */
    private Border getCustomBorder() {
            Border insideMargin = BorderFactory.createEmptyBorder(10, 10, 10, 10);
            Border thickBorder = BorderFactory.createLineBorder(Color.BLACK, 3);
            Border borderWithMargin = 
                BorderFactory.createCompoundBorder(thickBorder, insideMargin);

            return borderWithMargin;
    }
}


编辑:更新了图像和CustomBorderRenderer.java类,以显示带有边框的自定义边框