Path: csiph.com!x330-a1.tempe.blueboxinc.net!feeder1.hal-mli.net!news.glorb.com!news-out.readnews.com!news-xxxfer.readnews.com!news-out.news.tds.net!newsreading01.news.tds.net!86597e80!not-for-mail From: "Albretch Mueller" Subject: Re: cobbling a ComboBox Message-ID: <1214435196.953231@nntp.acecape.com> X-Comment-To: comp.lang.java.gui Newsgroups: comp.lang.java.gui In-Reply-To: References: Content-Type: text/plain; charset=IBM437 Content-Transfer-Encoding: 8bit X-Gateway: time.synchro.net [Synchronet 3.15a-Win32 NewsLink 1.92] Lines: 149 Date: Wed, 27 Apr 2011 15:46:29 GMT NNTP-Posting-Host: 96.60.20.240 X-Complaints-To: news@tds.net X-Trace: newsreading01.news.tds.net 1303919189 96.60.20.240 (Wed, 27 Apr 2011 10:46:29 CDT) NNTP-Posting-Date: Wed, 27 Apr 2011 10:46:29 CDT Organization: TDS.net Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.gui:3713 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 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