Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2005

Tip: Looking for answers? Try searching our database.

Color of JComboBox

Thread view: 
Timo Geissberger - 08 May 2005 02:16 GMT
Hello,
I like to change the color of the JComboBox, my code so far:

JComboBox piclist = new JComboBox(pics);
    piclist.setEditable(true);
    ComboBoxRenderer renderer = new ComboBoxRenderer();
class ComboBoxRenderer extends JLabel implements ListCellRenderer
{
        public ComboBoxRenderer()
        {
         setOpaque(true);
        }

        public Component getListCellRendererComponent(
                                          JList list,
                                          Object value,
                                          int index,
                                          boolean isSelected,
                                          boolean cellHasFocus)
 {
  list.setForeground(Color.green);
  list.setBackground(Color.black);
  return this;
 }
       }

thank you timo
Christian Kaufhold - 08 May 2005 14:35 GMT
> I like to change the color of the JComboBox, my code so far:

Have you tried comboBox.setFore-/Background?
For editable comboBox, also configure the editor component.

Christian
Timo Geissberger - 08 May 2005 16:37 GMT
I tryed it with no success.
thanks anyway

> > I like to change the color of the JComboBox, my code so far:
>
> Have you tried comboBox.setFore-/Background?
> For editable comboBox, also configure the editor component.
>
> Christian
Christian Kaufhold - 08 May 2005 19:18 GMT
> I tryed it with no success.
> thanks anyway

Please post a complete example that shows that behaviour.

Please don't top post.

Please don't whine.

Christian
Timo Geissberger - 08 May 2005 22:05 GMT
what do you mean by don't top post?
here is the program:

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

class vo extends JFrame
{
public vo()
{
 super("vo");
 try{UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
 catch (Exception e){e.printStackTrace();}
 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 this.setBackground(Color.black);
 this.setForeground(Color.green);
 this.setSize(500,400);
 this.setVisible(true);
 String[] pics = {"Hello World"};
 JComboBox piclist = new JComboBox(pics);
      piclist.setEditable(true);
      ComboBoxRenderer renderer = new ComboBoxRenderer();
 piclist.setBackground(Color.black);
 piclist.setForeground(Color.green);
 piclist.setOpaque(true);
 this.getContentPane().add(piclist);
}

public static void main(String args[])
{
 vo wnd = new vo();
 wnd.pack();
}

class ComboBoxRenderer extends JLabel implements ListCellRenderer
 {
        public ComboBoxRenderer()
        {
          setOpaque(true);
        }

        public Component getListCellRendererComponent(JList list,
                                            Object value,
                                            int index,
                                            boolean isSelected,
                                            boolean cellHasFocus)
  {
   setForeground(Color.green);
   setBackground(Color.black);
     list.setForeground(Color.green);
     list.setBackground(Color.black);
     return this;
   }
       }

}

> > I tryed it with no success.
> > thanks anyway
[quoted text clipped - 6 lines]
>
> Christian
Thomas Weidenfeller - 09 May 2005 08:46 GMT
> what do you mean by don't top post?

Is your Google broken?

http://learn.to/quote
http://www.catb.org/~esr/faqs/smart-questions.html

/Thomas
Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq

Christian Kaufhold - 10 May 2005 19:33 GMT
> here is the program:
[snip]

So, which behaviour do you *expect* to be different and how?
It works exactly as expected.

Christian
Timo Geissberger - 10 May 2005 21:38 GMT
Christian Kaufhold schrieb:

>So, which behaviour do you *expect* to be different and how?
>It works exactly as expected.

I like to change the color of the ComboBox Button and the TextField. The
solution for the TextField is:

mytextfield.setEditor(new MyComboBoxEditor());

class MyComboBoxEditor extends BasicComboBoxEditor
{
   public Component getEditorComponent()
   {
       Component comp = super.getEditorComponent();
       comp.setBackground(Coloer.black);
       comp.setForeground(Color.green);
       return comp;
   }
}

and that works okay :)
but now I have a problem with the Button. I have made my own class from the
metal Look and Feel(below) and call it with:

piclist.setUI(new MyUIButton());

and got the error:

.\XerxesMain.java:155: cannot access MyUIButton
bad class file: .\MyUIButton.java
file does not contain class MyUIButton
Please remove or make sure it appears in the correct subdirectory of the
classpa
th.
               piclist.setUI(new MyUIButton());
                                 ^
1 error

the class is:

package javax.swing.plaf.metal;

import java.awt.*;
import java.awt.event.*;
import javax.swing.plaf.basic.*;
import javax.swing.*;
import javax.swing.plaf.*;
import javax.swing.border.*;
import java.io.Serializable;

class MyUIButton extends MetalComboBoxUI
{
   protected JComboBox comboBox;
   protected JList listBox;
   protected CellRendererPane rendererPane;
   protected Icon comboIcon;
   protected boolean iconOnly = false;

   public final JComboBox getComboBox() { return comboBox;}
   public final void setComboBox( JComboBox cb ) { comboBox = cb;}

   public final Icon getComboIcon() { return comboIcon;}
   public final void setComboIcon( Icon i ) { comboIcon = i;}

   public final boolean isIconOnly() { return iconOnly;}
   public final void setIconOnly( boolean isIconOnly ) { iconOnly =
isIconOnly;}

   MyUIButton() {
       super( "" );
       DefaultButtonModel model = new DefaultButtonModel() {
           public void setArmed( boolean armed ) {
               super.setArmed( isPressed() ? true : armed );
           }
       };
       setModel( model );
   }

   public MyUIButton( JComboBox cb, Icon i,
                               CellRendererPane pane, JList list ) {
       this();
       comboBox = cb;
       comboIcon = i;
       rendererPane = pane;
       listBox = list;
       setEnabled( comboBox.isEnabled() );
   }

   public MyUIButton( JComboBox cb, Icon i, boolean onlyIcon,
                               CellRendererPane pane, JList list ) {
       this( cb, i, pane, list );
       iconOnly = onlyIcon;
   }

   public boolean isFocusTraversable() {
return false;
   }

   public void setEnabled(boolean enabled) {
super.setEnabled(enabled);

// Set the background and foreground to the combobox colors.
if (enabled) {
    setBackground(Color.black);
    setForeground(Coloer.green);
} else {
    setBackground(UIManager.getColor("ComboBox.disabledBackground"));
    setForeground(UIManager.getColor("ComboBox.disabledForeground"));
}
   }

   public void paintComponent( Graphics g ) {
       boolean leftToRight = MetalUtils.isLeftToRight(comboBox);

       // Paint the button as usual
       super.paintComponent( g );

       Insets insets = getInsets();

       int width = getWidth() - (insets.left + insets.right);
       int height = getHeight() - (insets.top + insets.bottom);

       if ( height <= 0 || width <= 0 ) {
           return;
       }

       int left = insets.left;
       int top = insets.top;
       int right = left + (width - 1);
       int bottom = top + (height - 1);

       int iconWidth = 0;
       int iconLeft = (leftToRight) ? right : left;

       // Paint the icon
       if ( comboIcon != null ) {
           iconWidth = comboIcon.getIconWidth();
           int iconHeight = comboIcon.getIconHeight();
           int iconTop = 0;

           if ( iconOnly ) {
               iconLeft = (getWidth() / 2) - (iconWidth / 2);
               iconTop = (getHeight() / 2) - (iconHeight / 2);
           }
           else {
        if (leftToRight) {
     iconLeft = (left + (width - 1)) - iconWidth;
 }
 else {
     iconLeft = left;
 }
               iconTop = (top + ((bottom - top) / 2)) - (iconHeight / 2);
           }

           comboIcon.paintIcon( this, g, iconLeft, iconTop );

           // Paint the focus
           if ( comboBox.hasFocus() ) {
               g.setColor( Coloer.green );
               g.drawRect( left - 1, top - 1, width + 3, height + 1 );
           }
       }

       // Let the renderer paint
       if ( ! iconOnly && comboBox != null ) {
           ListCellRenderer renderer = comboBox.getRenderer();
           Component c;
           boolean renderPressed = getModel().isPressed();
           c = renderer.getListCellRendererComponent(listBox,

comboBox.getSelectedItem(),
                                                     -1,
                                                     renderPressed,
                                                     false);
           c.setFont(rendererPane.getFont());

           if ( model.isArmed() && model.isPressed() ) {
               if ( isOpaque() ) {
                   c.setBackground(Color.black);
               }
               c.setForeground(Color.green);
           }
           else if ( !comboBox.isEnabled() ) {
               if ( isOpaque() ) {

c.setBackground(UIManager.getColor("ComboBox.disabledBackground"));
               }

c.setForeground(UIManager.getColor("ComboBox.disabledForeground"));
           }
           else {
               c.setForeground(Color.green);
               c.setBackground(Color.black);
           }

           int cWidth = width - (insets.right + iconWidth);

           // Fix for 4238829: should lay out the JPanel.
           boolean shouldValidate = false;
           if (c instanceof JPanel)  {
               shouldValidate = true;
           }

    if (leftToRight) {
        rendererPane.paintComponent( g, c, this,
         left, top, cWidth, height, shouldValidate );
    }
    else {
        rendererPane.paintComponent( g, c, this,
         left + iconWidth, top, cWidth, height, shouldValidate );
    }
       }
   }
}

