[英]How to change cell's border whenever new cell is selected in jtable?
[英]Change border cell color to all Jtable, JList when it is selected
我的个人外观和摔倒有一点问题,我不想扩展 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.
我现在想问你是否有一个不变的外观和感觉来改变这个颜色或者说如何设置标签,你有什么想法吗?
感谢您的帮助,我将在下面发布带有小演示的图片。
具有金属外观和感觉的效果
具有个人外观和感觉的效果
/*
* 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();
}
});
}
}
尝试为此Swing JTable设置Table.focusCellHighlightBorder属性或使用getTableCellRendererComponent类似的帖子- 以与所选行的其余部分不同的颜色突出显示所选单元格?
示例如何将黄色边框更改为红色(选择边框)
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();
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.