1

I have the following problem :

I need to set a custom UI for a JComboBoxComponent (to modify colors, arrow button etc.) Currently, I'm doing it in a constructor, like this :

public MyComboBox() {
   setUI(new MyComboBoxUI);
}

Problem is, after setting UI in such way, I somehow loose all InputMap and ActionMap contents for list in combo box popup, i.e. it doesn't scroll list up or down with arrow keys.

What am I doing wrong here?

Here's the code :

public class CurrencyPairComboBox extends JComboBox { 

    public CurrencyPairComboBox() {
        setUI(new CurrencyPairComboBoxUI());
    }
}

class CurrencyPairComboBoxUI extends BasicComboBoxUI {

    @Override
    public void installUI(JComponent c) {
       super.installUI(c);

       listBox.setSelectionBackground(Color.BLACK);
       listBox.setSelectionForeground(Color.WHITE);
    }

    @Override
    protected JButton createArrowButton() {
       arrowButton = new JButton();
       arrowButton.setIcon(OrderWidgetUIConstants.DROPDOWN_ARROW_ICON);
       arrowButton.setRolloverIcon(OrderWidgetUIConstants.DROPDOWN_ARROW_HOVER_ICON);
       return arrowButton;
    }    
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
S.R.
  • 21
  • 2
  • For better help sooner, post an [SSCCE](http://pscode.org/sscce.html) of your current code. Also, please use code formatting for code. I've edited your post to use it for the snippet. – Andrew Thompson Oct 19 '11 at 12:16
  • could you please provide source for you MyComboBoxUI class? – Wojciech Owczarczyk Oct 19 '11 at 12:17
  • there I can't see any code, only setUI for un_know MyComboBoxUI, are you kidding – mKorbel Oct 19 '11 at 12:21
  • 2
    'I need to set a custom UI' - no, you only _think_ you so :-) Generally it's a bad idea. If it turns out you really need it, it's a _lot_ of work to get right .. – kleopatra Oct 19 '11 at 14:59

1 Answers1

3

I tried code that you posted here, I didn't see any Keyboard issue(s), all works as I expected

enter image description here enter image description here

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

class ComboBoxTest extends JFrame {

    private static final long serialVersionUID = 1L;
    private JComboBox comboBox;
    private ImageIcon infoIcon = (ImageIcon) UIManager.getIcon("OptionPane.informationIcon");
    private ImageIcon warnIcon = (ImageIcon) UIManager.getIcon("OptionPane.warningIcon");

    ComboBoxTest() {
        String[] items = {"Item1", "Item2"};
        comboBox = new JComboBox(items);
        Container c = getContentPane();
        c.setLayout(new FlowLayout());
        c.add(comboBox);
        comboBox.setUI(new MyUI());
    }

    public JFrame getCurrentInstance() {
        return this;
    }

    public static void main(String[] args) {
        ComboBoxTest frame = new ComboBoxTest();
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);
    }

    class MyUI extends javax.swing.plaf.basic.BasicComboBoxUI {

        @Override
        protected JButton createArrowButton() {
            JButton btn = new JButton();
            btn.setIcon(infoIcon);
            btn.setRolloverIcon(warnIcon);
            return btn;
        }
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • i don't think it's connected with that – S.R. Oct 19 '11 at 12:41
  • @S.R. I talking about methods that I seen in code that you posted here, – mKorbel Oct 19 '11 at 12:47
  • at my application startup `SynthLookAndFeel` is set as default L&F, to make all UI components look the same. If I need to customize a certain component, I would need to either describe it in XML file, or provide a custom UI for it. – S.R. Oct 19 '11 at 13:15
  • @S.R. nothing easier, only to implement http://stackoverflow.com/questions/3954616/look-and-feel-in-java, maybe to look for Substance for excelent and todays output to the GUI, notice but is too sensitive for EDT – mKorbel Oct 19 '11 at 13:24