Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Combobox Project: How to put 4 text items in the combobox

Reply
Thread Tools

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

 
 
christopher.m.lusardi@gmail.com
Guest
Posts: n/a
 
      10-10-2012
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[i] == 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_CLOS E);

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);
}
}
 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      10-10-2012
On 10/10/2012 12:41 PM, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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[i] == 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_CLOS E);
>
> 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?

--

Knute Johnson
 
Reply With Quote
 
 
 
 
clusardi2k@aol.com
Guest
Posts: n/a
 
      10-10-2012
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,
 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      10-10-2012
On 10/10/2012 2:35 PM, (E-Mail Removed) wrote:
> 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);
}
}
}



--

Knute Johnson
 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      10-10-2012
On 10/10/2012 4:30 PM, Knute Johnson wrote:

>
> 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....


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-11-2012
On Wed, 10 Oct 2012 12:41:36 -0700 (PDT),
(E-Mail Removed) wrote, quoted or indirectly quoted
someone who said :

>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.


see http://mindprod.com/jgloss/jcombobox.html
--
Roedy Green Canadian Mind Products http://mindprod.com
The iPhone 5 is a low end Rolex.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
dataridviewcomboboxcolumn / combobox hiding items RS ASP .Net 0 10-07-2008 06:45 PM
combobox / datagridcomboboxcolumn disable or hide items RS ASP .Net 0 10-07-2008 06:35 PM
why does the following with Queue, q.put('\x02', True) not put itin the queue? Gabriel Rossetti Python 3 04-25-2008 03:41 PM
Remove items from a combobox SM Javascript 1 05-26-2007 10:13 AM



Advertisments