Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #3698 > unrolled thread

cobbling a ComboBox

Started by"Albretch Mueller" <albretch.mueller@THRWHITE.remove-dii-this>
First post2011-04-27 15:46 +0000
Last post2011-04-27 15:46 +0000
Articles 9 — 2 participants

Back to article view | Back to comp.lang.java.gui


Contents

  cobbling a ComboBox "Albretch Mueller" <albretch.mueller@THRWHITE.remove-dii-this> - 2011-04-27 15:46 +0000
    Re: cobbling a ComboBox "Daniele Futtorovic" <daniele.futtorovic@THRWHITE.remove-dii-this> - 2011-04-27 15:46 +0000
      Re: cobbling a ComboBox "Albretch Mueller" <albretch.mueller@THRWHITE.remove-dii-this> - 2011-04-27 15:46 +0000
        Re: cobbling a ComboBox "Daniele Futtorovic" <daniele.futtorovic@THRWHITE.remove-dii-this> - 2011-04-27 15:46 +0000
          Re: cobbling a ComboBox "Albretch Mueller" <albretch.mueller@THRWHITE.remove-dii-this> - 2011-04-27 15:46 +0000
            Re: cobbling a ComboBox "Daniele Futtorovic" <daniele.futtorovic@THRWHITE.remove-dii-this> - 2011-04-27 15:46 +0000
              Re: cobbling a ComboBox "Daniele Futtorovic" <daniele.futtorovic@THRWHITE.remove-dii-this> - 2011-04-27 15:46 +0000
                Re: cobbling a ComboBox "Albretch Mueller" <albretch.mueller@THRWHITE.remove-dii-this> - 2011-04-27 15:46 +0000
                  Re: cobbling a ComboBox "Daniele Futtorovic" <daniele.futtorovic@THRWHITE.remove-dii-this> - 2011-04-27 15:46 +0000

#3698 — cobbling a ComboBox

From"Albretch Mueller" <albretch.mueller@THRWHITE.remove-dii-this>
Date2011-04-27 15:46 +0000
Subjectcobbling a ComboBox
Message-ID<1214257403.665864@nntp.acecape.com>
  To: comp.lang.java.gui
 I have a JComboBox that looks like this:

 http://www.geocities.com/tekmonk2005/pix/ComboBoxColors.jpg

 I would like to use the color on the top title bar to use it as the
background of the selected item in the combobox, so it kind of looks like a
continuation of what was suggested in the title bar

 But I don't know how to ask the UI Manager to give me the color of the top
title bar a JFrame uses. If you go:

 UIManager.get("InternalFrame.borderColor")

 you will get something like:

 javax.swing.plaf.ColorUIResource[r=238,g=238,b=238]

 Which is the grey color of the default Window background in the Metal L&F

 Also I don't know how to only have the selection boldened (as in the window
bar), when the selection is made. If you go:

 UIManager.put("ComboBox.font", new Font("Monospaced", Font.PLAIN +
Font.BOLD, 16));

 The font stays bold all the time

 I would like for it to be bold only when you select it

 Is this possible?

 thanx
 lbrtchx

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [next] | [standalone]


#3699

From"Daniele Futtorovic" <daniele.futtorovic@THRWHITE.remove-dii-this>
Date2011-04-27 15:46 +0000
Message-ID<g3pac7$aki$1@registered.motzarella.org>
In reply to#3698
  To: comp.lang.java.gui
On 2008-06-23 23:43 +0100, Albretch Mueller allegedly wrote:
>  I have a JComboBox that looks like this:
> 
>  http://www.geocities.com/tekmonk2005/pix/ComboBoxColors.jpg
> 
>  I would like to use the color on the top title bar to use it as the
> background of the selected item in the combobox, so it kind of looks like a
> continuation of what was suggested in the title bar
> 
>  But I don't know how to ask the UI Manager to give me the color of the top
> title bar a JFrame uses. If you go:
> 
>  UIManager.get("InternalFrame.borderColor")
> 
>  you will get something like:
> 
>  javax.swing.plaf.ColorUIResource[r=238,g=238,b=238]
> 
>  Which is the grey color of the default Window background in the Metal L&F

