I'm currently writing a custom ListCellRenderer
for a JComboBox
. To do this, I'm using a system to fetch a new renderer whenever the L&F changes, and delegate the method to this. This works nicely in all L&Fs. However, when I place this component in a panel (so I can add some more components), it works nicely in the Metal and Windows L&Fs, but the JComboBox
text disappears in Motif. See screenshot and code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TextDemo extends JPanel implements ActionListener {
private static JFrame frame;
public TextDemo() {
super(new GridBagLayout());
JComboBox correct = new JComboBox(new String[]{"One", "Two", "Three"});
JComboBox alsoCorrect = new JComboBox(new String[]{"One", "Two", "Three"});
alsoCorrect.setRenderer(new MyRenderer());
JComboBox incorrect = new JComboBox(new String[]{"One", "Two", "Three"});
incorrect.setRenderer(new NotWorkingRenderer());
JButton button = new JButton("Change LnF");
button.addActionListener(this);
add(correct, getConstraints(0));
add(alsoCorrect, getConstraints(1));
add(incorrect, getConstraints(2));
add(button, getConstraints(3));
}
private GridBagConstraints getConstraints(int y) {
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0; c.gridy = y;
c.insets = new Insets(4,8,4,8);
c.weightx = 1.0; c.weighty = 1.0;
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.FIRST_LINE_START;
return c;
}
@Override
public void actionPerformed(ActionEvent ev) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
SwingUtilities.updateComponentTreeUI(frame);
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static void createAndShowGUI() {
frame = new JFrame("TextDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TextDemo());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
class MyRenderer implements ListCellRenderer {
protected static ListCellRenderer delegate;
static {
refreshRenderers();
UIManager.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals("lookAndFeel")) {
refreshRenderers();
}
}
});
}
protected static void refreshRenderers() {
delegate = new JComboBox().getRenderer();
}
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
return delegate.getListCellRendererComponent(list, value,
index, isSelected, cellHasFocus);
}
}
class NotWorkingRenderer extends MyRenderer {
private JPanel panel = new JPanel();
public NotWorkingRenderer() {
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
}
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
c.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
panel.removeAll();
panel.add(c);
return panel;
}
}
Any help on understanding why this happens would be greatly appreciated!
not an answer,
but see whats happens, with JPanel as renderers JComponents for JComboBox
are you sure that JPanel with String value is proper way, please whats goal,
is there the same effect with default JLabel, (J)Component instead of JPanel
from code
EDIT_1st.
all Standard L&F excluding ModifL&F showing that properly
one step forward ???, code line
incorrect.setEditable(true);
generatingEDIT_2nd.
I'm blind
class NotWorkingRenderer extends MyRenderer {
, pip... pip... pip...phaaa I'm participated on never ever to
add
/remove
/modify
aJComponent
inXxx(Xxx)Renderer
, but is about if is possible or not,