package de.ms.swing.tests;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ComboBoxEditor;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.event.EventListenerList;
public class teststart {
public teststart() {
//Color[] c = { Color.BLACK, Color.BLUE, Color.RED, Color.GREEN};
JComboBox jcb = new dddd();
ComboBoxRenderer cbr = new ComboBoxRenderer();
jcb.setRenderer(cbr);
jcb.setEditor(new ColorComboBoxEditor(Color.WHITE));
jcb.setEditable(true);
jcb.addItem("whatever");
jcb.addItem("whatever2");
JFrame fr = new JFrame();
fr.setLayout(new BorderLayout());
fr.add(jcb, BorderLayout.PAGE_START);
fr.setSize(800, 600);
fr.setVisible(true);
}
public static void main(String[] args) {
new teststart();
}
class ColorComboBoxEditor implements ComboBoxEditor {
final protected JButton editor;
transient protected EventListenerList listenerList = new EventListenerList();
public ColorComboBoxEditor(Color initialColor) {
editor = new JButton("");
editor.setBackground(initialColor);
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("test");
}
};
editor.addActionListener(actionListener);
}
@Override
public void addActionListener(ActionListener l) {
// TODO Auto-generated method stub
}
@Override
public Component getEditorComponent() {
// TODO Auto-generated method stub
return editor;
}
@Override
public Object getItem() {
// TODO Auto-generated method stub
return null;
}
@Override
public void removeActionListener(ActionListener l) {
// TODO Auto-generated method stub
}
@Override
public void selectAll() {
// TODO Auto-generated method stub
}
@Override
public void setItem(Object anObject) {
// TODO Auto-generated method stub
}
}
class ComboBoxRenderer extends DefaultListCellRenderer implements ActionListener{
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
JButton bt = new JButton("testButton");
bt.setEnabled(true);
bt.addActionListener(this);
return bt;
}
public void actionPerformed(ActionEvent aev) {
// TODO Auto-generated method stub
//Dieses Event soll passieren, wenn auf ein Button in der Combobox geklickt wird.
System.out.println("hello");
}
}
}