Attach action Event on JComboBox arrow JButton - java

I try to attach action Events on the JCombobox arrow JButton.
So I make a custom ComboBoxUI:
public class CustomBasicComboBoxUI extends BasicComboBoxUI {
public static CustomBasicComboBoxUI createUI(JComponent c) {
return new CustomBasicComboBoxUI ();
}
#Override
protected JButton createArrowButton() {
JButton button=super.createArrowButton();
if(button!=null) {
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// arrow button clicked
}
});
}
return button;
}
}
The problem with this is that the look of combobox is different, seem to be an old look.
Why? I only add a listener to the same arrow button...
Thank.

Perhaps the problem is due to your expecting a JComboBox isn't a BasicComboBoxUI but one of another look and feel, perhaps a MetalComboBoxUI.
Rather than create a new CustomBasicComboBoxUI object, could you extract the JButton component from an existing JComboBox object? i.e.,
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class ComboBoxArrowListener {
private static void createAndShowUI() {
String[] data = {"One", "Two", "Three"};
JComboBox combo = new JComboBox(data);
JPanel panel = new JPanel();
panel.add(combo);
JButton arrowBtn = getButtonSubComponent(combo);
if (arrowBtn != null) {
arrowBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("arrow button pressed");
}
});
}
JFrame frame = new JFrame("ComboBoxArrowListener");
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static JButton getButtonSubComponent(Container container) {
if (container instanceof JButton) {
return (JButton) container;
} else {
Component[] components = container.getComponents();
for (Component component : components) {
if (component instanceof Container) {
return getButtonSubComponent((Container)component);
}
}
}
return null;
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}

Related

unselect JCheckBox once re-clicked

I tried a number of other solutions posted on StackExchange but I can't get the JCheckBox to unselect when I click it again [after it's been selected].
I've tried the following and it just stays clicked even when re-clicked:
checkbox2.setSelected(false);
// check state
//if (checkbox2.isSelected()) {
// do something...
checkbox2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
// return str;
//checkbox = (JCheckBox) event.getSource();
if (checkbox2.isSelected()) {
checkbox2.setSelected(false);
experimentalSelection = "ML";
System.out.println(experimentalSelection);
}
}
});
I assumed that if i added checkbox2.setSelected(false); then it would uncheck if it was already selected and then clicked.
The behavior that you describe -- making the JCheckBox unselected when a user clicks on an already selected JCheckBox is the check box's default/innate behavior. You don't have to do anything special for this to occur, and you certainly don't have to (or want to) change its selection state within a listener. For example:
import java.awt.Dimension;
import java.awt.event.ItemEvent;
import javax.swing.*;
public class GuiFun extends JPanel {
private JCheckBox checkbox = new JCheckBox("Foo");
public GuiFun() {
setPreferredSize(new Dimension(300, 150));
add(checkbox);
checkbox.setSelected(false);
checkbox.addItemListener(l -> {
if (l.getStateChange() == ItemEvent.SELECTED) {
System.out.println("CheckBox SELECTED");
} else {
System.out.println("CheckBox UNSELECTED");
}
});
}
private static void createAndShowGui() {
GuiFun mainPanel = new GuiFun();
JFrame frame = new JFrame("Gui Fun");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

How to delete button after clicking on "delete" button?

My program seems to run fine, except for the delete part. Every time I click on the 'delete' button, it deletes itself. So my question is, how would I delete a selected button after I clicked on the "delete" button?
Here is a snippet of my code:
public class DeleteButton extends JFrame implements ActionListener
{
JButton b18a = new JButton("Delete");
JPanel panel = new JPanel();
panel.add(b18);
class ClickListenerTwo implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JButton buttonThatWasClicked = (JButton) e.getSource();
Container parent = buttonThatWasClicked.getParent();
parent.remove(buttonThatWasClicked);
parent.revalidate();
parent.repaint();
}
}
}
ActionListener b18aClicked = new ClickListenerTwo();
b18a.addActionListener(b18aClicked);
P.S - This selected button that I'm talking about is made during run time, so I want to delete it during run time too, if that would be possible. Thanks!
So, assuming that you need to click the "other" button first, you could use an instance field to maintain a reference to the "last" clicked button and then use that when the delete button is clicked
For example...
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JButton lastButton;
public TestPane() {
JPanel grid = new JPanel(new GridLayout(8, 8));
for (int index = 0; index < 8 * 8; index++) {
JButton btn = new JButton(Integer.toString(index + 1));
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
lastButton = btn;
}
});
grid.add(btn);
}
JButton delete = new JButton("Delete");
delete.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (lastButton != null) {
lastButton.getParent().remove(lastButton);
grid.revalidate();
grid.repaint();
}
lastButton = null;
}
});
setLayout(new BorderLayout());
add(grid);
add(delete, BorderLayout.SOUTH);
}
}
}
Personally, a JToggleButton would give a better user experience
You have to find the Component from the Top Frame.
May be this code will help you.
public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
if (source instanceof Component) {
Window w = findWindow((Component) source);
//Find your component and remove it from this window.
} else {
System.out.println("source is not a Component");
}
}
public static Window findWindow(Component c) {
System.out.println(c.getClass().getName());
if (c instanceof Window) {
return (Window) c;
} else if (c instanceof JPopupMenu) {
JPopupMenu pop = (JPopupMenu) c;
return findWindow(pop.getInvoker());
} else {
Container parent = c.getParent();
return parent == null ? null : findWindow(parent);
}
}

