使用自定义渲染突出显示JTable中的行选择

时间:2015-09-29 06:58:39

标签: java swing jtable imageicon tablecellrenderer

我正在使用自定义表格单元格渲染器创建JTable,方法是扩展DefaultTableCellRenderer我希望将一些图像与文本一起使用。

enter image description here

如上所示,即使我选择了特定行,也不会在Testcases列中突出显示。 我已经尝试了一些现有的SO问题(Q1Q2)解决方案,但它仍然没有用。我在哪里弄错了?

JTableTest.java

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class JTableTest extends JFrame {

    public DefaultTableModel testcaseModel;
    public JTable testcasesTable;
    Object[] testcaseTableColumns = {"S.No", "Testcases"};
    public static final int TESTCASE_SNO_COLUMN = 0;
    public static final int TESTCASE_NAME_COLUMN = 1;

    public JTableTest() {
        initComponents();
    }

    private void initComponents() {
        if (testcaseModel == null) {
            testcaseModel = new DefaultTableModel(null, testcaseTableColumns){

                @Override
                public Class<?> getColumnClass(int columnIndex) {
//                    if (columnIndex == -1) {
//                        return Integer.class;
//                    } else if(columnIndex==1){
//                        return JLabel.class;
//                    }
                    return Object.class;
                }

            };
        }
        if (testcasesTable == null) {
            testcasesTable = new JTable(testcaseModel) {
                @Override
                public boolean isCellEditable(int row, int column) {
                    return false;
                }
            };
        }
        testcasesTable.setCellSelectionEnabled(true);
        testcasesTable.getColumnModel().getColumn(TESTCASE_NAME_COLUMN).setCellRenderer(new LabelRenderer());
        for (int i = 0; i < 10; i++) {
            testcaseModel.addRow(new Object[]{i, (i * 100)});
        }
    }

    public static void main(String[] args) {
        JTableTest jTableTest = new JTableTest();
        jTableTest.setSize(300, 300);
        jTableTest.setTitle("TableIcon");
        jTableTest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel pnlMain = new JPanel(new BorderLayout(10, 10));
        JScrollPane scrollPane = new JScrollPane(jTableTest.testcasesTable);
        pnlMain.add(scrollPane);
        jTableTest.getContentPane().add(pnlMain);
        jTableTest.setVisible(true);

    }

}

LabelRenderer.java

import java.awt.Component;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableCellRenderer;


public class LabelRenderer extends DefaultTableCellRenderer {

    public static final Border focusedCellBorder = UIManager.getBorder("Table.focusCellHighlightBorder");

    public static final Border unfocusedCellBorder = createEmptyBorder();

    private static Border createEmptyBorder() {
        Insets i = focusedCellBorder.getBorderInsets(new JLabel());
        return BorderFactory.createEmptyBorder(i.top, i.left, i.bottom, i.right);
    }

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

        String text = value.toString();
        JLabel label = new JLabel(text);

        try {
            label.setIcon(new ImageIcon(getClass().getResource("ok_16px.png")));
            // This below code setting the border to be highlighted, but not whole              
            label.setBorder(hasFocus ? focusedCellBorder : unfocusedCellBorder);

        } catch (Exception ex) {
            System.out.println(ex);
        }
       // This also not working.....    
        if (isSelected) {
            label.setBackground(table.getSelectionBackground());
            label.setForeground(table.getSelectionForeground());
        } else {
            label.setBackground(table.getBackground());
            label.setForeground(table.getForeground());
        }
        return label;
    }
}

1 个答案:

答案 0 :(得分:3)

请勿在{{1​​}}中创建另一个JLabelTableCellEditor已从DefaultTableCellEditor延伸,您只需应用所需的设置,离开{{1}照顾其余的

Selected

JLabel

另请注意,启用DefaultTableCellRenderer后,您需要单击第二列中的单元格以查看突出显示。将其关闭以允许完整行选择