Combobox Project: How to put 4 text items in the combobox

  • Thread starter christopher.m.lusardi
  • Start date

C

christopher.m.lusardi

How do I change the below project, so that it has the following text as the 4 items of the combobox: Apples, Cars, Shrimp, Moon.



package colorcomboboxeditor;

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.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.event.EventListenerList;

class ColorComboBoxEditor implements ComboBoxEditor
{
final protected JButton editor;
protected EventListenerList listenerList = new EventListenerList();

public ColorComboBoxEditor(Color initialColor)
{
editor = new JButton("");
editor.setBackground(initialColor);

ActionListener actionListener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Color currentBackground = editor.getBackground();
Color color = JColorChooser.showDialog(editor, "Color Chooser", currentBackground);

if ( (color != null) && (currentBackground != color) )
{
editor.setBackground(color);
fireActionEvent(color);
}
}
};

editor.addActionListener(actionListener);
}

public void addActionListener(ActionListener l)
{
listenerList.add(ActionListener.class, l);
}

public Component getEditorComponent()
{
return editor;
}

public Object getItem()
{
return editor.getBackground();
}

public void removeActionListener(ActionListener l)
{
listenerList.remove(ActionListener.class, l);
}

public void selectAll()
{
// Ignore
}

public void setItem(Object newValue)
{
if ( newValue instanceof Color )
{
Color color = (Color) newValue;
editor.setBackground(color);
}
else
{
try
{
Color color = Color.decode(newValue.toString());
editor.setBackground(color);
}
catch (NumberFormatException e)
{
}
}
}

protected void fireActionEvent(Color color)
{
Object listeners[] = listenerList.getListenerList();

for (int i = listeners.length - 2; i >= 0; i -= 2)
{
if ( listeners == ActionListener.class )
{
ActionEvent actionEvent = new ActionEvent(editor, ActionEvent.ACTION_PERFORMED, color.toString());
((ActionListener) listeners[i + 1]).actionPerformed(actionEvent);
}
}
}
}



//-----------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package colorcomboboxeditor;

import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JComboBox;
import javax.swing.JFrame;

public class ColorComboBoxEditorDemo
{

public static void main(String args[])
{
Color colors[] = {Color.WHITE, Color.BLACK, Color.RED, Color.BLUE};
JFrame frame = new JFrame("Editable JComboBox");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final JComboBox comboBox = new JComboBox(colors);
comboBox.setEditable(true);
comboBox.setEditor(new ColorComboBoxEditor(Color.RED));
frame.add(comboBox, BorderLayout.NORTH);

frame.setSize(300, 100);
frame.setVisible(true);
}
}
 
Ad

Advertisements

K

Knute Johnson

How do I change the below project, so that it has the following text as the 4 items of the combobox: Apples, Cars, Shrimp, Moon.



package colorcomboboxeditor;

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.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.event.EventListenerList;

class ColorComboBoxEditor implements ComboBoxEditor
{
final protected JButton editor;
protected EventListenerList listenerList = new EventListenerList();

public ColorComboBoxEditor(Color initialColor)
{
editor = new JButton("");
editor.setBackground(initialColor);

ActionListener actionListener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Color currentBackground = editor.getBackground();
Color color = JColorChooser.showDialog(editor, "Color Chooser", currentBackground);

The code below is problematic as it won't compare whether
currentBackground is the same color, only if it is the same Object. You
must use the Color.equals() method to determine equality.
if ( (color != null) && (currentBackground != color) )
{
editor.setBackground(color);
fireActionEvent(color);
}
}
};

editor.addActionListener(actionListener);
}

public void addActionListener(ActionListener l)
{
listenerList.add(ActionListener.class, l);
}

public Component getEditorComponent()
{
return editor;
}

public Object getItem()
{
return editor.getBackground();
}

public void removeActionListener(ActionListener l)
{
listenerList.remove(ActionListener.class, l);
}

public void selectAll()
{
// Ignore
}