How to add ArrayList of JButton on a JFrame?

I am coding a little game in which,in one class(named Brick) which extends JPannel,i have a JButton working as a "Brick" and in another class which extends JFrame i have an ArrayList of Brick named Grid which holds the Bricks.But i don't know how to represent the Bricks of ArrayList of Bricks in the serial form,i.e,one after the anoher,on the JFrame of Grid class.Please give me some suggestions.
EDIT : I also want no space between the two buttons.How can i stick two buttons,sharing boundaries?
You may use the following code snippet to add and array of customized classes of JButton.
You may edit it as per your needs.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* A JList of JButtons.
*/
public class JButtonListDemo implements Runnable
{
private JList jlist;
public static void main(String args[])
{
SwingUtilities.invokeLater(new JButtonListDemo());
}
public void run()
{
Object[] items = new ButtonItem[] {
new ButtonItem("Apple"),
new ButtonItem("Banana"),
new ButtonItem("Carrot"),
new ButtonItem("Date"),
new ButtonItem("Eggplant"),
new ButtonItem("Fig"),
new ButtonItem("Guava"),
};
jlist = new JList(items);
jlist.setCellRenderer(new ButtonListRenderer());
jlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jlist.setVisibleRowCount(5);
jlist.addMouseListener(new MouseAdapter()
{
#Override
public void mouseClicked(MouseEvent event)
{
clickButtonAt(event.getPoint());
}
});
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new JScrollPane(jlist));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private void clickButtonAt(Point point)
{
int index = jlist.locationToIndex(point);
ButtonItem item = (ButtonItem) jlist.getModel().getElementAt(index);
item.getButton().doClick();
// jlist.repaint(jlist.getCellBounds(index, index));
}
public class ButtonItem
{
private JButton button;
public ButtonItem(String name)
{
this.button = new JButton(name);
button.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
System.out.println(button.getText() + " was clicked.");
}
});
}
public JButton getButton()
{
return button;
}
#Override
public String toString()
{
return button.getText();
}
}
class ButtonListRenderer extends JButton implements ListCellRenderer
{
public Component getListCellRendererComponent(JList comp, Object value, int index,
boolean isSelected, boolean hasFocus)
{
setEnabled(comp.isEnabled());
setFont(comp.getFont());
setText(value.toString());
if (isSelected)
{
setBackground(comp.getSelectionBackground());
setForeground(comp.getSelectionForeground());
}
else
{
setBackground(comp.getBackground());
setForeground(comp.getForeground());
}
return this;
}
}
}
Alternatively, you could always layout your JButtons vertically on a JPanel (using a new GridLayout(0,1) perhaps) and then put your JPanel in a JScrollPane, thus mocking a JList of JButtons.

Java JTextArea KeyListener

When I pressed the ENTER my JTextArea starts a new row and I only want do to the doClick() method nothing else.
How should I do that?
textarea.addKeyListener(new KeyListener(){
#Override
public void keyPressed(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_ENTER){
button.doClick();
}
}
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyReleased(KeyEvent e) {
}
});
Use .consume():
Consumes this event so that it will not be processed in the default
manner by the source which originated it.
public void keyPressed(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_ENTER){
e.consume();
button.doClick();
}
}
Documentation
You should use KeyBindings with any JTextComponent in question. KeyListeners are way too low level from Swing's perspective. You are using the concept which was related to AWT, Swing uses KeyBindings to do the same task with more efficiency and provides desired results :-)
A small program for your help :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class KeyBindingExample {
private static final String key = "ENTER";
private KeyStroke keyStroke;
private JButton button;
private JTextArea textArea;
private Action wrapper = new AbstractAction() {
#Override
public void actionPerformed(ActionEvent ae) {
button.doClick();
}
};
private void displayGUI() {
JFrame frame = new JFrame("Key Binding Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel(new BorderLayout(5, 5));
textArea = new JTextArea(10, 10);
keyStroke = KeyStroke.getKeyStroke(key);
Object actionKey = textArea.getInputMap(
JComponent.WHEN_FOCUSED).get(keyStroke);
textArea.getActionMap().put(actionKey, wrapper);
button = new JButton("Click Me!");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
System.out.format("Button Clicked :-)%n");
}
});
contentPane.add(textArea, BorderLayout.CENTER);
contentPane.add(button, BorderLayout.PAGE_END);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
new KeyBindingExample().displayGUI();
}
};
EventQueue.invokeLater(r);
}
}

