How do you edit a JComboBox dropdown arrow? - java

How do you remove the background of a JComboBox dropdown arrow, leaving only the small triangle? The dropdown list was created using the JComboBox tool in Netbeans version 8.

#Drew I don't think the OP wants the button to disappear - he just wants the button to have no visible manifestation other than the arrow triangle:
JComboBox<String> box = new JComboBox<>();
final Color bg = box.getBackground();
box.setUI(new BasicComboBoxUI() {
#Override
protected JButton createArrowButton() {
JButton b = super.createArrowButton();
b.setBackground(bg);
b.setBorderPainted(false);
return b;
}
});

Related

Changing button color after click and resetting it Java

public class ButtonsActionListener implements ActionListener {
String[] buttons = { "Button1", "Button2", "Button3", "Button4"};
for (String btn: buttons ) {
JButton button = new JButton(btn);
this.add(button);
button.addActionListener(this);
}
}
#Override
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton)e.getSource();
btn.setBackground(Color.Red);
}
}
What Im trying to do is when the user clicks on for example Button1, Button1's color should turn to grey and when I click on BUtton3, Button1 color should go back to normal and Button3 should turn grey. I dont know how to check the previous click
Your actionPerformed function changes the background of what is currently clicked, so it will not allow you to change other JButton objects without a very rudimentary condition. You should store all of your buttons as unique variables for such cases.

Check if JButtons are within a particular JPanel

