Programmatically clicking JButton if in focus - java

I'm trying to programmatically click a JButton, which is fine, the doClick() method works prefectly. The problem is that I want to be able to programmatically click whatever button is currently in focus.
I can programmatically give a button focus just fine with .grabFocus() (at least it would seem so) but for some reason .isFocusOwner() always returns false.
If the code is run you can visually confirm that the button 'b2' is indeed in focus, however both if(frame.getFocusOwner() instanceof JButton) and if(b2.isFocusOwner) return false.
The code below illustrates the problem I'm having.
I imagine I've missed something obvious, but any advice would be fantastic.
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JButton b1 = new JButton("b1");
JButton b2 = new JButton("b2");
JTextField j1 = new JTextField(10);
b1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Push the button...");
}
});
b2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("...and let it go...");
}
});
panel.add(b1);
panel.add(b2);
panel.add(j1);
frame.add(panel);
frame.setSize(400, 400);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setVisible(true);
//// The two problems are below
// It looks like this does give 'b2' the focus, at least as far as the generated GUI is concerned
b2.grabFocus();
// First - Always returns false
if(frame.getFocusOwner() instanceof JButton) {
JButton focusedButton = (JButton) frame.getFocusOwner();
focusedButton.doClick();
System.out.println("In focus?");
}
else {
System.out.println("Apparently not");
}
// Second - Also always returns false
if(b2.isFocusOwner()) {
System.out.println("In focus...");
}
else {
System.out.println("Not in focus");
}
}

Not all code executes synchronously. Some code get added to the end of the Event Dispatch Thread (EDT). It appears that this is the case for focus requests. So when the if statements are executed, focus has not yet been placed on the component.
The solution is to wrap your code with a SwingUtilties.invokeLater() so the code gets added to the end of the EDT, so it can execute after the component has received focus:
//b2.grabFocus();
b2.requestFocusInWindow();
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
// First - Always returns false
if(frame.getFocusOwner() instanceof JButton) {
JButton focusedButton = (JButton) frame.getFocusOwner();
focusedButton.doClick();
System.out.println("In focus?");
}
else {
System.out.println("Apparently not");
}
// Second - Also always returns false
if(b2.isFocusOwner()) {
System.out.println("In focus...");
}
else {
System.out.println("Not in focus");
}
}
});
Also, don't use grabFocus(), you should use requestFocusInWindow(). Read the API for more information.

Related

Swing: How do I use multiple buttons?

I created this little test program. It has 2 buttons and 2 labels. I want to know how I can use 2 buttons. So when I press button-1 then I change the text for text-1 and when I press button-2 then I change text for text-2. I just wanna get an idea of how I can use multiple buttons.
My code:
JLabel text1, text2;
JButton button1, button2;
public Game(String title) {
super(title);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setLayout(new FlowLayout());
addComponents();
setSize(250, 250);
setResizable(false);
}
public void addComponents() {
text1 = new JLabel();
getContentPane().add(text1, text2);
text2 = new JLabel();
getContentPane().add(text2);
button1 = new JButton("Button");
getContentPane().add(button1);
button1.addActionListener(this);
button2 = new JButton("Button 2");
getContentPane().add(button2);
button2.addActionListener(this);
}
#Override
public void actionPerformed(ActionEvent e) {
}
I'm new to programming, so I would also like if someone could write some comments for the code. Just so I get an idea on how the code for multiple buttons work.
In your actionPerformed method you can get the source of the action
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button1){
//Do Something
}else if(e.getSource() == button2){
//Do Something Else
}
There are various approaches to add listeners to buttons, here just a couple:
Inner
If you don't have to do much actions in each button you can add inner listener in each button
button1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
// DO STUFF
}
});
Common Listener
If you have more than 2 buttons (i guess your app will be bigger) you can use your actionPerformed(ActionEvent e) and get source of the action
#Override
public void actionPerformed(ActionEvent e) {
JButton source = (JButton) e.getSource();
if(source.equals(button1)){
// DO STUFF
}
}
Use actionCommand to clarify
To clarify this approach I would reccommend to use JButton.setActionCommand(stringCommand) so after you can use a switch:
Declaring buttons:
button1.setActionCommand("command1");
button2.setActionCommand("command2");
In ActionListener::actionPerformed()
public void actionPerformed(ActionEvent e) {
String command = ((JButton) e.getSource()).getActionCommand();
switch (command) {
case "command1":
// DO STUFF FOR BUTTON 1
break;
case "command2":
// DO STUFF FOR BUTTON 2
break;
}
}
Using Java 8, its it much more concise to add ActionListeners:
button.addActionListener(ae -> System.out.println("foo"));
Using multiple statements:
button.addActionListener(ae -> {
System.out.println("foo");
System.out.println("bar");
});
You should not use setVisible(true) before the components are added.
There are a few ways to deal with more elements in an ActionEvent:
e.getSource() returns the object on which the event occurred. So, if button1 was pressed, e.getSource() will be the same as button1 (and e.getSource()==button1 will thus be true)
You can use separate classes for each ActionEvent. If you would add the ActionListener "Button1ActionEvent" [button1.addActionListener(new Button1ActionEvent());] you have to create this class, let it implement ActionListener and add the method actionPerformed as you had in your main class. Also, you can create a listener inside of the addActionListener-method [button1.addActionListener(new ActionListener() { // actionPerformed-method here });]

JDialog opening very slow

I'm having some problems while working on a project for school. The JDialog renders very slow. Let me show you.
This is the GUI:
When I click "Advance time" it takes around 3 seconds to open the JDialog. When it's open I get this (which is fine):
But when I drag that JDialog around, I get this (which is not ok):
It also takes very long to close the JDialog. It closes, but you can still see it:
This is the snippet of code where I think the problem lays, it's the code for the menu-bar:
private JMenu editMenu()
{
JMenu editMenu = new JMenu("Edit");
editMenu.setMnemonic(KeyEvent.VK_E);
JMenuItem advanceTimeMenuItem = new JMenuItem("Advance time");
advanceTimeMenuItem.setMnemonic(KeyEvent.VK_A);
advanceTimeMenuItem.setToolTipText("Advance the internal clock");
advanceTimeMenuItem.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
//We first create panel to add to the dialog.
JPanel panel = new JPanel();
//Calendar
AdvanceTimePanel calendar = new AdvanceTimePanel(internalClockController);
//Button that will be used to confirm the system time change.
JButton btnSave = new JButton("Save");
//Add actionlistener to the save button
btnSave.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e)
{
Date selectedDate = calendar.getDate();
try
{
internalClockController.updateDateTime(selectedDate);
} catch (InvalidInternalClockException e1)
{
System.out.println("InvalidInternalClockException: " + e1.getMessage());
}
}
});
//Add the components to the panel.
panel.add(btnSave);
//This is the calendar added to the panel.
panel.add(calendar);
//Create the dialog and add the panel to it.
JDialog jDialog = new JDialog();
jDialog.add(panel);
jDialog.setBounds(100, 100, 400, 200);
jDialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
jDialog.setVisible(true);
}
});
editMenu.add(advanceTimeMenuItem);
Does someone have an idea how to speed things up?
Thanks in advance.
Events are handled on one single thread. (Also repaint events.) For the application to remain
responsive, perform longer actions a bit later by using invokeLater, as below:
#Override
public void actionPerformed(ActionEvent event)
{
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
... // All code here
}
});
}
This is quite wordy; Java 8 allows:
advanceTimeMenuItem.addActionListener(
(event) -> {
EventQueue.invokeLater(
() -> {
... // All code
});
});

How to tell if a JFrame window has already been opened?

I have a JFrame with a Button that opens a different JFrame. But I want the button to only open the second frame once. Problem is, every time I click it I get a new instance of the frame. This must be a very common problem, since I'm following a book on how to create this GUI. I find it odd that the author didn't mention this "feature".
So how do I keep my button from opening multiple copies of the new frame?
Instead of letting the button create a new JFrame every time, make the second JFrame a member of the first JFrame, and only let the button call jframe2.setVisible(true);:
class JFrame1 {
JFrame2 jframe2=...;
JButton button=...;
JFrame1() {
...
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jframe2.setVisible(true);
}
});
...
}
}
UPDATED!
try this:
JFrame frame2 = new JFrame(); // instance variable
...
//when button is clicked
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if(!frame2.isVisible())
frame2.setVisible(true);
}
});
make sure you are handling the closing of all of the JFrames manually like this:
frame2.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
// handle closing the window
frame2.setVisible(false);
frame2.dispose();
}
});
instead of using the JFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
hope this helps.
You should keep a reference to the sub frame you open for first time. At second time you first check if you have a reference or not and then decide to create a new frame or to put the focus onto the existing open frame.
Example as answer to comment of OP (similar to other answer of #AlexanderTorstling, but not immediately creating the sub frame):
class MainFrame extends JFrame {
private JFrame subFrame = null;
MainFrame() {
...
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (subFrame == null) {
subFrame = new JFrame();
...
}
subFrame.setVisible(true);
}
});
}
}
This example has also the advantage to give you the possibility to close the subframe via a registered WindowAdapter if the main frame is closed.
please try this one
JFrame frame2 = new JFrame(); // instance variable
boolean secondWindowIsOpne = false;
...
//when button is clicked
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if(secondWindowIsOpne == false){
frame2.setVisible(true);
secondWindowIsOpne = true;
}
else{
System.out.println("This Window is already running");
}
});
make sure you are handling the closing of all of the JFrames manually like this:
frame2.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
// handle closing the window
secondWindowIsOpne = false;
frame2.setVisible(false);
frame2.dispose();
}
});

jframe actionperformed jframe

I currently have a button that, when clicked, performs a method that creates a jframe with a panel that loads multiple images. If this button is clicked multiple times, the images keep adding onto the prexisting images loaded onto the jframe. What code should I use so that if the button is clicked after the jframe and elements have been loaded, after clicking once, nothing else will be added.
Many thanks
What about:
public void actionPerformed(ActionEvent evt) {
button.setEnabled(false);
// the other code that creates imgFrame
imgFrame.addWindowListener(new WindowAdapter() {
#Override public void windowClosing(WindowEvent evt) {
button.setEnabled(true);
}});
imgFrame.setVisible(true);
}
don't create lots of JFrames on the runtime, because these Object never gone from Used JVM Memory untill current JVM instance exist
you have look at CardLayout that very confortly to solve your issues with multiple of views (in this case in one JFrame)
put Images as Icon/ImageIcon to the JLabel
Disable the button when frame show up, and when the frame close, enable the button.
public void actionPerformed(ActionEvent evt) {
final JButton finalButton = button;
button.setEnabled(false);
JFrame frame = new JFrame()
{
protected void processWindowEvent(WindowEvent e)
{
if (e.getID() == WindowEvent.WINDOW_CLOSING)
{
finalButton.setEnabled(true);
}
super.processWindowEvent(e);
}
};
frame.setVisible(true);
}
I suggest a boolean that is false when the program starts, and then when the button is clicked, it tests if the boolean is false. If it is false, then create the stuff that you want, and then make it true. If it is true, do nothing, or alert the user not to click the button more that once, or something of that matter
boolean isAlreadyCreated = false;
yourButton.addActionListener(new ActionListener()
{
if(!isAlreadyCreated)
{
//CREATE YOUR NEW FRAME
isAlreadyCreated = true;
}
});

JFrame problems

I am creating a popup JFrame that will have a message and yes/no buttons. I am using this method in 2 ways. In 1, the main program calls this method and in the other, this method is called directly after a previous JFrame is closed. This method works when being called form the main program, but when another JFrame calls it, the JFrame created in this method shows up completely blank and the GUI freezes. I cannot exit out of the JFrame, but I can move it. The freezing is a result of the Thread.yield because response is always null, but in what instances will the JFrame fail to be created properly?
Note: response is a static variable. Also when this JFrame is created by another JFrame, the original JFrame does not exit correctly. That JFrame has a JComboBox, and the selected option is frozen on the dropdown. When it does not call this method, it closes properly.
public static String confirmPropertyPurchase(String message)
{
response = null;
final JFrame confirmFrame = new JFrame("Confirm");
confirmFrame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent ev){
response = "No";
}
public void windowDeactivated(WindowEvent e) {
confirmFrame.requestFocus();
}
});
final JPanel confirmPanel = new JPanel();
final JButton yes = new JButton();
final JButton no = new JButton();
yes.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
response = "Yes";
confirmFrame.setVisible(false);
}
});
no.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
response = "No";
confirmFrame.setVisible(false);
}
});
final JLabel confirmLabel = new JLabel(" " + message);
yes.setText("Yes");
yes.setPreferredSize(new Dimension(100, 100));
no.setText("No");
no.setPreferredSize(new Dimension(100,100));
confirmFrame.add(confirmLabel, BorderLayout.CENTER);
confirmPanel.add(yes);
confirmPanel.add(no);
confirmFrame.add(confirmPanel, BorderLayout.AFTER_LAST_LINE);
confirmFrame.setPreferredSize(new Dimension(520, 175
));
confirmFrame.pack();
confirmFrame.setVisible(true);
while(response == null)
{
Thread.yield();
}
return response;
}
Again, you shouldn't be using a JFrame as a dialog. In fact your whole bit of code can be replaced with a simple JOptionPane. e.g.,
Component parent = null; // non-null if being called by a GUI
queryString = "Do you want fries with that?";
int intResponse = JOptionPane.showConfirmDialog(parent, queryString,
"Confirm", JOptionPane.YES_NO_OPTION);
myResponse = (intResponse == JOptionPane.YES_OPTION) ? "Yes" : "No";
System.out.println(myResponse);
And this:
while(response == null)
{
Thread.yield();
}
should never be called on the main Swing thread, the EDT or event dispatch thread. The reason the code works when it does is because you're calling this little bit above off of the EDT, but when you call it on the EDT it freezes the EDT and thus the entire GUI. Simply don't do it.
You can't do this, plain and simple. There's only one event thread, and while you're sitting in a loop waiting for somebody to click in your JFrame, you're tying up that thread such that no events can be handled.
Don't try to create your own dialog out of a JFrame -- use JOptionPane or a JDialog, which are designed to handle this situation for you internally.

Categories