Try UIManager.getColor(Object).

>  Also I don't know how to only have the selection boldened (as in the window
> bar), when the selection is made. If you go:
> 
>  UIManager.put("ComboBox.font", new Font("Monospaced", Font.PLAIN +
> Font.BOLD, 16));
> 
>  The font stays bold all the time
> 
>  I would like for it to be bold only when you select it
> 
>  Is this possible?

Don't know about that. You might try to dump all the UIManager keys
(UIManager.getDefaults().keySet()) and look for the appropriate key.
Alternatively, find a webpage which lists all of them -- unfortunately,
I'm not aware of such a resource.

Chances are such nuanced behaviour as you want may not be available. In
that case, you may end up having to write/override the ComponentUI itself.

-- 
DF.
to reply privately, change the top-level domain
in the FROM address from "invalid" to "net"

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#3705

From"Albretch Mueller" <albretch.mueller@THRWHITE.remove-dii-this>
Date2011-04-27 15:46 +0000
Message-ID<1214321081.136206@nntp.acecape.com>
In reply to#3699
  To: comp.lang.java.gui
Daniele Futtorovic wrote:

> Try UIManager.getColor(Object).
 
 Actually I had done both.

 If you go:

  JFrame JFrm = new JFrame(" Seting Parsing Rules for: ");
  Clr = UIManager.getColor(JFrm);
System.out.println("// __ UIManager.getColor(JFrm): |" + Clr + "|");

 you will get:

// __ UIManager.getColor(JFrm): |null|

> You might try to dump all the UIManager keys
(UIManager.getDefaults().keySet()) and look for the appropriate key.

 That I did also via:

  String aKey;
  Hashtable HTUIDef = UIManager.getDefaults();
  Enumeration HTKeys = HTUIDef.keys();
  while(HTKeys.hasMoreElements()){
   Object ObjKey = HTKeys.nextElement();
   if(ObjKey instanceof String){
. . .
   }
  }

 But I don't see which of these is the one I need

sh-3.1# cat t.txt | grep Color
// __ |TabbedPane.contentAreaColor|
// __ |List.dropLineColor|
// __ |InternalFrame.borderColor|
// __ |ToolBar.borderColor|
// __ |Slider.tickColor|
// __ |Tree.dropLineColor|
// __ |Table.dropLineColor|
// __ |ColorChooser.swatchesRecentSwatchSize|
// __ |ColorChooser.swatchesSwatchSize|
// __ |SplitPane.dividerFocusColor|
// __ |ColorChooser.foreground|
// __ |ColorChooser.swatchesDefaultRecentColor|
// __ |Table.gridColor|
// __ |SplitPaneDivider.draggingColor|
// __ |TabbedPane.borderHightlightColor|
// __ |Tree.selectionBorderColor|
// __ |MenuBar.borderColor|
// __ |ColorChooser.background|
// __ |Table.sortIconColor|
// __ |TitledBorder.titleColor|
// __ |Table.dropLineShortColor|
// __ |ColorChooserUI|
// __ |Slider.altTrackColor|
sh-3.1# cat t.txt | grep color
// __ |RootPane.colorChooserDialogBorder|
~ 
> Chances are such nuanced behaviour as you want may not be available.
~ 
 They are nuanced behaviours but I am trying to be a little more intuitive
to the user (in my understanding) and make the best use of the window's
real estate
~ 
 Thank you Daniele.
 Daniele for president!!! ;-) (Well, I could tell you may not be that bored
to become a politician)
 lbrtchx

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#3706

From"Daniele Futtorovic" <daniele.futtorovic@THRWHITE.remove-dii-this>
Date2011-04-27 15:46 +0000
Message-ID<g3reau$avf$1@registered.motzarella.org>
In reply to#3705
  To: comp.lang.java.gui