I am creating a GUI application using Java. There are different sections in the main frame, each having a particular functionality. The header panel contains the buttons Submit, Undo, Shuffle, Help and Quit. Here is the sample code:
JPanel header = new JPanel();
JButton submit = new JButton("Submit");
header.add(submit);
JButton undo= new JButton("Undo");
header.add(undo);
JButton shuffle= new JButton("Shuffle");
header.add(shuffle);
JButton help= new JButton("Help");
header.add(help);
JButton quit = new JButton("Quit");
header.add(quit);
Further down my code, I need to check that the button clicked is not in the header panel (there are buttons in other panels too).
public void actionPerformed(ActionEvent e)
{
String clicked = e.getActionCommand();
if(!clicked.equals("Submit") && !clicked.equals("Undo") && !clicked.equals("Help")&& !clicked.equals("Quit") && !clicked.equals("Shuffle")){
//some code here
Is there any alternate neater way to check that the button clicked is not in the header panel? I will need to do something similar in another panel which contains more buttons. Using IF statements to check for each button is inefficient and untidy, I believe.
Honestly, I'd just implement actions for every button. It may require more code but it's a better solution. But if you insist to go this way you can try this
public void actionPerformed(ActionEvent e) {
if (!Arrays.asList(PANEL.getComponents()).stream().filter(b -> b instanceof JButton)
.map(b -> (JButton) b).filter(b ->
b.getText().equals(e.getActionCommand())).findFirst().isPresent()) {
// execute code if button is not a child of PANEL
}
}
You can use addActionListener for every button separately.

Defining/Setting the color of JComboBox BasicArrowButton in case of disabling

I didn't like the standard arrow button of the JComboBox, because it didn't fit well in my GUI. So I changed it. To do this, I wrote a class, which is extending BasicComboUI. In the regular case, everything is like I am expecting it. But as soon as I am disabling the button, it stays the way it is, which means that the button doesn't get the grey background color anymore. Instead, the left part of the list does. So I would like to know, if there is a way to define or to modify the disabling-behavior of the arrow button.
Here is my current code:
public class CustomArrowUI extends BasicComboBoxUI{
private static Color buttonBackground;
private static Color borderBox;
private static Color arrowColor;
private static Color buttonBorder;
public static ComboBoxUI createUI(JComponent c, Color buttonBackground, Color borderBox, Color arrowColor, Color buttonBorder)
{
CustomArrowUI.buttonBackground = buttonBackground;
CustomArrowUI.borderBox = borderBox;
CustomArrowUI.arrowColor = arrowColor;
CustomArrowUI.buttonBorder = buttonBorder;
return new CustomArrowUI();
}
#Override
protected JButton createArrowButton()
{
JButton button = new BasicArrowButton(BasicArrowButton.SOUTH, buttonBackground, borderBox, arrowColor, buttonBorder);
LineBorder border = new LineBorder(buttonBorder, 1);
button.setEnabled(false);
button.setBorder(border);
return button;
}}
What I could do is setting the background light grey as default, and change the color in the ActionListener of the previous button, which is enabling my JComboBox. But I kinda don't like this solution. I would prefer to do it directly in my CustomArrowUI
I found one short way to solve this. I added a ChangeListener to my arrow button, which is checking if the button is enabled or not, and coloring the button:
#Override
protected JButton createArrowButton()
{
final JButton button = new BasicArrowButton(BasicArrowButton.SOUTH, buttonBackground, borderBox, arrowColor, buttonBorder);
LineBorder border = new LineBorder(buttonBorder, 1);
button.setEnabled(false);
button.setBorder(border);
button.addChangeListener(new ChangeListener(){
#Override
public void stateChanged(ChangeEvent arg0) {
if(button.isEnabled())
button.setBackground(Color.WHITE);
else
{
button.setBackground(ColorPalette.LIGHT_GREY);
button.setBorder(new LineBorder(ColorPalette.LIGHT_GREY, 1));
}
}
});
return button;
}
Above I changed the background of the button and the border color too make it look like the arrow is part of the JComboBox, and not like a separate button inside of it.
Since the disabling-behavior is inheritet from the JComboBox itself (I guess), I don't need to take care if the button is usable or not. All I need to define is the color.
Another important point is to make sure, to call the setEnabled()-method after setting the UI of the JComboBox, if the JComboBox should be disabled by default. Otherwise it will not react initially, and the arrow button will look enabled.

Java Radio button clear/deselect

I am trying to deselect JRadio button while clear button is pressed. I have tried googling and gone thru lots of forum the only fix i could find is create an invisible button and while clear button is pressed select invisible one. Is there any other method i can use? My code is as follows
public class deselectRadioBtn extends JFrame {
private JRadioButton[] buttons; // array for JRadio buttons
public deselectRadioBtn() {
super("Deselect Radio");
for (int nbrOfButtons = 0; nbrOfButtons < options.length; nbrOfButtons++) {
//create new JRadioButtons and labels and add ( ) around label
buttons[nbrOfButtons] = new JRadioButton(( nbrOfButtons + radioLabel[nbrOfButtons] ));
//add buttons to eastPanel
rightPanel.add(options[nbrOfButtons]);
}//end for (JRadio)
//Create a ButtonGroup object, add buttons to the group
ButtonGroup optionSelect = new ButtonGroup();
optionSelect.add(buttons[0]);
optionSelect.add(buttons[1]);
optionSelect.add(buttons[2]);
optionSelect.add(buttons[3]);
}
This is just a piece of code and i havent include full code.
Use ButtonGroup.clearSelection().

Show the clicking visual effect of a JButton inside a JComboBox

I have created a JComboBox with a custom ListCellRenderer (a JButton) as below:
JButton b1 = new JButton("One");
JButton b2 = new JButton("Two");
JButton b3 = new JButton("Three");
JButton[] buttonList = {b1, b2, b3};
JComboBox box = new JComboBox(buttonList);
box.setRenderer(new CellRenderer());
//...
/**************** Custom Cell Renderer Class ****************/
class CellRenderer implements ListCellRenderer {
public Component getListCellRendererComponent(JList list, Object value, int index,
boolean isSelected, boolean cellHasFocus) {
JButton button = (JButton) value;
return button;
}
I have set button actions separately and all are works fine except when I am clicking
a button from the combo box it doesn't shows the button clicking visual effect.
How to show the clicking visual effect of a JButton inside a JComboBox?
I think I found the reason for not showing the visual clicking effects inside a JComboBox From this --> Stackoverflow question

Categories

Resources