Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #19232 > unrolled thread
| Started by | christopher.m.lusardi@gmail.com |
|---|---|
| First post | 2012-10-10 12:41 -0700 |
| Last post | 2012-10-11 14:29 -0700 |
| Articles | 6 — 5 participants |
Back to article view | Back to comp.lang.java.programmer
Combobox Project: How to put 4 text items in the combobox christopher.m.lusardi@gmail.com - 2012-10-10 12:41 -0700
Re: Combobox Project: How to put 4 text items in the combobox Knute Johnson <nospam@knutejohnson.com> - 2012-10-10 13:31 -0700
Re: Combobox Project: How to put 4 text items in the combobox clusardi2k@aol.com - 2012-10-10 14:35 -0700
Re: Combobox Project: How to put 4 text items in the combobox Knute Johnson <nospam@knutejohnson.com> - 2012-10-10 16:30 -0700
Re: Combobox Project: How to put 4 text items in the combobox markspace <-@.> - 2012-10-10 16:53 -0700
Re: Combobox Project: How to put 4 text items in the combobox Roedy Green <see_website@mindprod.com.invalid> - 2012-10-11 14:29 -0700
| From | christopher.m.lusardi@gmail.com |
|---|---|
| Date | 2012-10-10 12:41 -0700 |
| Subject | Combobox Project: How to put 4 text items in the combobox |
| Message-ID | <f8e072b4-0c3c-4aac-9ded-83c5962cab9a@googlegroups.com> |
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_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);
}
}
[toc] | [next] | [standalone]
| From | Knute Johnson <nospam@knutejohnson.com> |
|---|---|
| Date | 2012-10-10 13:31 -0700 |
| Message-ID | <k54lvo$9pj$1@dont-email.me> |
| In reply to | #19232 |
On 10/10/2012 12:41 PM, christopher.m.lusardi@gmail.com 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_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?
--
Knute Johnson
[toc] | [prev] | [next] | [standalone]
| From | clusardi2k@aol.com |
|---|---|
| Date | 2012-10-10 14:35 -0700 |
| Message-ID | <eb02218c-281f-4c3e-9219-291e23ee6fce@googlegroups.com> |
| In reply to | #19232 |
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,
[toc] | [prev] | [next] | [standalone]
| From | Knute Johnson <nospam@knutejohnson.com> |
|---|---|
| Date | 2012-10-10 16:30 -0700 |
| Message-ID | <k550dp$dlk$1@dont-email.me> |
| In reply to | #19238 |
On 10/10/2012 2:35 PM, clusardi2k@aol.com 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
[toc] | [prev] | [next] | [standalone]
| From | markspace <-@.> |
|---|---|
| Date | 2012-10-10 16:53 -0700 |
| Message-ID | <k551qn$k4b$1@dont-email.me> |
| In reply to | #19243 |
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....
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2012-10-11 14:29 -0700 |
| Message-ID | <8hee78tos6r8vc5dbhat56q4jt7ap07gsv@4ax.com> |
| In reply to | #19232 |
On Wed, 10 Oct 2012 12:41:36 -0700 (PDT), christopher.m.lusardi@gmail.com 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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web