On 2008-06-24 05:25 +0100, Albretch Mueller allegedly wrote:
> Daniele Futtorovic wrote:
> 
>> Try UIManager.getColor(Object).
>  
>  Actually I had done both.

I meant UIManager.getColor("InternalFrame.borderColor").

Note that ColorUIResource *extends* java.awt.Color.


> 
>  If you go:
> 
>   JFrame JFrm = new JFrame(" Seting Parsing Rules for: ");
>   Clr = UIManager.getColor(JFrm);
> System.out.println("// __ UIManager.getColor(JFrm): |" + Clr + "|");
> 
>  you will get:
> 
> // __ UIManager.getColor(JFrm): |null|
> 
>> You might try to dump all the UIManager keys
> (UIManager.getDefaults().keySet()) and look for the appropriate key.
> 
>  That I did also via:
> 
>   String aKey;
>   Hashtable HTUIDef = UIManager.getDefaults();
>   Enumeration HTKeys = HTUIDef.keys();
>   while(HTKeys.hasMoreElements()){
>    Object ObjKey = HTKeys.nextElement();
>    if(ObjKey instanceof String){
> .. . .
>    }
>   }

for(Object o: UIManager.getDefaults().keySet()){
     System.out.println(o);
}

;)

>> Chances are such nuanced behaviour as you want may not be available.
> ~ 
>  They are nuanced behaviours but I am trying to be a little more intuitive
> to the user (in my understanding) and make the best use of the window's
> real estate

Is the following what you try to achieve? Mind, it only works for one
specific L&F at a time (in this case "Metal", the cross-platform L&F).

<sscce>
package scratch;

import java.util.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.*;
import javax.swing.plaf.basic.*;
import javax.swing.plaf.metal.*;

public class Test {
     public static void main(String[] ss) throws Exception {
         UIManager.put("ComboBoxUI", "scratch.Test$CustomMetalComboBoxUI");

         Font fo = new Font("Sans Serif", Font.PLAIN, 12);
         UIManager.put("ComboBox.font", new FontUIResource(fo));

         fo = fo.deriveFont( Font.BOLD | Font.ITALIC );
         UIManager.put("ComboBox.popupSelectedFont", new 
FontUIResource(fo));

         UIManager.put("ComboBox.popupSelectedBackground", new 
ColorUIResource(0xfff0a0a0));

         EventQueue.invokeLater( new Runnable(){
             public void run(){
                 JComboBox jcb = new JComboBox(new Object[]{"One", 
"Two", "Three", "Four", "Five", "Six"});

                 System.out.println(jcb.getUI().getClass().toString());

                 jcb.setBorder( BorderFactory.createEmptyBorder(40, 60, 
40, 60) );

                 JFrame f = new JFrame();
                 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                 f.getContentPane().add(jcb, BorderLayout.CENTER);

                 f.pack();
                 f.setLocationRelativeTo(null);

                 f.setVisible(true);
             }
         });
     }

     public static class CustomMetalComboBoxUI
     extends MetalComboBoxUI
     {
         public static ComponentUI createUI(JComponent c) {
             return new CustomMetalComboBoxUI();
         }

         @Override
         protected ListCellRenderer createRenderer() {
             return new CustomComboBoxRenderer();
         }

         private class CustomComboBoxRenderer
         extends BasicComboBoxRenderer
         {
             final Font selectedFont = 
UIManager.getFont("ComboBox.popupSelectedFont");
             final Color selectedBackground = 
UIManager.getColor("ComboBox.popupSelectedBackground");

             @Override
             public Component getListCellRendererComponent(
                                                          JList list,
                                                          Object value,
                                                          int index,
                                                          boolean 
isSelected,
                                                          boolean 
cellHasFocus)
             {
                 Component c = super.getListCellRendererComponent(list, 
value, index, isSelected, cellHasFocus);

                 if( index == 
CustomMetalComboBoxUI.this.comboBox.getSelectedIndex() ){
                     if( selectedFont != null ){
                         c.setFont( selectedFont );
                     }

                     if( selectedBackground != null && ! isSelected ){
                         c.setBackground( selectedBackground );
                     }
                 }

                 return c;
             }
         }
     }
}
</sscce>

