Popup existing Jframe when clicking button in another Jframe - java

i am working in JAVA GUI project with netbeans ,,
I just created Jframe and put a button on it ,,
I created also another JFrame and added many labels
I am asking how can the second JFrame appears when i click on the button in the first JFrame

button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
new SecondFrame().setVisible(true);
FirstFrame.this.dispose(); // if you want the first frame to close
}
Check out How to Write an Action Listener for more information.

Related

How to change the background color of a JFrame with a button in the Mainframe?

I´ve created a simple warehouse management program for my company and everything is working fine.
But now I am trying to implement a darkmode to my program, that will be 'active' if the user presses the corresponding button in the mainframe of the program.
There are multiple buttons in the Mainframe that open other JFrames. And that´s the point im stuck at.
If I´m trying to change the background-color of one of the JFrames it only works if the JFrame already is open.
Here´s an example:
Pressing the "Darkmode"-Button in the Mainframe
turnOnDarkmodePanel(EditArticle.edCP);
Method:
public static void turnOnDarkmodePanel(JPanel panel) {
panel.setBackground(new Color(18,18,18));
}
This will be executed. And while the mainframe is perfectly in darkmode the other JFrames are not until I open them and press the Darkmode button in the mainframe again.
Everything else in the JFrame is working fine - Just the background of the Panel doesnt.
Additional Code for Buttons in Darkmode (Working on every single button without pressing the darkmode button again after opening the JFrame):
public static void turnOnDarkmode(JButton button) {
button.setForeground(button_fg);
button.setBackground(button_dm);
button.setBorder(null);
button.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent evt) {
button.setBackground(button_dm_hover);
}
public void mouseExited(MouseEvent evt) {
button.setBackground(button_dm);
}
});
}
Anyone knows how I can fix this that the JFrames are effected without opening them first?
PS.: I am new to JAVA and if more code is needed - I will add it.
Thanks in advance. :)

create an exit button in java?

I'm a beginner in java, I'm practicing a Project that have should create an exit button that exit the program when we press it. But when I run this project in JDK the exit button is not working.
How can I make exit button work?
There two ways for closing a window:
X exit button at the top side of the window.
Custom exit button.
Solution for 1
To achieve it you should set for the JFrame the suitable default action you want to produce when clicking on X.
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
Solution for 2
this.dispose();
if you want to create a message of yes_no option than follow the given code whenever u press the exit button you have created in J Frame so it will show a dialog message confirm if you want to Exit click yes so your app will be closed.
private void JbtnExitActionPerformed(java.awt.event.ActionEvent evt) {
Frame = new JFrame("Exit");
if (JOptionPane.showConfirmDialog( Frame,"confirm if you Want to Exit","Name of the Application or Title",
JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION)
System.exit(0);
If you want a JButton that closes the application, you would create the button:
JButton Button = new JButton("Close");
Then you would add a custom handler to the button:
Button.addActionListener (new ActionListener ()) {
public void actionPerformed (ActionEvent e) {
System.exit(0);
}
};
Then you would add the button to the frame or panel.
frame.add(button);
(If your frame is called frame)

Opening a new GUI window on a main GUI using GUI builder on netbeans

I am playing about with GUI builder and i was wondering if there is an easy way to open the register window through the current main window (in reference to the page below). I am trying to do this through the menu bar.
I've been trying all day, because GUI Builder generates some code, its not possible to edit this code.
Thanks For the help!
Create a separate class which extends JDialog class and add your GUI components:
public Register extends JDialog {
//Make GUI
setModalityType(ModalityType.APPLICATION_MODAL); //Make it modal
}
Add ActionListener to that menu item which is supposed to open a register window:
mnuItmRegisteration.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Register r = new Register();
r.setVisible(true);
}
});
Right click on that shortcut button, click Events, click ActionPreformed.
There you should write codes to make your register window appear.
An example:
private void RegisterationEventActionPerformed(java.awt.event.ActionEvent evt) {
JFrame Register = new Register();
Register.setVisible(true);
}
Remember to make another JFrame called ("Register" assuming u are using the code i gave) at the same package as your current JFrame
Maybe u would probably should use the run button (The button with a Green Triangle or Arrow), run it try to press the menu item, it should appear the register window.

java - detect action performed on JPanel in JDialog

In netbeans I have a JDialog with a JPanel component (called Keypad). I simply draged and droped the JPanel Keypad onto JDialog and netbeans generated the code. On the Keypad I have an Enter button for which I am trying to detect ActionPerformed (button pressed) in the JDialog. Is this possible and how do I do it?
You have to add an ActionListener to your Enter button. You need to pass a reference to your JDialog in the JPanel constructor so you can communicate with it. You either need to implement ActionListener or you can use an anonymous class:
enterButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Do something to your JPanel reference
}
});

Problem with setVisible(boolean)

I have a JFrame with some component on it. I want that the frame disappears when i click on a special button for example exit button.
I wrote this code in exit button
this.setvisible(false);
but it only hides the component on it and frame doesn't disappear.
What can I do that when I click on exit button the frame disappears?
Here's an example of a button that hides the frame:
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
final JButton hideButton = new JButton("hide frame");
frame.add(hideButton);
hideButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
}
});
frame.setVisible(true);
frame.pack();
In your call this.setVisible(false), this probably refers to the button and not the frame.
You need to call setVisible() on the Frame not the Button.
Also make sure you are calling dispose() on the frame to clean up all resources.
Additionally you should also use
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
during creation of the frame, to make sure the windows is properly closed and disposed when the user clicks the "standard" close button in the upper right corner (on Windows).
This tutorial might also help you understand what's going on better:
http://download.oracle.com/javase/tutorial/uiswing/components/frame.html
call it on JFrame object.
example:
// when exit is pressed
fr.setVisible(false); // fr is a reference to object of type JFrame `

Categories

Resources