Reputation: 1645
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.
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();
}
});
}
}
Upvotes: 1
Views: 390
Reputation: 1772
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?
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();
});
}
}
Upvotes: 1