-- 
DF.
to reply privately, change the top-level domain
in the FROM address from "invalid" to "net"

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#3708

From"Albretch Mueller" <albretch.mueller@THRWHITE.remove-dii-this>
Date2011-04-27 15:46 +0000
Message-ID<1214354521.815064@nntp.acecape.com>
In reply to#3706
  To: comp.lang.java.gui
(new javax.swing.JComboBox()).getUI() I causing a NP Exception
~ 
sh-3.1# javac DanieleFurttorovic00.java
sh-3.1# java DanieleFurttorovic00
UIDefaults.getUI() failed: no ComponentUI class for:
javax.swing.JComboBox[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.0,border=,
flags=16777224,maximumSize=,minimumSize=,preferredSize=,isEditable=false,
lightWeightPopupEnabled=true, maximumRowCount=8,selectedItemReminder=One]
java.lang.Error
 at javax.swing.UIDefaults.getUIError(UIDefaults.java:711)
 at javax.swing.MultiUIDefaults.getUIError(MultiUIDefaults.java:117)
 at javax.swing.UIDefaults.getUI(UIDefaults.java:741)
 at javax.swing.UIManager.getUI(UIManager.java:1012)
 at javax.swing.JComboBox.updateUI(JComboBox.java:247)
 at javax.swing.JComboBox.init(JComboBox.java:212)
 at javax.swing.JComboBox.<init>(JComboBox.java:178)
 at DanieleFurttorovic00$1.run(DanieleFurttorovic00.java:23)
 at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
 at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
 at
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
 at
java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
 at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
 at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
 at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
 at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
// __ jcb: 
javax.swing.JComboBox[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.0,border=,
flags=16777224,maximumSize=,minimumSize=,preferredSize=,isEditable=false,
lightWeightPopupEnabled=true,maximumRowCount=8,selectedItemReminder=One]|
// __ jcb.getUI(): |null|
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
 at DanieleFurttorovic00$1.run(DanieleFurttorovic00.java:29)
 at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
 at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
 at
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
 at
java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
 at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
 at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
 at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
 at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#3709

From"Daniele Futtorovic" <daniele.futtorovic@THRWHITE.remove-dii-this>
Date2011-04-27 15:46 +0000
Message-ID<g3s832$5g1$1@registered.motzarella.org>
In reply to#3708
  To: comp.lang.java.gui
On 2008-06-24 14:42 +0100, Albretch Mueller allegedly wrote:
> (new javax.swing.JComboBox()).getUI() I causing a NP Exception
> ~ 
> sh-3.1# javac DanieleFurttorovic00.java
> sh-3.1# java DanieleFurttorovic00
> UIDefaults.getUI() failed: no ComponentUI class for:
> javax.swing.JComboBox[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.0,border=,
> flags=16777224,maximumSize=,minimumSize=,preferredSize=,isEditable=false,
> lightWeightPopupEnabled=true, maximumRowCount=8,selectedItemReminder=One]

Mind the package directory structure.

Note the package declaration in the code I posted:
   package scratch;

This is important for the following line to work:
   UIManager.put("ComboBoxUI", "scratch.Test$CustomMetalComboBoxUI");
(the runtime will try a Class.forName(String) with the value of that entry).

You need a directory X. Then you need the code in
   X/scratch/Test.java

go to directory X, compile. Run *from that directory* using
   "java -cp . scratch.Test"

This should work.

Since you weren't able to resolve that error on your own, I assume there
will be at least some things in that code you might not understand.
Please, try to figure them out (GIYF). Don't simply copy and paste code.
It would be nigh-worthless that way. I won't give you an overall
explanation, but if you come back at me with specific points, I'll try
my best to lay them out.

Always read the JavaDocs:
<http://java.sun.com/javase/6/docs/api/>

