简体   繁体   English

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

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

I have a little problem with my personal look and fell, I don't want to extend MetalLookAndFeel but I want to create a pure look and feel with BasicLookAndFell.我的个人外观和摔倒有一点问题,我不想扩展 MetalLookAndFeel,但我想用 BasicLookAndFell 创建纯粹的外观和感觉。

During the development of the look and feel I realized I had a problem with the label border when a display component like a JTable, JList is selected, I get this effect here on the yellow JLabel. During the development of the look and feel I realized I had a problem with the label border when a display component like a JTable, JList is selected, I get this effect here on the yellow JLabel.

I wanted to ask you now if there is a constant look and feel to change this color or say how to set the label, do you have any ideas?我现在想问你是否有一个不变的外观和感觉来改变这个颜色或者说如何设置标签,你有什么想法吗?

thanks for your help, I will post pictures with a small demo below.感谢您的帮助,我将在下面发布带有小演示的图片。

Effect with metal look and feel具有金属外观和感觉的效果

在此处输入图片说明

Effect with personal look and feel具有个人外观和感觉的效果

在此处输入图片说明

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

}

Try to set the 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?尝试为此Swing JTable设置Table.focusCellHighlightBorder属性或使用getTableCellRendererComponent类似的帖子- 以与所选行的其余部分不同的颜色突出显示所选单元格?

Example how to change the yellow border to red (the selection border)示例如何将黄色边框更改为红色(选择边框)

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

Full code完整代码

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM