This question already has answers here:
Opening a new JFrame from a Button
(4 answers)
Closed 6 years ago.
I know this has been asked thousands of times, but I have never found an answer that works for me. I'm using Java IDE for Java Developers (Eclipse Kepler).
I need to have a JButton which by clicking it, it will close the JFrame that the button is on, and opens a new one that exists in a different class. I have this:
JButton button = new JButton("Click Me!");
add(button);
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
}
});
}
I have no idea what to put after the actionPerformed. And frame.dispose(); does not work for me.
I'm asking, how do I close the JFrame with a JButton, and by clicking the same button it also opens a new class's JFrame?
Here's an example that may help:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class MyFrame extends JFrame {
public MyFrame() {
setLayout(new BorderLayout());
getContentPane().setPreferredSize(new Dimension(400, 250));
JButton btn = new JButton("Click Me");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
JFrame frame2 = new JFrame();
frame2.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame2.setLocation(300, 150);
frame2.add(new JLabel("This is frame2."));
frame2.setVisible(true);
frame2.setSize(200, 200);
}
} );
add(btn,BorderLayout.SOUTH);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
MyFrame frame = new MyFrame();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setLocation(150, 150);
frame.add(new JLabel("This is frame1."), BorderLayout.NORTH);
frame.setVisible(true);
}
});
}
}
Related
On button click, I am creating new JFrame, adding a JButton inside it and setting it visible. JFrame is visible but the JButton is not visible.
I tried finding answers on stackoverflow but everyone says to set the JFrame visible after adding the components. I did that also but still, the issue is not solved
below is button code
#Override
public void actionPerformed(ActionEvent arg0) {
popup.showPopup();
}
I have made a class named "popup" with a show popup method.
Below is the code of popup.java class
package justin;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class popupFrame {
private JFrame f = new JFrame("Please wait...");
public void showPopup() {
System.out.println("Showing Popup");
f.setSize(300, 150);
f.setLayout(new FlowLayout());
f.add(new JButton("Test"));
f.setVisible(true);
}
}
It should show the JFrame with items added in it on the click of button.
Please check the below link for my complete code:
https://github.com/jamesfdz/Justin-code
Since I can't see the rest of your code, I have posted an example instead.
public class ControlInterface{
public ControlInterface() {
JFrame frame = new JFrame();
frame.setSize(400, 400);
frame.add(new JButton("Test"));
frame.setVisible(true);
}
}
And the calling class:
public class BusinessLogic{
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setSize(300, 300);
JButton button = new JButton("Popup");
frame.add(button);
frame.setVisible(true);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button) {
new ControlInterface();
}
}
});
}
}
This question already has answers here:
How are Anonymous inner classes used in Java?
(18 answers)
Closed 5 years ago.
I know these questions have been asked and I have searched for it on stackoverflow but my problem didn't get solved. I am getting error while setting actionlistener to a button but i am always getting errors:
Talk.java:25: error: <identifier> expected
button.addActionListener(new ActionListener());
^
Talk.java:25: error: illegal start of type
button.addActionListener(new ActionListener());
^
THE CODE:
import java.util.Scanner;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.*;
import java.io.BufferedReader;
class Talk extends javax.swing.JFrame {
public void main(String args[]) {
JFrame frame = new JFrame("LET'S TALK");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
JLabel textLabel=new JLabel("What's up.",SwingConstants.CENTER);
textLabel.setPreferredSize(new Dimension(530,100));
frame.getContentPane().add(textLabel, BorderLayout.CENTER);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
JButton button=new JButton("OK");
JPanel panel1=new JPanel();
panel1.add(button);
frame.add(panel1, BorderLayout.SOUTH);}
private class ActionListener {
button.addActionListener(new ActionListener());
public void actionPerformed(ActionEvent e){
final TextField tf=new TextField();
tf.setText("welcome");
}
}
}
You're doing it wrong. Try this
button.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
final TextField tf=new TextField();
tf.setText("welcome");
}
});
Edit: Your brackets are misplaced. Do it like this:
2nEdit: You should only call frame.pack() and frame.setVisible(true) after you add all your components to panel
class Talk extends javax.swing.JFrame {
public static void main(String args[]) {
JFrame frame = new JFrame("LET'S TALK");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
JLabel textLabel=new JLabel("What's up.",SwingConstants.CENTER);
textLabel.setPreferredSize(new Dimension(530,100));
frame.getContentPane().add(textLabel, BorderLayout.CENTER);
frame.setLocationRelativeTo(null);
JButton button=new JButton("OK");
JPanel panel1=new JPanel();
JTextField textField = new JTextField(8);
panel1.add(button);
panel1.add(textField);
frame.add(panel1, BorderLayout.SOUTH);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
textField.setText("welcome");
}
});
frame.pack();
frame.setVisible(true);
}
}
Greggz is right.
You need to create an anonymous inner class of ActionListener implementing actionPerform(ActionEvent e) method and write your action code inside this method. This is the easiest way.
Or, you can create a separate class implementing the ActionListener interface and override the actionPerform(ActionEvent e) method for your action code. Then just place an object of that class here,
button.addActionListener( hereObjectOfThatClass )
I have a problem with JFrame. All I want to do is to create a JFrame for Login with a Button, and when the Button is pressed: it close the Login Frame and opens the Program Frame.
This is my Login Frame:
public static void main(String[] args) {
JFrame frame = new JFrame("My Program");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
LoginPanel primary = new LoginPanel(frame);
frame.setPreferredSize (new Dimension (650, 500));
frame.getContentPane().add(primary);
frame.pack();
frame.setVisible(true);
}
Which opens the Login Panel by passing the Frame in the constructor, the Login Panel:
public class LoginPanel extends JPanel {
JFrame fr;
class submitButton implements ActionListener {
public void actionPerformed(ActionEvent e) {
ProgramFrame programFrame = new ProgramFrame();
programFrame.setVisible(true);
fr.setVisible(false);
fr.dispose();
}
}
public LoginPanel(JFrame frame) {
fr = frame;
JButton submit = new JButton("Button Login");
submit.addActionListener(new submitButton());
add(submit);
}
This is the problem:
When I click on the Button "Button Login" of the LoginPanel, it succesfully opens the new ProgramFrame but it doesn't close at all the old frame (LoginFrame). The LoginFrame becomes smaller, very little, but remains:
Thanks in advance for the help! :)
class submitButton implements ActionListener {
public void actionPerformed(ActionEvent e) {
ProgramFrame programFrame = new ProgramFrame();
programFrame.setVisible(true);
this.dispose(); //changed line
}
}
well your panel is closed bt the jframe is still opened without the jpanel
i have made some changes to your code now both will disposed at the same time
You first initialise JFrame completely, so after inside JButton click event first hide JFrame later dispose it.
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class HideLoginPage{
public static void main(String[] args){
HideLoginPage loginPage = new HideLoginPage();
JFrame frame = new JFrame();
frame.setUndecorated(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setBounds(200, 200, 200, 100);
loginPage.setPane(frame);
frame.setVisible(true);
}
public void setPane(final JFrame frame){
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JButton submit = new JButton("Login");
submit.setSize(100, 30);
panel.add(submit);
submit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
JFrame newFrame = new JFrame();
newFrame.setBounds(400, 200, 400, 400);
newFrame.setVisible(true);
frame.setVisible(false);
frame.dispose();
}
});
frame.getContentPane().add(panel);
}
}
This question already has answers here:
JFrame not presenting any Components
(4 answers)
Closed 7 years ago.
When i run this code:
public class Menu extends JFrame implements ActionListener {
JLabel logo = new JLabel("MyChef");
JPanel north = new JPanel();
public void main(String args[]){
new Menu();
}
Menu(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setTitle("MyChef");
frame.setSize(500, 300);
frame.setVisible(true);
frame.setResizable(false);
frame.add(north, BorderLayout.NORTH);
north.add(logo);
}
public void actionPerformed(ActionEvent e){
}
}
The window opens but it does not show anything... Where is my label? I am very lost because I have done various GUI before and either I am being stupid or i don't know! Sorry if its a stupid question, I'm just so stuck I had to post this.
Your code works for me, but I think it doesn't for you because you add a component after you set the frame visible. Call frame.setVisible(true) (and setSize) after
frame.add(north, BorderLayout.NORTH);
north.add(logo);
So your code should look like this (also formatted it properly):
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Menu extends JFrame implements ActionListener {
JLabel logo = new JLabel("MyChef");
JPanel north = new JPanel();
public static void main(String args[]) {
new Menu();
}
public Menu() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setTitle("MyChef");
frame.setResizable(false);
frame.add(north, BorderLayout.NORTH);
north.add(logo);
frame.setSize(500, 300);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
}
}
I have got a frame in which sometimes a dialog is opened. I would like this dialog to be attached to the existing frame, so for example when I drag that frame the opened dialog follows it. I have heard that this might be possible to achieve using GlassPane but I need some hints. Right now, when I open a new dialog and set its location relative to frame it looks like this:
I would like the "testDialog" to appear next to the frame attached to its upper-right corner.
When I drag the "test" frame, the "testDialog" follows it.
Here is a working example:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Example {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
showGUI();
}
});
}
public static void showGUI() {
final JFrame frame=new JFrame("test");
JButton open=new JButton("Open new dialog");
open.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("test");
JDialog dialog=new JDialog((Frame)null,"testdialog");
dialog.setPreferredSize(new Dimension(200,300));
dialog.getContentPane().add(new JLabel("testlabel"));
dialog.pack();
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);
}
});
frame.setLayout(new FlowLayout());
frame.getContentPane().add(open);
frame.getContentPane().add(new JLabel("test"));
frame.setLocationRelativeTo(null);
frame.setPreferredSize(new Dimension(400, 200));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}