Check the SUN tutorials:
<http://java.sun.com/docs/books/tutorial/reallybigindex.html>

-- 
DF.
to reply privately, change the top-level domain
in the FROM address from "invalid" to "net"

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#3710

From"Daniele Futtorovic" <daniele.futtorovic@THRWHITE.remove-dii-this>
Date2011-04-27 15:46 +0000
Message-ID<g3s91e$696$1@registered.motzarella.org>
In reply to#3709
  To: comp.lang.java.gui
On 2008-06-25 03:50 +0100, Daniele Futtorovic allegedly wrote:
> On 2008-06-24 14:42 +0100, Albretch Mueller allegedly wrote:
>> (new javax.swing.JComboBox()).getUI() I causing a NP Exception
>> ~ sh-3.1# javac DanieleFurttorovic00.java
>> sh-3.1# java DanieleFurttorovic00
>> UIDefaults.getUI() failed: no ComponentUI class for:
>> javax.swing.JComboBox[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.0,border=, 
>>
>> flags=16777224,maximumSize=,minimumSize=,preferredSize=,isEditable=false,
>> lightWeightPopupEnabled=true, maximumRowCount=8,selectedItemReminder=One]
> 
> Mind the package directory structure.

You must also name the class as I did or make the according 
modifications to the code.

-- 
DF.
to reply privately, change the top-level domain
in the FROM address from "invalid" to "net"

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#3713

From"Albretch Mueller" <albretch.mueller@THRWHITE.remove-dii-this>
Date2011-04-27 15:46 +0000
Message-ID<1214435196.953231@nntp.acecape.com>
In reply to#3710
  To: comp.lang.java.gui
 Hi Daniele et al,

 I have been trying a number of things and I have started to think it may
not be possible in the way we are trying it. Maybe we will have to go down
to the abstrack buttons of the internal JList the combo box has.
~ 
 As you can see I got, in a sense, the opposite of what I need; which is for
the already selected item on the top on the combobox to have a dark color
background and a white foreground (as it happens in most window bars). I am
getting what I want but in the internal popup window the combo box has:
~ 
package scratch;

import java.util.*;
import java.awt.*;
import java.awt.font.*;

import javax.swing.*;
import javax.swing.plaf.*;
import javax.swing.plaf.basic.*;
import javax.swing.plaf.metal.*;

import java.io.*;

import javax.swing.text.*;
import javax.swing.event.*;
import javax.swing.border.*;

