选中时将边框单元格颜色更改为所有 Jtable、JList

Change border cell color to all Jtable, JList when it is selected

我个人的外观有点问题,摔倒了,我不想扩展 MetalLookAndFeel 但我想用 BasicLookAndFell 创建一个纯粹的外观。

在外观和感觉的开发过程中,我意识到当显示jtable,jlist之类的显示组件时,我遇到了标签边框的问题,我在黄色的jlabel上得到了这种效果。

我现在想问你有没有常量的观感来改变这个颜色或者说怎么设置标签,你有什么想法吗?

感谢您的帮助,我将post图片和下面的小演示。

金属外观效果

个人观感效果

/*
 * This code is under license Creative Commons Attribution-ShareAlike 1.0
 * <a href="https://creativecommons.org/licenses/by-sa/1.0/legalcode"></a>
 */
package javaapplication5;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.basic.BasicLookAndFeel;
import javax.swing.table.AbstractTableModel;

/**
 * @author https://github.com/vincenzopalazzo 
 */

public class DemoLookAndFeel extends JFrame {

    static {
        try {
            //UIManager.setLookAndFeel(new MetalLookAndFeel());
            UIManager.setLookAndFeel(new MyLookAndFeel());
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(DemoLookAndFeel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private JTable table;

    public void init() {
        table = new JTable();

        table.setModel(new AbstractTableModel() {
            @Override
            public int getRowCount() {
                return 1;
            }

            @Override
            public int getColumnCount() {
                return 2;
            }

            @Override
            public Object getValueAt(int rowIndex, int columnIndex) {
                if (columnIndex == 0) {
                    return "Pasta";
                }
                return "Italy";
            }
        });

        this.add(table);
        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    private static class MyLookAndFeel extends BasicLookAndFeel {

        @Override
        public String getName() {
            return "my look and feel";
        }

        @Override
        public String getID() {
            return "qwerty";
        }

        @Override
        public String getDescription() {
            return "";
        }

        @Override
        public boolean isNativeLookAndFeel() {
            return false;
        }

        @Override
        public boolean isSupportedLookAndFeel() {
            return true;
        }

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                DemoLookAndFeel demo = new DemoLookAndFeel();
                demo.init();
            }
        });
    }

}

尝试设置 Table.focusCellHighlightBorder property or with getTableCellRendererComponent similar post for this Swing JTable - Highlight selected cell in a different color from rest of the selected row?

示例如何将黄色边框更改为红色(选择边框)

UIManager.put("Table.focusCellHighlightBorder",
        new BorderUIResource.LineBorderUIResource(Color.red));

完整代码

/*
 * This code is under license Creative Commons Attribution-ShareAlike 1.0
 * <a href="https://creativecommons.org/licenses/by-sa/1.0/legalcode"></a>
 */
package javaapplication5;

import java.awt.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.BorderUIResource;
import javax.swing.plaf.basic.BasicLookAndFeel;
import javax.swing.table.AbstractTableModel;

/**
 * @author https://github.com/vincenzopalazzo
 */

public class DemoLookAndFeel extends JFrame {

    static {
        try {
            //UIManager.setLookAndFeel(new MetalLookAndFeel());
            UIManager.setLookAndFeel(new MyLookAndFeel());
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(DemoLookAndFeel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private JTable table;

    public void init() {
        table = new JTable();

        table.setModel(new AbstractTableModel() {
            @Override
            public int getRowCount() {
                return 1;
            }

            @Override
            public int getColumnCount() {
                return 2;
            }

            @Override
            public Object getValueAt(int rowIndex, int columnIndex) {
                if (columnIndex == 0) {
                    return "Pasta";
                }
                return "Italy";
            }
        });

        this.add(table);
        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    private static class MyLookAndFeel extends BasicLookAndFeel {

        @Override
        public String getName() {
            return "my look and feel";
        }

        @Override
        public String getID() {
            return "qwerty";
        }

        @Override
        public String getDescription() {
            return "";
        }

        @Override
        public boolean isNativeLookAndFeel() {
            return false;
        }

        @Override
        public boolean isSupportedLookAndFeel() {
            return true;
        }

    }

    public static void main(String[] args) {
        UIManager.put("Table.focusCellHighlightBorder",
                new BorderUIResource.LineBorderUIResource(Color.red));
        SwingUtilities.invokeLater(() -> {
            DemoLookAndFeel demo = new DemoLookAndFeel();
            demo.init();
        });
    }

}