thank you timo
Arnaud Berger - 11 May 2005 07:23 GMT
Hi,

The package for your class is wrong :

1) You should NEVER create classes with package containing "java" or
"javax", let alone using an "official" package
like javax.swing.plaf.metal
2) Since javax.swing.plaf.metal is already in another place of the classpath
(i.e the jars of the JDK) you are
running into a "competition" between two paths containing the same package
(generallly, the first which appears in the classpath, is the one taken into
account ; here it is the jars from the JDK, which obviously don't have your
MyUIButton class)

So, just change your package statement to something else (say
mypackage.myuis or whatever but avoid the terms "java" or "javax")

Regards,

Arnaud

> Christian Kaufhold schrieb:
>
[quoted text clipped - 213 lines]
>
> thank you timo
Timo Geissberger - 11 May 2005 19:00 GMT
"Arnaud Berger" <a.berger@libertycontact.fr> schrieb:

> The package for your class is wrong :

okay I have removed it. But now I get some errors I don't understand why
they apear:

.\MyUIButton.java:135: cannot resolve symbol
symbol  : method isOpaque ()
location: class MyUIButton
               if ( isOpaque() ) {
                    ^
.\MyUIButton.java:141: cannot resolve symbol
symbol  : method isOpaque ()
location: class MyUIButton
               if ( isOpaque() ) {
                    ^
.\MyUIButton.java:161: cannot resolve symbol
symbol  : method paintComponent
(java.awt.Graphics,java.awt.Component,MyUIButton
,int,int,int,int,boolean)
location: class javax.swing.CellRendererPane
               rendererPane.paintComponent( g, c, this,
                           ^
.\MyUIButton.java:165: cannot resolve symbol
symbol  : method paintComponent
(java.awt.Graphics,java.awt.Component,MyUIButton
,int,int,int,int,boolean)
location: class javax.swing.CellRendererPane
               rendererPane.paintComponent( g, c, this,
                           ^
23 errors

here is the class again:

import java.awt.*;
import java.awt.event.*;
import javax.swing.plaf.basic.*;
import javax.swing.*;
import javax.swing.plaf.*;
import javax.swing.border.*;
import java.io.Serializable;
import javax.swing.plaf.metal.*;

class MyUIButton extends MetalComboBoxUI
{
   protected JComboBox comboBox;
   protected JList listBox;
   protected CellRendererPane rendererPane;
   protected Icon comboIcon;
   protected boolean iconOnly = false;

   public final JComboBox getComboBox() { return comboBox;}
   public final void setComboBox( JComboBox cb ) { comboBox = cb;}

   public final Icon getComboIcon() { return comboIcon;}
   public final void setComboIcon( Icon i ) { comboIcon = i;}

   public final boolean isIconOnly() { return iconOnly;}
   public final void setIconOnly( boolean isIconOnly ) { iconOnly =
isIconOnly;}

   MyUIButton() {
       super( "" );
       DefaultButtonModel model = new DefaultButtonModel() {
           public void setArmed( boolean armed ) {
               super.setArmed( isPressed() ? true : armed );
           }
       };
       setModel( model );
   }

   public MyUIButton( JComboBox cb, Icon i,
                               CellRendererPane pane, JList list ) {
       this();
       comboBox = cb;
       comboIcon = i;
       rendererPane = pane;
       listBox = list;
       setEnabled( comboBox.isEnabled() );
   }

   public MyUIButton( JComboBox cb, Icon i, boolean onlyIcon,
                               CellRendererPane pane, JList list ) {
       this( cb, i, pane, list );
       iconOnly = onlyIcon;
   }

   public boolean isFocusTraversable() {
return false;
   }

   public void setEnabled(boolean enabled) {
super.setEnabled(enabled);

// Set the background and foreground to the combobox colors.
if (enabled) {
    setBackground(Color.black);
    setForeground(Coloer.green);
} else {
    setBackground(UIManager.getColor("ComboBox.disabledBackground"));
    setForeground(UIManager.getColor("ComboBox.disabledForeground"));
}
   }

   public void paintComponent( Graphics g ) {
       boolean leftToRight = MetalUtils.isLeftToRight(comboBox);

       // Paint the button as usual
       super.paintComponent( g );

       Insets insets = getInsets();

       int width = getWidth() - (insets.left + insets.right);
       int height = getHeight() - (insets.top + insets.bottom);

       if ( height <= 0 || width <= 0 ) {
           return;
       }

       int left = insets.left;
       int top = insets.top;
       int right = left + (width - 1);
       int bottom = top + (height - 1);

       int iconWidth = 0;
       int iconLeft = (leftToRight) ? right : left;

       // Paint the icon
       if ( comboIcon != null ) {
           iconWidth = comboIcon.getIconWidth();
           int iconHeight = comboIcon.getIconHeight();
           int iconTop = 0;

           if ( iconOnly ) {
               iconLeft = (getWidth() / 2) - (iconWidth / 2);
               iconTop = (getHeight() / 2) - (iconHeight / 2);
           }
           else {
        if (leftToRight) {
     iconLeft = (left + (width - 1)) - iconWidth;
 }
 else {
     iconLeft = left;
 }
               iconTop = (top + ((bottom - top) / 2)) - (iconHeight / 2);
           }

           comboIcon.paintIcon( this, g, iconLeft, iconTop );

           // Paint the focus
           if ( comboBox.hasFocus() ) {
               g.setColor( Coloer.green );
               g.drawRect( left - 1, top - 1, width + 3, height + 1 );
           }
       }

       // Let the renderer paint
       if ( ! iconOnly && comboBox != null ) {
           ListCellRenderer renderer = comboBox.getRenderer();
           Component c;
           boolean renderPressed = getModel().isPressed();
           c = renderer.getListCellRendererComponent(listBox,

comboBox.getSelectedItem(),
                                                     -1,
                                                     renderPressed,
                                                     false);
           c.setFont(rendererPane.getFont());

           if ( model.isArmed() && model.isPressed() ) {
               if ( isOpaque() ) {
                   c.setBackground(Color.black);
               }
               c.setForeground(Color.green);
           }
           else if ( !comboBox.isEnabled() ) {
               if ( isOpaque() ) {

c.setBackground(UIManager.getColor("ComboBox.disabledBackground"));
               }

c.setForeground(UIManager.getColor("ComboBox.disabledForeground"));
           }
           else {
               c.setForeground(Color.green);
               c.setBackground(Color.black);
           }

           int cWidth = width - (insets.right + iconWidth);

           // Fix for 4238829: should lay out the JPanel.
           boolean shouldValidate = false;
           if (c instanceof JPanel)  {
               shouldValidate = true;
           }

    if (leftToRight) {
        rendererPane.paintComponent( g, c, this,
         left, top, cWidth, height, shouldValidate );
    }
    else {
        rendererPane.paintComponent( g, c, this,
         left + iconWidth, top, cWidth, height, shouldValidate );
    }
       }
   }
}
Timo Geissberger - 09 May 2005 10:50 GMT
"Christian Kaufhold" <usenet@chka.de> schrieb:

> For editable comboBox, also configure the editor component.

I have seen some code about the EditorComponent, but don't found anything
about the Textfield and the Button in the API.
My tryings with the Editor Component were without success. I got the old
cannot resolve symbol error!?
My code is so far:

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

class vo extends JFrame
{
public vo()
{
 super("vo");
 try{UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
 catch (Exception e){e.printStackTrace();}
 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 this.setBackground(Color.black);
 this.setForeground(Color.green);
 this.setSize(500,400);
 this.setVisible(true);
 String[] pics = {"Hello World"};
 JComboBox piclist = new JComboBox(pics);
      piclist.setEditable(true);
      ComboBoxRenderer renderer = new ComboBoxRenderer();

((JComboBoxEditor)piclist.getEditor()).getTextField().setBackground(Color.bl
ack);

((JComboBoxEditor)piclist.getEditor()).getTextField().setForeground(Color.gr
een);

((JComboBoxEditor)piclist.getEditor()).getButton().setBackground(Color.black
);

((JComboBoxEditor)piclist.getEditor()).getButton().setForeground(Color.green
);
piclist.setBackground(Color.black);
 piclist.setForeground(Color.green);
 piclist.setOpaque(true);
 this.getContentPane().add(piclist);
}

public static void main(String args[])
{
 vo wnd = new vo();
 wnd.pack();
}

class ComboBoxRenderer extends JLabel implements ListCellRenderer
 {
        public ComboBoxRenderer()
        {
          setOpaque(true);
        }

        public Component getListCellRendererComponent(JList list,
                                            Object value,
                                            int index,
                                            boolean isSelected,
                                            boolean cellHasFocus)
  {
   setForeground(Color.green);
   setBackground(Color.black);
     list.setForeground(Color.green);
     list.setBackground(Color.black);
     return this;
   }
       }

}
Arnaud Berger - 09 May 2005 11:03 GMT
Hi,

Try to create your own ComboBoxEditor :

private class MyComboBoxEditor extends BasicComboBoxEditor{

public Component getEditorComponent(){
Component comp=super.getEditorComponent();
comp.setBackground(Color.red);
return comp;
}

}

Then,

piclist.setEditor(new MyComboBoxEditor() );

Regards,

Arnaud

> "Christian Kaufhold" <usenet@chka.de> schrieb:
>
[quoted text clipped - 14 lines]
>  {
>   super("vo");

try{UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
>   catch (Exception e){e.printStackTrace();}
>   this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
[quoted text clipped - 6 lines]
>        piclist.setEditable(true);
>        ComboBoxRenderer renderer = new ComboBoxRenderer();

((JComboBoxEditor)piclist.getEditor()).getTextField().setBackground(Color.bl
> ack);

((JComboBoxEditor)piclist.getEditor()).getTextField().setForeground(Color.gr
> een);

((JComboBoxEditor)piclist.getEditor()).getButton().setBackground(Color.black
> );

((JComboBoxEditor)piclist.getEditor()).getButton().setForeground(Color.green
> );
>  piclist.setBackground(Color.black);
[quoted text clipped - 31 lines]
>
> }
Timo Geissberger - 09 May 2005 17:07 GMT
"Arnaud Berger" <a.berger@libertycontact.fr> schrieb:

> Try to create your own ComboBoxEditor :

Thank you that helped!
My only need is now to get ride of the Button.

Greetings Timo
raven - 08 May 2005 14:59 GMT
JComboBox piclist = new JComboBox(pics);
piclist.setEditable(true);
ComboBoxRenderer renderer = new ComboBoxRenderer();
piclist.setRenderer(renderer);
//...
class ComboBoxRenderer extends JLabel implements ListCellRenderer
{
public ComboBoxRenderer()
{super();
setOpaque(true);
}

public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus)
{
setForeground(Color.green);
setBackground(Color.black);
return this;
}
}

http://www.DevPlug.com --Connecting Developers
Posted from: http://www.devplug.com/ftopic28190.htm
Timo Geissberger - 08 May 2005 16:36 GMT
this does not help the textfield and the butten remain gray black
thanks anyway

> JComboBox piclist = new JComboBox(pics);
> piclist.setEditable(true);
[quoted text clipped - 23 lines]
> http://www.DevPlug.com --Connecting Developers
> Posted from: http://www.devplug.com/ftopic28190.htm


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.