// __ 
public class Test06{
 private static final ColorUIResource ClrUIRsrcBLUE = new
ColorUIResource(Color.BLUE);
 private static final ColorUIResource ClrUIRsrcWHITE = new
ColorUIResource(Color.WHITE);

// __ 
 public static void main(String[] ss) throws Exception{
  UIManager.put("ComboBoxUI", "scratch.Test06$CustomMetalComboBoxUI");

// __ 
  Color Clr = (new Color(57, 133, 217));
  Clr = (new Color(238, 238, 238));

// __ 
  Font fo = new Font("Sans Serif", Font.PLAIN, 14);
  UIManager.put("ComboBox.font", new FontUIResource(fo));
// __ 
  Font BFnt = fo.deriveFont(Font.BOLD);
  FontUIResource FUIRsrc = new FontUIResource(BFnt);
// __ 
  UIManager.put("ComboBox.background", Color.WHITE);
  UIManager.put("ComboBox.selectionBackground", Clr);
  UIManager.put("ComboBox.selectionForeground", Color.WHITE);
  UIManager.put("ComboBox.popupSelectedFont", FUIRsrc);
  UIManager.put("ComboBox.popupSelectedBackground", ClrUIRsrcBLUE); // Color
that prev selected item takes after you hover over other ones
// __ 
  EventQueue.invokeLater( new Runnable(){
   public void run(){
    JComboBox jcb = new JComboBox(new Object[
{"One", "Two", "Three", "Four", "Five", "Six"});

    System.out.println(jcb.getUI().getClass().toString());

    jcb.setBorder( BorderFactory.createEmptyBorder(40, 60, 40, 60));

     JFrame f = new JFrame();
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     f.getContentPane().add(jcb, BorderLayout.CENTER);

     f.pack();
     f.setLocationRelativeTo(null);

     f.setVisible(true);
    }
   });
  }
// __ 
 public static class CustomMetalComboBoxUI extends MetalComboBoxUI{
  public static ComponentUI createUI(JComponent c){ return new
CustomMetalComboBoxUI(); }
@Override
  protected ListCellRenderer createRenderer(){ return new
CustomComboBoxRenderer(); }
// __ 
  private class CustomComboBoxRenderer extends BasicComboBoxRenderer{

   final Font selectedFont =
UIManager.getFont("ComboBox.popupSelectedFont");

   final Color selectedBackground =
UIManager.getColor("ComboBox.popupSelectedBackground");

   Map<TextAttribute, ?> MTxtAttrb = selectedFont.getAttributes();

// __ 
@Override
   public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus){
    Component c = super.getListCellRendererComponent(list, value, index,
isSelected, cellHasFocus);
    if(index == CustomMetalComboBoxUI.this.comboBox.getSelectedIndex()){
     if( selectedFont != null){ 

      c.setFont(selectedFont);
      c.setBackground(ClrUIRsrcBLUE);
      c.setForeground(ClrUIRsrcWHITE);

     }
     if( selectedBackground != null && !isSelected){ c.setBackground(
selectedBackground); }
    }
    return c;
   }
  }// CustomComboBoxRenderer
 }// CustomMetalComboBoxUI
}
~ 
>> 
>> Mind the package directory structure.
> 
> You must also name the class as I did or make the according
> modifications to the code.
> 
~ 
Also could you clarify to me why doesn't

package scratch;

. . .

import scratch.*;
import scratch.Test06.*;

. . .
  UIManager.put("ComboBoxUI", "CustomMetalComboBoxUI");
. . .

work?

 Thanks
 lbrtchx

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#3714

From"Daniele Futtorovic" <daniele.futtorovic@THRWHITE.remove-dii-this>
Date2011-04-27 15:46 +0000
Message-ID<g3utbv$lpn$1@registered.motzarella.org>
In reply to#3713
  To: comp.lang.java.gui
On 2008-06-25 13:07 +0100, Albretch Mueller allegedly wrote:
>  Hi Daniele et al,
> 
>  I have been trying a number of things and I have started to think it may
> not be possible in the way we are trying it. Maybe we will have to go down
> to the abstrack buttons of the internal JList the combo box has.
> ~ 
>  As you can see I got, in a sense, the opposite of what I need; which is for
> the already selected item on the top on the combobox to have a dark color
> background and a white foreground (as it happens in most window bars). I am
> getting what I want but in the internal popup window the combo box has:

Do you use an IDE? If you do (and you probably should), I strongly
suggest you have a look at the library source code (of Swing, in this
case). With Netbeans, for instance, I right-click on any method or field
name and select "Go To" -> "Source" and it opens the relevant class. Why
am I telling you this? Because for your current problem, I simply opened
the source code for javax.swing.plaf.basic.BasicComboBoxUI and
javax.swing.plaf.metal.MetalComboBoxUI, looked how they did things, and
thence knew which parts I could and should interfere with. I suggest you
do the same. The library source code is, in my humble opinion, the most
valuable well of insight.

