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

Discussion in 'Java' started by christopher.m.lusardi@gmail.com, Oct 10, 2012.

  1. Guest

    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);
    }
    }
    , Oct 10, 2012
    #1
    1. Advertising

  2. On 10/10/2012 12:41 PM, 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 == 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
    Knute Johnson, Oct 10, 2012
    #2
    1. Advertising

  3. Guest

    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,
    , Oct 10, 2012
    #3
  4. On 10/10/2012 2:35 PM, 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
    Knute Johnson, Oct 11, 2012
    #4
  5. markspace Guest

    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....
    markspace, Oct 11, 2012
    #5
  6. Roedy Green Guest

    On Wed, 10 Oct 2012 12:41:36 -0700 (PDT),
    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.
    Roedy Green, Oct 11, 2012
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UGF1bA==?=

    any easy way to do this?-put items in table

    =?Utf-8?B?UGF1bA==?=, Jun 2, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    351
  2. Gabriel Rossetti
    Replies:
    3
    Views:
    540
    Jerry Hill
    Apr 25, 2008
  3. RS
    Replies:
    0
    Views:
    429
  4. RS
    Replies:
    0
    Views:
    280
  5. SM

    Remove items from a combobox

    SM, May 26, 2007, in forum: Javascript
    Replies:
    1
    Views:
    161
Loading...

Share This Page