[英]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.