This purpose is mainly aesthetic, i´ve already done somthing similar on JTextArea, but i can´t figure it out or get access to the vertical scrollbar on the popup from the JComboBox. I´ve removed the arrow that displays on top by setting it´s witdh to 0.
setUI(new BasicComboBoxUI(){
protected JButton createArrowButton(){
return new JButton(){
@Override public int getWidth() {
return 0;
}
@Override
public void setFocusable(boolean focusable) {
super.setFocusable(false);
}
};
}
});
Here´s a JTextArea inside of a JScrollPane with no arrows and a thinner bar
getVerticalScrollBar().setPreferredSize(new Dimension(10, 0));
getVerticalScrollBar().setUI(new BasicScrollBarUI(){
@Override
protected JButton createDecreaseButton(int orientation) {
return createZeroButton();
}
@Override
protected JButton createIncreaseButton(int orientation) {
return createZeroButton();
}
private JButton createZeroButton() {
JButton jbutton = new JButton();
jbutton.setPreferredSize(new Dimension(0, 0));
jbutton.setMinimumSize(new Dimension(0, 0));
jbutton.setMaximumSize(new Dimension(0, 0));
return jbutton;
}
});
You could override the getPopup()
method of the BasicComboBoxUI
in order to create a new BasicComboPopup
. Then you have access to the createScroller()
method in which, by overriding it, you can just return your own customized JScrollPane
.
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.plaf.basic.BasicComboBoxUI;
import javax.swing.plaf.basic.BasicComboPopup;
import javax.swing.plaf.basic.BasicScrollBarUI;
import javax.swing.plaf.basic.ComboPopup;
public class Example {
public Example() {
JComboBox<Integer> comboBox = new JComboBox<Integer>();
for (int i = 1; i <= 10; i++) {
comboBox.addItem(i);
}
comboBox.setUI(new BasicComboBoxUI() {
@Override
protected ComboPopup createPopup() {
return new BasicComboPopup(comboBox) {
@Override
protected JScrollPane createScroller() {
JScrollPane scroller = new JScrollPane(list, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scroller.getVerticalScrollBar().setUI(new BasicScrollBarUI() {
@Override
protected JButton createDecreaseButton(int orientation) {
return createZeroButton();
}
@Override
protected JButton createIncreaseButton(int orientation) {
return createZeroButton();
}
@Override
public Dimension getPreferredSize(JComponent c) {
return new Dimension(10, super.getPreferredSize(c).height);
}
private JButton createZeroButton() {
return new JButton() {
@Override
public Dimension getMinimumSize() {
return new Dimension(new Dimension(0, 0));
}
@Override
public Dimension getPreferredSize() {
return new Dimension(new Dimension(0, 0));
}
@Override
public Dimension getMaximumSize() {
return new Dimension(new Dimension(0, 0));
}
};
}
});
return scroller;
}
};
}
});
JPanel mainPanel = new JPanel();
mainPanel.add(comboBox);
JFrame frame = new JFrame("Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Example();
}
});
}
}