public void setItem(Object newValue)
{
if ( newValue instanceof Color )
{
Color color = (Color) newValue;
editor.setBackground(color);

The code above is redundant. Just say;

editor.setBackground(newValue);
}
else
{
try
{
Color color = Color.decode(newValue.toString());
editor.setBackground(color);
}
catch (NumberFormatException e)
{
}
}
}

protected void fireActionEvent(Color color)
{
Object listeners[] = listenerList.getListenerList();

for (int i = listeners.length - 2; i >= 0; i -= 2)
{
if ( listeners == ActionListener.class )
{
ActionEvent actionEvent = new ActionEvent(editor, ActionEvent.ACTION_PERFORMED, color.toString());
((ActionListener) listeners[i + 1]).actionPerformed(actionEvent);
}
}
}
}



//-----------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package colorcomboboxeditor;

import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JComboBox;
import javax.swing.JFrame;

public class ColorComboBoxEditorDemo
{

public static void main(String args[])
{
Color colors[] = {Color.WHITE, Color.BLACK, Color.RED, Color.BLUE};


You change the line above to be the list of items you want in the combo box.


All Swing GUI creation code must be run on the Event Dispatch Thread
(EDT). To do that you need to put the code below inside of;

EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("Editable JComboBox");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final JComboBox comboBox = new JComboBox(colors);
comboBox.setEditable(true);
comboBox.setEditor(new ColorComboBoxEditor(Color.RED));
frame.add(comboBox, BorderLayout.NORTH);

frame.setSize(300, 100);
frame.setVisible(true);
} }
});
}

It really isn't clear what you are trying to do. Do you want to be able
to add colors to the JComboBox or do you want them to have some obscured
name?
 
C

clusardi2k

My intention is to (1) maintain the 4 different colors already in the project, but to also add the text (Apples, Cars, Shrimp, Moon) described. Each item of the combobox will have its color plus text.

I want to also invoke one of 4 different methods when someone chooses one of the colors. Each corresponding method will print the attached text.

Thank you,
 
K

Knute Johnson

My intention is to (1) maintain the 4 different colors already in the
project, but to also add the text (Apples, Cars, Shrimp, Moon)
described. Each item of the combobox will have its color plus text.

I want to also invoke one of 4 different methods when someone chooses
one of the colors. Each corresponding method will print the attached
text.

Thank you,

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test extends JPanel implements ActionListener {
KColor[] kColors = {
new KColor(200,200,255,"Blue moon","Always a blue moon"),
new KColor(255,100,0,"Orange","Halloween color"),
new KColor(100,20,0,"Toast","I like mine burnt") };

public test() {
setPreferredSize(new Dimension(320,240));

JComboBox<KColor> box = new JComboBox<>(kColors);
box.addActionListener(this);
box.setEditable(true);
add(box);
}

public void actionPerformed(ActionEvent ae) {
JComboBox<KColor> box = (JComboBox<KColor>)ae.getSource();
if (box.getSelectedItem() instanceof String) {
JOptionPane.showMessageDialog(this,"Enter new KColor Data");
// add new KColor to JComboBox
} else {
KColor kColor = (KColor)box.getSelectedItem();
System.out.println(kColor.message);
setBackground(kColor);
}
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.add(new test(),BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
});
}

class KColor extends Color {
public String name;
public String message;

public KColor(int red,int green,int blue,String name,String
message) {
super(red,green,blue);
this.name = name;
this.message = message;
}

public String toString() {
return name;
}

public String showDetail() {
return String.format("%s %d %d %d %s",name,getRed(),
getGreen(),getBlue(),message);
}
}
}
 
M

markspace

public void actionPerformed(ActionEvent ae) {
JComboBox<KColor> box = (JComboBox<KColor>)ae.getSource();
class KColor extends Color {


Your example is much shorter than the OP's, and more clear too (two
things that usually go hand in hand). However the OP used a
ComboBoxEditor, so I wonder if that was the point of his example, or
just an implementation detail.

Of course, he never said....
 
Ad

Advertisements

Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top