Show the clicking visual effect of a JButton inside a JComboBox - java

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

Related

How can I get the ImageIcon displayed in a JButton?

public ImageIcon getIcon(){
return button.getIcon();
}
public void moveTo(Square j){
JButton current = new JButton();
current = button;
button.setIcon(j.getIcon());
button.setIcon(current.getIcon());
}
Here's the problem. Button is just a simple JButton without content, and with conditions I'm adding different content into Button. Though when I created the move method, which will switch the two images of two buttons, I failed to attempt to get the ImageIcon of the JButton. It reports this error in the first method:
Icon cannot be converted to ImageIcon
How can I solve that?
Icon is an interface which is implemented by the class ImageIcon so inherently every ImageIcon is an Icon so you can cast the Icon to imageIcon as follows
public ImageIcon getIcon(){
return (ImageIcon)button.getIcon();
/*getIcon() returns Icon and ImageIcon is child
of Icon so you can cast as you cast parent class reference to child class in this
case it is interface acting as parent*/
}
You can add it through the constructor:
JButton button = new JButton("text", icon);
The better way is to create an action and create the button from the action. Then you can update the icon of the action and it will be udpated everywhere that action is used: the button, menu bars, popup menus, etc.
Action action = new AbstractAction("text", icon) {
public void actionPerformed(ActionEvent e) {
}
}
//place the action in an ActionMap
Jbutton button = new JButton(action);
Then when you need to update the icon it would just be
getActionMap().get(key).putValue(Action.LARGE_ICON_KEY, icon); //for buttons
getActionMap().get(key).putValue(Action.SMALL_ICON_KEY, icon); //for menus

JButton action is not performed inside of JTable

I got a simple JTable which one of it's cells is an Edit button which needs to open a new JDialog.
After a several examples from the web I created a new class which implements TableCellRenderer and returned a JButton from it but the button is not doing what I need for some reason.
This is my code:
final MessageResourcesTableModel model = new MessageResourcesTableModel(columnNames);
JTable resultsTable = new JTable(model);
resultsTable.setShowGrid(true);
resultsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
resultsTable.setPreferredScrollableViewportSize(resultsTable.getPreferredSize());
resultsTable.setFillsViewportHeight(true);
resultsTable.setFont(font13);
final TableCellRenderer buttonRenderer = new JTableButtonRenderer();
resultsTable.getColumn(COLUMN_EDIT).setCellRenderer(buttonRenderer);
And this is the renderer:
class JTableButtonRenderer implements TableCellRenderer {
#Override public Component getTableCellRendererComponent(final JTable table, final Object value, final boolean isSelected, final boolean hasFocus, final int row, final int column) {
final JButton editButton = new JButton("Edit");
editButton.setOpaque(true);
editButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(final ActionEvent e) {
System.out.println("Hi from Action !!!");
}
});
return editButton;
}
}
Although there is a simple ActionListener implementation, there is nothing over the console
A renderer only displays the data in the cell. You can't add an ActionListener to the button because it is not a real component. A renderer only paints an image of the button in the table.
You need to create a custom editor if you want to be able to click on the button.
See Table Button Column for an editor implementation that does this for you.

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.

Assigning JButton to any location

I have added my button to my Container which can hold components, everything works fine however, the clearButton.setLocation() method below is not working. any ideas?
public JButton clearButton(){ // clearButton method
JButton clearButton = new JButton("CLEAR"); // new button of type JButton
clearButton.setPreferredSize(new Dimension(200,30)); // dimension of the button
clearButton.setLocation(400,450);
return clearButton; // return reference to the JButton object
}
You have to set a absolute layout to the JPanel where you are adding the button to. Then it should work.

How do you edit a JComboBox dropdown arrow?

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;
}
});

Categories

Resources