Making a single component full screen

I'm trying to make a JPanel go full screen when you click a button, and back again when you press escape.
I've managed to get the window to go full screen, but because of the whole thing about adding components removing them from other containers, I end up with a blank JPanel.
I chose to make a separate JFrame to render full screen, the class of which is as follows (note that this is an inner class, so myPanel refers to a panel that already exists in MyJFrame):
public class FullScreen extends JFrame {
private static final long serialVersionUID = 1L;
private GraphicsDevice device;
private boolean isFullScreen;
public FullScreen() {
this.setContentPane(myPanel);
this.setUndecorated(true);
// Fullscreen return
this.addKeyListener(new KeyListener() {
#Override
public void keyPressed(KeyEvent e) {
// Exit fullscreen when ESC pressed
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
exitFullScreen();
}
}
#Override
public void keyReleased(KeyEvent e) {
}
#Override
public void keyTyped(KeyEvent e) {
}
});
}
public void enterFullScreen() {
if (!isFullScreen) {
// Get the current device
GraphicsEnvironment graphicsEnvironment =
GraphicsEnvironment.getLocalGraphicsEnvironment();
device = graphicsEnvironment.getDefaultScreenDevice();
if (device.isFullScreenSupported()) {
// Make the current window invisible
MyJFrame.this.setVisible(false);
// Set the full screen window
device.setFullScreenWindow(this);
isFullScreen = true;
}
}
}
public void exitFullScreen() {
if (isFullScreen) {
// Reset the full screen window
device.setFullScreenWindow(null);
MyJFrame.this.setVisible(true);
isFullScreen = false;
}
}
}
Any other bright ideas on how to accomplish this?
Something like this seems to do it alright (to be improved and adapted):
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
class TestFullScreenPanel {
private static class FSPanel implements ActionListener {
private JPanel panel;
private JButton button;
private boolean fullScreen = false;
private Container previousContentPane;
public FSPanel(String label) {
panel = new JPanel(new BorderLayout());
button = new JButton(label);
button.addActionListener(this);
panel.add(button);
}
public JComponent getComponent() {
return panel;
}
#Override
public void actionPerformed(ActionEvent e) {
if (!fullScreen) {
goFullScreen();
} else {
ungoFullScreen();
}
}
private void goFullScreen() {
Window w = SwingUtilities.windowForComponent(button);
if (w instanceof JFrame) {
JFrame frame = (JFrame) w;
frame.dispose();
frame.setUndecorated(true);
frame.getGraphicsConfiguration().getDevice().setFullScreenWindow(w);
previousContentPane = frame.getContentPane();
frame.setContentPane(button);
frame.revalidate();
frame.repaint();
frame.setVisible(true);
fullScreen = true;
}
}
private void ungoFullScreen() {
Window w = SwingUtilities.windowForComponent(button);
if (w instanceof JFrame) {
JFrame frame = (JFrame) w;
frame.dispose();
frame.setUndecorated(false);
frame.getGraphicsConfiguration().getDevice().setFullScreenWindow(null);
frame.setContentPane(previousContentPane);
panel.add(button);
frame.revalidate();
frame.repaint();
frame.setVisible(true);
fullScreen = false;
}
}
}
TestFullScreenPanel() {
final JFrame f = new JFrame(TestFullScreenPanel.class.getSimpleName());
f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
f.add(new FSPanel("Center").getComponent(), BorderLayout.CENTER);
f.add(new FSPanel("North").getComponent(), BorderLayout.NORTH);
f.add(new FSPanel("South").getComponent(), BorderLayout.SOUTH);
f.add(new FSPanel("West").getComponent(), BorderLayout.WEST);
f.add(new FSPanel("East").getComponent(), BorderLayout.EAST);
f.setSize(800, 600);
f.setLocationByPlatform(true);
f.setVisible(true);
}
public static void main(String[] args) {
// start the GUI on the EDT
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new TestFullScreenPanel();
}
});
}
}
PS: disposal of the JFrame is only there to change the setUndecorated state.
don't extend JFrame, create this Object an local variable
JFrame by default never react to the KeyEvents, set KeyListener to the JPanel
don't to use KeyListener for Swing JComponents, otherwise have to JPanel#setFocusable
use KeyBindings instead of KeyListener
use Escape by #camickr
.
import java.awt.Dimension;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
public class FullScreen {
private static final long serialVersionUID = 1L;
private GraphicsDevice device;
private JButton button = new JButton("Close Meeee");
private JPanel myPanel = new JPanel();
private JFrame frame = new JFrame();
public FullScreen() {
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
myPanel.setFocusable(true);
myPanel.add(button);
frame.add(myPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke("ENTER"), "clickENTER");
frame.getRootPane().getActionMap().put("clickENTER", new AbstractAction() {
private static final long serialVersionUID = 1L;
#Override
public void actionPerformed(ActionEvent e) {
exitFullScreen();
}
});
enterFullScreen();
frame.setVisible(true);
// code line for #MOD
// from http://stackoverflow.com/questions/15152297/how-to-get-extendedstate-width-of-jframe
Runnable doRun = new Runnable() {
#Override
public void run() {
System.out.println(frame.getBounds());
}
};
SwingUtilities.invokeLater(doRun);
}
private void enterFullScreen() {
GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
device = graphicsEnvironment.getDefaultScreenDevice();
if (device.isFullScreenSupported()) {
device.setFullScreenWindow(frame);
frame.validate();
}
}
private void exitFullScreen() {
device.setFullScreenWindow(null);
myPanel.setPreferredSize(new Dimension(400, 300));
frame.pack();
}
public static void main(String[] args) {
Runnable doRun = new Runnable() {
#Override
public void run() {
FullScreen fullScreen = new FullScreen();
}
};
SwingUtilities.invokeLater(doRun);
}
}
Here's my class built into an example that works very nicely. I'm sure I'm not disposing and validating the frame properly so please comment on it so I can update it.
public class FullScreenExample extends JFrame {
public class FullScreen {
private GraphicsDevice device;
private JFrame frame;
private boolean isFullScreen;
public FullScreen() {
frame = new JFrame();
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
frame.setContentPane(content);
frame.setUndecorated(true);
// Full screen escape
frame.addKeyListener(new KeyListener() {
#Override
public void keyPressed(KeyEvent e) {
// Exit full screen when ESC pressed
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
exitFullScreen();
}
}
#Override
public void keyReleased(KeyEvent e) {}
#Override
public void keyTyped(KeyEvent e) {}
});
}
public void enterFullScreen() {
if (!isFullScreen) {
// Get the current device
GraphicsConfiguration config = FullScreenExample.this.getGraphicsConfiguration();
device = config.getDevice();
// Remove the panel from the wrapper
myWrapper.remove(myPanel);
// Add the panel to the full screen frame
frame.getContentPane().add(myPanel);
// Set the full screen window
device.setFullScreenWindow(frame);
isFullScreen = true;
}
}
public void exitFullScreen() {
if (isFullScreen) {
// Remove the fractal from the full screen frame
frame.getContentPane().remove(myPanel);
// Add the panel back to the wrapper
myWrapper.add(myPanel);
// Disable full screen
device.setFullScreenWindow(null);
// Dispose frame
frame.dispose();
// Revalidate window
FullScreenExample.this.validate();
isFullScreen = false;
}
}
}
/*
* This example uses a main content panel, myPanel
* and a wrapper to host the panel in the main JFrame, myWrapper.
*/
private JPanel myPanel, myWrapper;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
FullScreenExample frame = new FullScreenExample();
frame.init();
frame.setVisible(true);
}
});
}
public void init() {
// Generate example main window
JPanel content = new JPanel();
content.setBorder(new EmptyBorder(5, 5, 5, 5));
content.setLayout(new BorderLayout());
this.setContentPane(content);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myPanel = new JPanel();
myPanel.setBackground(Color.BLUE);
// Full screen button and listener
JButton fullscreen = new JButton("Full Screen");
final FullScreen fs = new FullScreen();
fullscreen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
fs.enterFullScreen();
}
});
myWrapper = new JPanel();
myWrapper.setLayout(new BorderLayout());
myWrapper.add(myPanel);
content.add(myWrapper, BorderLayout.CENTER);
content.add(fullscreen, BorderLayout.SOUTH);
this.setBounds(100, 100, 350, 350);
}
}

Categories