Re: Different height at JComboBox editor and renderer



Here is a some code to illustrate the problem. Notice that the editor
is twice as wide as it needs to be. I am at a lost on how to fix this.

import java.awt.*;

import javax.swing.*;

import javax.swing.border.*;

import java.util.*;

@SuppressWarnings("serial")
public class MultiLineListCellRenderer extends JPanel implements
ListCellRenderer {

private final JLabel preferredNameLabel = new JLabel();

private JLabel[] possibleLabels;

private Font smallFont = null;

protected Border indentBorder;

protected Border defaultBorder = getBorder();

protected Font defaultFont;

public MultiLineListCellRenderer() {

setLayout(new BorderLayout());
int padX = 10;
int indent = 10;
indentBorder = BorderFactory.createEmptyBorder(0, padX + indent, 0,
padX);

add(preferredNameLabel, BorderLayout.CENTER);
defaultFont = getFont();
}

protected void render(Object value) {

Resource model = (Resource) value;

if (smallFont == null) {
smallFont = defaultFont.deriveFont(defaultFont.getSize2D() * 0.8f);
}

String preferredName = model.getPreferredName();
String[] possibleNames = model.getPossibleNames();
if (possibleNames.length > 1) {
if (possibleLabels == null) {
int size = possibleNames.length - 1;
setLayout(new GridLayout(size + 1, 1));
possibleLabels = new JLabel[size];
for (int i = 0; i < size; i++) {
possibleLabels[i] = new JLabel();
possibleLabels[i].setFont(smallFont);
possibleLabels[i].setBorder(indentBorder);
add(possibleLabels[i]);
}
validate();
}
int index = 0;
for (String name : possibleNames) {
if (!name.equals(preferredName)) {
possibleLabels[index].setText(name);
index++;
}
}
}

preferredNameLabel.setBorder(defaultBorder);
preferredNameLabel.setFont(defaultFont);
preferredNameLabel.setEnabled(isEnabled());
preferredNameLabel.setComponentOrientation(getComponentOrientation());
preferredNameLabel.setText(preferredName);
}

public Component getListCellRendererComponent(JList list, Object
value,
int row, boolean isSelected, boolean cellHasFocus) {
Color color;

Component component = getRendererComponent(list, value, row,
isSelected, cellHasFocus);
Resource model = (Resource) value;
if (isSelected) {
preferredNameLabel.setBackground(list.getSelectionBackground());
preferredNameLabel.setForeground(list.getSelectionForeground());
if (possibleLabels != null) {
for (JLabel label : possibleLabels) {
label.setForeground(list.getSelectionForeground());
}
}
} else {
preferredNameLabel.setBackground(list.getBackground());
color = list.getForeground();
preferredNameLabel.setForeground(color);
color = new Color(color.getRed(), color.getGreen(),
color.getBlue(), 160);
if (possibleLabels != null) {
for (JLabel label : possibleLabels) {
label.setForeground(color);
}
}

}

return component;
}

private JComponent getRendererComponent(JComponent component, Object
value,
int index, boolean isSelected, boolean cellHasFocus) {


// Set this cell properties to the same as the list.
setEnabled(component.isEnabled());
setFont(component.getFont());
setComponentOrientation(component.getComponentOrientation());
setOpaque(component.isOpaque());
setBackground(component.getBackground());
setForeground(component.getForeground());

Border border = null;
if (cellHasFocus) {
if (isSelected) {
border = UIManager
.getBorder("List.focusSelectedCellHighlightBorder");
}
if (border == null) {
border = UIManager.getBorder("List.focusCellHighlightBorder");
}
} else {
border = new EmptyBorder(1, 1, 1, 1);
}

setBorder(border);
render(value);
return this;
}


public static void main(String[] args) {

JFrame frame = new JFrame("JComboBoxExample");
Vector vector = new Vector();
vector.add(new Resource("Joe", "Smith"));
vector.add(new Resource("Mary", "Jane"));
JComboBox combobox = new JComboBox(vector);
combobox.setEditable(true);
combobox.setRenderer(new MultiLineListCellRenderer());
frame.getContentPane().add(combobox);
frame.setSize(200, 200);
frame.pack();
frame.setVisible(true);
}

}

public class Resource{


String name1, name2;

public Resource(String name1, String name2) {

this.name1 = name1;
this.name2 = name2;

}

public String getPreferredName() {
return name1;
}

public String[] getPossibleNames() {
return new String[] {name1, name2};
}

public String toString() {
return name1;
}
}

.



Relevant Pages

  • Re: Brian Kernighan, maybe Im not worthy, maybe Im scum
    ... conformant string. ... int repeats, reps; ... ref satisfierLength); ...
    (comp.programming)
  • RE: Controling Modal Dialogs (Solution)
    ... doesn't return until the 'modal' browser returns. ... string varOptions) ... public void DocumentComplete ... int rc = winDisp.Invoke(rgDispId, ref guid, 0, ...
    (microsoft.public.inetsdk.programming.webbrowser_ctl)
  • Gcc compatible header file
    ... A string collection is a table of zero terminated strings that will grow ... typedef struct _StringCollection StringCollection; ... int; ... bool; ...
    (comp.lang.c)
  • Kernighan and Pikes "Beautiful" Code
    ... conformant string. ... int repeats, reps; ... ref satisfierLength); ...
    (comp.programming)
  • Re: FTP CD command
    ... public const int GENERIC_WRITE = 0x40000000; ... string lpszProxyName, ... public static extern IntPtr InternetConnect ( ... public static extern bool FtpGetCurrentDirectory ( ...
    (microsoft.public.dotnet.languages.vb)