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();
}
}
});
}
}
Related
My program complies and displays a blank frame. I have tried multiple ways, but I am thinking this should work, I don't understand why it doesn't?
The code is simple it just shows a blank box and a button that informs the user the action Listener is working.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Udemy {
public Udemy() {
JFrame f = new JFrame();
f.setTitle("La's Frame");
f.setSize(400,400);
f.setVisible(true);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
f.getContentPane().add(panel);
JButton b1 = new JButton("Click me");
panel.add(b1);
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("Action Listener is working");
}
});
}
public static void main(String[] args){
Udemy ud = new Udemy();
}
}
Here, I moved f.setVisible(true); to the bottom and everything works fine
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Udemy {
public Udemy(){
JFrame f = new JFrame();
f.setTitle("La's Frame");
f.setSize(400,400);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
f.getContentPane().add(panel);
JButton b1 = new JButton("Click me");
panel.add(b1);
f.setVisible(true);
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("Action Listener is working");
}
});
}
public static void main(String[] args){
Udemy ud = new Udemy();
}
}
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);
}
});
}
}
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);
}
}
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);
}
}
I've got a Frame (named here "MainApplication"), which mainly has a JPanel to show informations, depending on the context.
On startup, the MainApplication has an empty JPanel.
It then creates a "LoginRequest" class, which creates a simple login/password form, and send it back to the MainApplication, which displays it in its JPanel.
The "LoginRequest" class implements ActionListener, so when the user clicks on the "Login" button, it checks wheter or not the login/password is correct, and, if the user is granted, I want to unload that form, and display the main screen on the MainApplication Frame.
So, to do it, I came up with this :
public class LoginRequest implements ActionListener {
protected MainApplication owner_m = null;
public LoginRequest(MainApplication owner_p) {
owner_m = owner_p;
}
#Override
public void actionPerformed(ActionEvent event_p) {
// the user just clicked the "Login" button
if (event_p.getActionCommand().equals("RequestLogin")) {
// check if login/password are correct
if (getParameters().isUserGranted(login_l, password_l)) {
// send an ActionEvent to the "MainApplication", so as it will
// be notified to display the next screen
this.owner_m.actionPerformed(
new java.awt.event.ActionEvent(this, 0, "ShowSummary")
);
} else {
messageLabel_m.setForeground(Color.RED);
messageLabel_m.setText("Incorrect user or password");
}
}
}
}
Then, the "MainApplication" class (which extends JFrame) :
public class MainApplication extends JFrame implements ActionListener {
protected void load() {
// create the panel to display information
mainPanel_m = new JPanel();
// on startup, populate the panel with a login/password form
mainPanel_m.add(new LoginRequest(this).getLoginForm());
this.add(mainPanel_m);
}
#Override
public void actionPerformed(ActionEvent event_p) {
// show summary on request
if (event_p.getActionCommand().equals("ShowSummary")) {
// remove the previous information on the panel
// (which displayed the login form on this example)
mainPanel_m.removeAll();
// and populate the panel with other informations, from another class
mainPanel_m.add(...);
...
...
}
// and then refresh GUI
this.validate();
this.repaint();
this.pack();
}
}
When the ActionEvent is sent from the "LoginRequest" class to the "MainApplication" class, it executes the code, but at the end, nothing happens, as if the JFrame wasn't repainted.
Any ideas ?
Thanks,
The best way would be to use JDialog (main frame JFrame would be a parent component) for login form and CardLayout to switch between panels (so there is no need for removing, repainting and revalidating):
Your main form should look something like this:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MainFrame{
JFrame frame = new JFrame("Main frame");
JPanel welcomePanel = new JPanel();
JPanel workspacePanel = new JPanel();
JPanel cardPanel = new JPanel();
JButton btnLogin = new JButton("Login");
JLabel lblWelcome = new JLabel("Welcome to workspace");
CardLayout cl = new CardLayout();
LoginRequest lr = new LoginRequest(this);
public MainFrame() {
welcomePanel.add(btnLogin);
btnLogin.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
lr.setVisible(true);
}
});
workspacePanel.add(lblWelcome);
cardPanel.setLayout(cl);
cardPanel.add(welcomePanel, "1");
cardPanel.add(workspacePanel, "2");
cl.show(cardPanel,"1");
frame.getContentPane().add(cardPanel);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setPreferredSize(new Dimension(320,240));
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String [] args){
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new MainFrame();
}
});
}
}
Your login form should look something like this:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginRequest extends JDialog{
/**You can add, JTextFields, JLabel, JPasswordField..**/
JPanel panel = new JPanel();
JButton btnLogin = new JButton("Login");
public LoginRequest(final MainFrame mf) {
setTitle("Login");
panel.add(btnLogin);
btnLogin.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//Put some login logic here
mf.cl.show(mf.cardPanel,"2");
dispose();
}
});
add(panel, BorderLayout.CENTER);
setModalityType(ModalityType.APPLICATION_MODAL);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
pack();
setLocationByPlatform(true);
}
}
EDIT:
Your way:
MainFrame class:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MainFrame{
JFrame frame = new JFrame("Main frame");
JPanel welcomePanel = new JPanel();
JPanel workspacePanel = new JPanel();
JPanel cardPanel = new JPanel();
JButton btnLogin = new JButton("Login");
JLabel lblWelcome = new JLabel("Welcome");
LoginRequest lr = new LoginRequest(this);
public MainFrame() {
welcomePanel.add(btnLogin);
btnLogin.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
lr.setVisible(true);
}
});
workspacePanel.add(lblWelcome);
frame.getContentPane().add(welcomePanel);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setPreferredSize(new Dimension(320,240));
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String [] args){
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new MainFrame();
}
});
}
}
LoginRequest class:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginRequest extends JDialog{
/**You can add, JTextFields, JLabel, JPasswordField..**/
JPanel panel = new JPanel();
JButton btnLogin = new JButton("Login");
public LoginRequest(final MainFrame mf) {
setTitle("Login");
panel.add(btnLogin);
btnLogin.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//Put some login logic here
mf.frame.getContentPane().removeAll();
mf.frame.add(mf.workspacePanel);
mf.frame.repaint();
mf.frame.revalidate();
dispose();
}
});
add(panel, BorderLayout.CENTER);
setModalityType(ModalityType.APPLICATION_MODAL);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
pack();
setLocationByPlatform(true);
}
}