Here, I simply copy/pasted the method BasicComboBoxUI#paintCurrentValue,
and made a few modifications (no other way to do what you want to
achieve, I'm afraid).

<sscce>
package scratch;

import java.util.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.*;
import javax.swing.plaf.basic.*;
import javax.swing.plaf.metal.*;

public class Test {

     public static void main(String[] ss) throws Exception {

         UIManager.put("ComboBoxUI", "scratch.Test$CustomMetalComboBoxUI");

         UIManager.put("ComboBox.editorBackground", new 
ColorUIResource(0xfff0c0c0));
         UIManager.put("ComboBox.editorForeground", new ColorUIResource(0));


         EventQueue.invokeLater( new Runnable(){
             public void run(){
                 JComboBox jcb = new JComboBox(new Object[]{"One", 
"Two", "Three", "Four", "Five", "Six"});

                 System.out.println(jcb.getUI().getClass().toString());

                 jcb.setBorder( BorderFactory.createEmptyBorder(40, 60, 
40, 60) );

                 JFrame f = new JFrame();
                 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                 f.getContentPane().add(jcb, BorderLayout.CENTER);

                 f.pack();
                 f.setLocationRelativeTo(null);

                 f.setVisible(true);
             }
         });
     }

     public static class CustomMetalComboBoxUI
     extends MetalComboBoxUI
     {
         public static ComponentUI createUI(JComponent c) {
             return new CustomMetalComboBoxUI();
         }

         @Override
         public void paintCurrentValue(Graphics g,Rectangle 
bounds,boolean hasFocus) {
             ListCellRenderer renderer = comboBox.getRenderer();
             Component c;

             if ( hasFocus && !isPopupVisible(comboBox) ) {
                 c = renderer.getListCellRendererComponent( listBox,
 
comboBox.getSelectedItem(),
                                                            -1,
                                                            true,
                                                            false );
             }
             else {
                 c = renderer.getListCellRendererComponent( listBox,
 
comboBox.getSelectedItem(),
                                                            -1,
                                                            false,
                                                            false );
                 c.setBackground(UIManager.getColor("ComboBox.background"));
             }
             c.setFont(comboBox.getFont());
             if ( hasFocus && !isPopupVisible(comboBox) ) {
//                c.setForeground(listBox.getSelectionForeground());
//                c.setBackground(listBox.getSelectionBackground());
                 c.setForeground( 
UIManager.getColor("ComboBox.editorForeground") );
                 c.setBackground( 
UIManager.getColor("ComboBox.editorBackground") );
             }
             else {
                 if ( comboBox.isEnabled() ) {
                     c.setForeground(comboBox.getForeground());
                     c.setBackground(comboBox.getBackground());
                 }
                 else {
                     c.setForeground(sun.swing.DefaultLookup.getColor(
                              comboBox, this, 
"ComboBox.disabledForeground", null));
                     c.setBackground(sun.swing.DefaultLookup.getColor(
                              comboBox, this, 
"ComboBox.disabledBackground", null));
                 }
             }

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

             currentValuePane.paintComponent(g,c,comboBox,bounds.x,bounds.y,
                                             bounds.width,bounds.height, 
shouldValidate);
         }
     }
}
</sscce>

(Note: you'll get two warnings when trying to compile this, because of
the two calls to sun.swing.DefaultLookup. I wouldn't bother about those
for the time being, but keep them in mind, as they may create problems
in future releases)

>>> Mind the package directory structure.
>> You must also name the class as I did or make the according
>> modifications to the code.
>>
> ~ 
> Also could you clarify to me why doesn't
> 
> package scratch;
> 
> .. . .
> 
> import scratch.*;
> import scratch.Test06.*;
> 
> .. . .
>   UIManager.put("ComboBoxUI", "CustomMetalComboBoxUI");
> .. . .
> 
> work?

Because, as I told you, the UIManager loads the UI delegate
*reflectively*. In other words, it makes a call to Class.forName()
(essentially) with that String. So the String you put as the value must
be on the classpath. If the class "CustomMetalComboBoxUI" were not an
inner class, as I wrote it, but a class in the same package as my "Test"
class, the String ought to be "scratch.CustomMetalComboBoxUI". If it
were in another package, say package x.y.z, the String ought to be
"x.y.z.CustomMetalComboBoxUI". And so on. Google for "Java swing custom
UI delegate".

-- 
DF.
to reply privately, change the top-level domain
in the FROM address from "invalid" to "net"

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.gui


csiph-web