ich habe mir für eine Toolbar einen button gebastelt, der mir einen Dialog öffnet, welcher verschiedene Auswahlmöglichkeiten bietet.
Dies dient in erster Linie zur Vereinfachung des Menüs, falls sich jemand die Frage stellt, warum ich nicht einfach eine ComboBox benutzen kann . Nun möchte ich allerdings noch dafür sorgen können, dass mein Dialog vor jedem Text noch ein Icon anzeigt... Hier mal mein Code für den Dialog:
Code:
private String [] daten = {"1", "2", "3", "4"};
public Point holeButtonPosition()
{
return TestButton.getLocationOnScreen();
}
private void erzeugeListe()
{
ListBox = new JDialog();
ListBox.setUndecorated(true);
Liste = new JList(daten);
Liste.setPreferredSize(new Dimension(100, 70));
Liste.addMouseListener(TestListener);
//Panel für Liste
AuswahlPanel = new JPanel();
AuswahlPanel.add(Liste);
ListBox.add(AuswahlPanel);
}
public void zeigeAuswahl()
{
if(AuswahlBox == null)
{
erzeugeListe();
}
Liste.setVisible(true);
AuswahlPanel.setVisible(true);
AuswahlBox.setVisible(true);
AuswahlBox.setAlwaysOnTop(true);
ListeErneuern(daten, holeButtonPosition());
}
public void ListeVerstecken()
{
if(AuswahlBox!= null)
{
AuswahlBox.setVisible(false);
Liste.removeAll();
}
}
Das klappt swoeit schon ganz gut. Ich habe nun einiges gelesen, komme aber nicht so recht dahinter, wie ich meiner Liste noch die icons unterjubeln kann ? :bahnhof:
Gibt es hier vielleicht jemand, der mir dabei Hilfestellung geben kann?
vielen Dank für deine Antwort. Das ist genau das, was ich suche.
Ich habe mir das Beispiel für die ComboBox angesehen, allerdings komm ich da noch nicht ganz klar. Ich habe eine Klasse "TestListRenderer" und meine ausführende ViewKlasse "TestView". Ich verstehe allerdings das Prinzip noch nicht ganz, wie ich den Renderer genau hingebogen bekomme
Mein Code für den Renderer:
Code:
public class TestRenderer extends JLabel implements ListCellRenderer {
private Font errorFont;
private ImageIcon[] images;
private String[] text;
public TestRenderer() {
setOpaque(true);
setHorizontalAlignment(CENTER);
setVerticalAlignment(CENTER);
}
/*
* This method finds the image and text corresponding
* to the selected value and returns the label, set up
* to display the text and image.
*/
public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
//Get the selected index. (The index param isn't
//always valid, so just use the value.)
int selectedIndex = ((Integer)value).intValue();
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
//Set the icon and text. If icon was null, say so.
ImageIcon icon = images[selectedIndex];
String test = text[selectedIndex];
setIcon(icon);
if (icon != null) {
setText(test);
setFont(list.getFont());
} else {
setUhOhText(test+ " (no image available)",
list.getFont());
}
return this;
}
//Set the font and text when no image was found.
protected void setUhOhText(String uhOhText, Font normalFont) {
if (errorFont == null) { //lazily create this font
errorFont = normalFont.deriveFont(Font.ITALIC);
}
setFont(errorFont);
setText(uhOhText);
}
}
Ich habe nun ein Array mit icons und ein Array mit strings in der aufrufenden Klasse. Ich bekomme es nun aber nicht auf die Reihe eine Liste korrekt zu erzeugen und dann meine Array zu übergeben, so dass immer:
ICON TEXT
ICON TEXT
...
zu sehen ist.
Kannst Du mir da vielleicht nochmal etwas behilflich sein?