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


Groups > comp.lang.java.gui > #3706

Re: cobbling a ComboBox

From "Daniele Futtorovic" <daniele.futtorovic@THRWHITE.remove-dii-this>
Subject Re: cobbling a ComboBox
Message-ID <g3reau$avf$1@registered.motzarella.org> (permalink)
Newsgroups comp.lang.java.gui
References <1214321081.136206@nntp.acecape.com>
Date 2011-04-27 15:46 +0000
Organization TDS.net

Show all headers | View raw


  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

Back to comp.lang.java.gui | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web