jamie_lynn
asked on
In JComboBox, how do i control the space between the dropdown and the dropdown icon?
Hi,
In JComboBox, how do i control the space between the dropdown and the dropdown icon?
Using a renderer does not work, as it just elongates the dropdown. I need to control the space in between the dropdown component and the button.
If you run the code below, you will see a dropdown in a panel, with "V" denoting the dropdown icon
and "|| ||" representing the dropdown
||Rabbit || V
I want to make the dropdown look like
||Rabbit || V
I think I have to modify the MetalComboBoxUI or BasicComboBoxUI, but I'm not sure how.
Thanks
Jamie
In JComboBox, how do i control the space between the dropdown and the dropdown icon?
Using a renderer does not work, as it just elongates the dropdown. I need to control the space in between the dropdown component and the button.
If you run the code below, you will see a dropdown in a panel, with "V" denoting the dropdown icon
and "|| ||" representing the dropdown
||Rabbit || V
I want to make the dropdown look like
||Rabbit || V
I think I have to modify the MetalComboBoxUI or BasicComboBoxUI, but I'm not sure how.
Thanks
Jamie
import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ComboBoxSpace extends JPanel {
JComboBox petList;
public ComboBoxSpace() {
String[] petStrings = new String[5];
petStrings[0] = "Bird";
petStrings[1] = "Cat";
petStrings[2] = "Dog";
petStrings[3] = "Rabbit";
petStrings[4] = "Pig";
// Create the combo box
petList = new MyJComboBox(petStrings);
petList.setEditable(true);
petList.setSelectedIndex(3);
// Layout the demo
setLayout(new BorderLayout());
add(petList, BorderLayout.NORTH);
setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
}
public static class MyJComboBox extends JComboBox {
public MyJComboBox (String[] s) {
super(s);
}
}
public static void main(String s[]) {
JFrame frame = new JFrame("ComboBoxSpace");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
frame.getContentPane().add(new ComboBoxSpace(), BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}
Try overriding http://java.sun.com/javase/6/docs/api/javax/swing/plaf/basic/BasicComboBoxUI.html#createArrowButton() and set a wide enough left border on the button
The following code will display a wide button. You have to create the widebuttton.png which is empty on the left side and has a drop down triangle on the right side.
The button will be as wide as the png.
import javax.swing.*;
import javax.swing.plaf.metal.MetalComboBoxUI;
import javax.swing.plaf.metal.MetalComboBoxButton;
import java.util.Vector;
import java.awt.*;
public class WideJComboButton extends JFrame {
public WideJComboButton() {
Vector v = new Vector();
v.add("A simple ");
v.add("wider button ");
v.add("example");
JComboBox combo = new WideButtonComboBox(v);
add(combo);
pack();
setVisible(true);
}
public static void main(String[] args) {
new WideJComboButton();
}
public class WideButtonComboBox extends JComboBox {
public WideButtonComboBox(Vector c) {
super(c);
setUI(new MyComboUI());
}
public class MyComboUI extends MetalComboBoxUI {
protected JButton createArrowButton() {
JButton button = new MetalComboBoxButton(comboBox, new ImageIcon("widebuttton.png"), currentValuePane, listBox);
button.setMargin(new Insets(0, 1, 1, 3));
return button;
}
}
}
}
ASKER
Adding more space is not what I want, since it will look like
||Rabbit || V
instead of
||Rabbit || V
Adding space inside the button does not work either, since the button is wide, and the user can see the wide button when they click on the button.
CEHJ,
Could you attach a code snippet?
Thanks
Jamie
||Rabbit || V
instead of
||Rabbit || V
Adding space inside the button does not work either, since the button is wide, and the user can see the wide button when they click on the button.
CEHJ,
Could you attach a code snippet?
Thanks
Jamie
Jamie, pls try to explain again what it is that you want, since I am not sure now. This is what you stated in your question:
--------------------------
I want to make the dropdown look like
||Rabbit || V
--------------------------
In the above picture the right side represents the button, is that correct? If this is correct then the solution I posted will work.
What confuses me is your last post, where you say:
"....since the button is wide, and the user can see the wide button when they click on the button."
I dont understand it, because you are saying that you want to have a wide button.
ASKER
Hi dejanpazin,
I want to keep the button the same size, but put more spaces between the dropdown and the dropdown button.
| V | will represent the button
|| Rabbit || will represent the dropdown
I want the combobox to look like below where there are spaces in between
|| Rabbit || | v |
I ran your code snippet, and it shows, space in the button, but not between
|| Rabbit || | v |
Thanks
Jamie
I want to keep the button the same size, but put more spaces between the dropdown and the dropdown button.
| V | will represent the button
|| Rabbit || will represent the dropdown
I want the combobox to look like below where there are spaces in between
|| Rabbit || | v |
I ran your code snippet, and it shows, space in the button, but not between
|| Rabbit || | v |
Thanks
Jamie
Ok. I think I now know what you want. Try the code below.
import javax.swing.*;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
import java.util.Vector;
import java.awt.*;
public class WideJComboButton extends JFrame {
public WideJComboButton() {
Vector v = new Vector();
v.add("A simple ");
v.add("empty space ");
v.add("example");
//JComboBox combo = new WideButtonComboBox(v);
JComboBox combo = new JComboBox(v);
combo.setRenderer(new MyRenderer());
System.out.println("combo.getRenderer() = " + combo.getRenderer().getClass());
add(combo);
pack();
setVisible(true);
}
public static void main(String[] args) {
new WideJComboButton();
}
private class MyRenderer extends BasicComboBoxRenderer {
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
Component component = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
JLabel label = new JLabel();
label.setOpaque(true);
label.setBackground(Color.white);
JPanel panel = new JPanel(new BorderLayout());
panel.add(component, BorderLayout.WEST);
panel.add(label);
if (!isSelected){
component.setBackground(Color.white);
}
return panel;
}
}
}
ASKER
Hi dejanpazin:,
Using a renderer above gives me, with different colors
|| Rabbit || | v |
But I need to put space between the dropdown and the dropdown image
|| Rabbit || | v |
The reason why i am asking for this is that the dropdown is not a list for me, it is an complex component. I am just using a list as an example to make it simpler.
Thanks
Jamie
Using a renderer above gives me, with different colors
|| Rabbit || | v |
But I need to put space between the dropdown and the dropdown image
|| Rabbit || | v |
The reason why i am asking for this is that the dropdown is not a list for me, it is an complex component. I am just using a list as an example to make it simpler.
Thanks
Jamie
OK. In the following solution the actual button is not displayed, but there is one added next to the combobox. The space in between is controlled with the width of the Test.png.
This will display as
|| Rabbit || | v |
The | v | is an ordinary button, you can create one witch matches the combobox button.
import javax.swing.*;
import javax.swing.plaf.metal.*;
import java.util.Vector;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class WideJComboButton extends JFrame {
public WideJComboButton() {
Vector v = new Vector();
v.add("Rabbit");
v.add("more rabbit");
v.add("some more");
final WideButtonComboBox combo = new WideButtonComboBox(v);
setLayout(new FlowLayout());
add(combo);
JButton fakeButton = new JButton("V");
fakeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
combo.doClick();
}
});
add(fakeButton);
pack();
setVisible(true);
}
public static void main(String[] args) {
new WideJComboButton();
}
public class WideButtonComboBox extends JComboBox {
public WideButtonComboBox(Vector c) {
super(c);
setUI(new MyComboUI());
}
public void doClick() {
this.showPopup();
}
public class MyComboUI extends MetalComboBoxUI {
protected JButton createArrowButton() {
JButton button = new MetalComboBoxButton(comboBox,
new ImageIcon("Test.png"),
false,
currentValuePane,
listBox);
button.setMargin(new Insets(0, 1, 1, 3));
return button;
}
protected void installComponents() {
arrowButton = createArrowButton();
if (arrowButton != null) {
configureArrowButton();
}
if (comboBox.isEditable()) {
addEditor();
}
comboBox.add(currentValuePane, BorderLayout.WEST);
}
}
}
}
ASKER
Looks great!
One minor thing. How do I close the popup when clicking on the fakebutton?
It seems like isPopupVisible() always return false
Thanks
Jamie
One minor thing. How do I close the popup when clicking on the fakebutton?
It seems like isPopupVisible() always return false
Thanks
Jamie
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Works great!
petStrings[3] = "Rabbit ";