Handle event from another class (java) - java

i'm at a standstill developing a java application.
I have a series of JFrames that open each other, and once I open the next, the frame before shall close.
Now, all works fine, I used
setVisible(false);
to "close" the frame, but I have some problems with a specific form I generated:
The form has some panels inside, and the panels each have a button which opens the next form. Now, I wonder how can I apply setVisible(false) to the form that contains those panels.
ChooseForm is the name of the form that contains the panels;
And I have this event handling the button click of each panel's button
private void btn_scegliMouseClicked(java.awt.event.MouseEvent evt) {
Database.getAbitazioneByCodice(label_codice.getText());
MainForm.showFrame();
}
Thanks in advance for the help.

There are many ways to do this.
A Simple way you can do like this:
class YourFrame extends JFrame implements ActionListener {
private YourPanel panel;
public YourFrame() {
//...
panel.addButtonActionListener(this);
}
public void actionPerformed(ActionEvent e) {
//show other Frame and hide this Frame
}
}
class YourPanel extends JPanel {
private JButton yourButton;
public void addButtonActionListener(ActionListener listener) {
yourButton.addActionListener(listener);
}
}

Related

JFrame appears blank after swapping JFrames twice (using Intellij GUI designer)

I'm relatively new to Java Swing in general and decided to use Intellij's GUI Designer. For those not familiar with it, Intellij creates 2 files for each GUI "screen". One is the "design" in the form of a .form file where you drag and drop UI components, and one is the "controller".
Anyway, I'm doing a 5-step questionnaire implemented on Java Swing. The first part asks the user what is their favourite fruit, and when a choice button is clicked, the choice is saved and the next JFrame appears with the next question and so on. JFrame1 transitioning to JFrame2 worked fine, and all UI components were shown. However, when I tried to transition from JFrame2 to JFrame3, JFrame3 showed up blank instead.
I've tried to call .pack() before setVisible(true), and then calling .toFront() after that, but that didn't help.
Below shows a section of my code. JFrame1, 2, and 3 all use the same exact code in its constructors and calling of the next JFrame. JFrame1 only differs by a single line which will be stated later in the code.
JFrame1.java
public class Frame1 extends JFrame {
private JPanel mainPanel;
private JLabel narrationLabel;
private JButton option1_btn;
private JButton option2_btn;
private JButton option3_btn;
public Frame1()
{
setTitle("Questionnaire");
setSize(1000, 1000);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Frame2 nextScreen = new Frame2 (); //declare the next screen to show
add(mainPanel); //this is only for the first starting JFrame
//JFrame2 and JFrame3 do not have this
//repeat this for buttons option2_btn and option3_btn
option1_btn.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
//swap screens to the next main filler selection screen
mainPanel.setVisible(false);
nextScreen.pack();
add(nextScreen.getPanel());
nextScreen.getPanel().setVisible(true);
}
});
}
public JPanel getPanel()
{
return mainPanel;
}
}
Main.java
public class Main
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame1 frame1 = new JFrame1(); //create start screen GUI
frame1.setVisible(true); //display GUI
}
});
}
}
My hunch tells me it might a concurrency issue, but I'm not sure if I am right, or where is the issue and how to resolve it. If a .form file is needed, I would gladly upload the code for it. Any help appreciated.

How to go back to previous frame that was invisible

I am very new to Java Swing and I am working on a login frame with swing that works like this.
After I login successfully in a frame, another new frame is opened while the login frame goes to invisible.
What I am trying to do is that when i close the another frame (after login frame) I want the previous login frame to show again from invisible to visible. please let me know how to do this..:)
Suppose your previous frame is myPreviousFrame
just write myPreviousFrame.setVisible(true); when you want to make visible.
Example:
currentFrame.dispose();
myPreviousFrame.setVisible(true);
Note: if you write code System.exit(0) it will close (terminate) your application. When your application goes terminate you can not make login frame as visible. You need to restart your application. So you need to write dispose().
UPDATED:
I suppose you have a method exitForm() which invokes when you click the Close (X).
Example:
private void exitForm(java.awt.event.WindowEvent evt) {
//System.exit(0); which was used
// to fullfill your requirement you need to write below code
this.dispose();// here [this] keyword means your current frame
//OR simply you can use this.setVisible(false); instead of this.dispose();
myPreviousFrame.setVisible(true); // this will displays your login frame
}
u may try like this:
public class jFrame1 extends javax.swing.JFrame{
// ur code
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jFrame2 f2 = new jFrame2(this);
f2.setVisible(true);
this.setVisible(false);
}
}
public class jFrame2 extends javax.swing.JFrame{
// ur code
private JFrame frame;
public jFrame2(JFrame frame) {
this.frame = frame;
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
this.frame.setVisible(true);
this.setVisible(false);
this.dispose();
}
// so on
}
Here i am just considering two frames and at present you are at second frame and wanna go back to first frame.
public class previous_action implements ActionListener{
public void actionPerformed(ActionEvent t){
Movieticket m;
m=new Movieticket();
m.display();
}
}
Here previous action is a class which will take you back to previous frame.Button frame is a class which sets frame where we are at present.Movie ticket is a public class containing display function which sets frame when the application started that is the first frame.
when the button is clicked it will take you to previous frame.

Java Swing - .dispose() method not closing JFrame

I'm writing a simple game that is based on two JFrames. The user is only displayed on JFrame at a time. To get to the other JFrame, the user clicks a button. The button in the new JFrame will have an overriden method that should navigate the user back to the old JFrame.
I've found success doing this however the .dispose() method doesn't seem to close the new frame when the user tries to navigate back to the old frame.
Here's a snippet of my code (original JFrame class):
public class TicTacToe extends JFrame implements ActionListener{
....
public class gameModeListener implements ActionListener
{
#Override public void actionPerformed(ActionEvent e)
{
TicTacToeSingle singlePlayer = new TicTacToeSingle();
singlePlayer.setVisible(true);
dispose();
}
}
}
And from the other JFrame class:
public class TicTacToeSingle extends TicTacToe{
private int i;
private int j;
JButton boardArray[][];
Random random_generator = new Random();
int randomI;
int randomJ;
public TicTacToeSingle()
{
super("Single Player");
gameMode.setText("Two Player"); //gameMode is the button that has it's actionlistener method overriden. It navigates the user to and back from JFrame to JFrame
gameMode.addActionListener(new gameModeListener());
....
}
....
public class gameModeListener implements ActionListener
{
#Override public void actionPerformed(ActionEvent e)
{
TicTacToe twoPlayer = new TicTacToe("Two Player");
twoPlayer.setVisible(true);
dispose();
}
}
Your help is greatly appreciated :)
Well when u have used Object of the class and u are calling just Dispose(); then how compiler came to know that for this particular class object should be dispose on that call.?
so for giving reference u need to use this try this:
TicTacToe twoPlayer = new TicTacToe("Two Player");
twoPlayer.setVisible(true);
twoPlayer.dispose();
if still there is prob let me know.

Transfer data from one Jframe to another jframe using static class or this reference?

I have one jFrame and its have one jTextbox and a button. Another jFrame have a single jLabel. I want to bring out the text written in first frame's textbox to second frame's jLabel when button is pressed. And as i have searched about this than i got some unreliable answers. But as per my knowledge it could be done by creating another static class or by calling this reference.
It is a question of "what" you want to achieve that will drive the "how"...
For example...
You could maintain a reference to the second frame within the first frame and when the button is clicked, tell the second frame that a change has occurred...
public class FirstFrame extends JFrame {
// Reference to the second frame...
// You will need to ensure that this is assigned correctly...
private SecondFrame secondFrame;
// The text field...
private JTextField textField;
/*...*/
// The action handler for the button...
public class ButtonActionHandler implements ActionListener {
public void actionPerformed(ActionEvent evt) {
secondFrame.setLabelText(textField.getText());
}
}
}
The problem with this is it exposes the SecondFrame to the first, allowing it to do nasty things to it, like remove all the components for example.
A better solution would be to provide a series of interfaces that would allow the two classes to talk with each other...
public interface TextWrangler {
public void addActionListener(ActionListener listener);
public void removeActionListener(ActionListener listener);
public String getText();
}
public class FirstFrame extends JFrame implements TextWrangler {
private JButton textButton;
private JTextField textField;
/*...*/
public void addActionListener(ActionListener listener) {
textButton.addActionListener(listener);
}
public void removeActionListener(ActionListener listener) {
textButton.removeActionListener(listener);
}
public String getText() {
return textField.getText();
}
}
public class SecondFrame extends JFrame {
private JLabel textLabel;
private JTextField textField;
private TextWrangler textWrangler;
public SecondFrame(TextWrangler wrangler) {
textWrangler = wrangler;
wrangler.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
textLabel.setText(textWrangler.getText());
}
});
/*...*/
}
}
This basically restricts what the SecondFrame can actually gain access to. While it can be argued that the ActionListener in the SecondFrame could use the ActionEvent source to find out more information, by it's nature, it would be an unreliable mechanism, as the interface makes no mention of how it should be implemented...
This is a basic example of the Observer Pattern
The simplest way to pass the data between two JFrames is to create a public method.
By this you can create the public method in your class where you want to get the data and call this method directly with an object of second class.
And also if you want to call it without creating an extra object than just make you method static and call is directly with the class name.
for further details
visit this link

Swing - calling events from inside panel

I have simple Swing GUI with main window JFrame and its main panel derive from JPanel. The panel has some buttons that can be clicked and generate events.
I want these events affect data stored in JFrame because it is my main application - it has some queues for thread, open streams and so on.
So how do I make my button in panel invoke callbacks in its parent frame? What is best practice of this for Java/Swing?
To invoke methods in the parent frame you need a reference to the parent frame. So your JPanel's constructor can be declared like this:
public MyPanel(MyFrame frame){
super();
this.frame = frame;
//the rest of your code
}
And in the JFrame you invoke this constructor like this:
panel = new MyPanel(this);//this refers to your JFrame
In the event handlers attached to your buttons you now have access to the frame and can invoke the various methods as needed.
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//do some stuff
frame.someMethod();//invoke method on frame
//do more stuff
}
});
Have a look on this tutorial for using SwingWorker.
Use addActionListener method on desired buttons specifying the class implementing ActionListener.
ActionListenerClass actionListenerObject = new actionListenerClass();
JButton b = new JButton("Button");
b.addActionListener(actionListenerObject);
public class ActionListenerClass implements ActionListener(){
//or better : actionListenerClass extends AbstractAction
public void actionPerformed(ActionEvent e) {
}
}
EDIT:
Yes, I know this. But the action
listener I want to be in parent JFrame
class - this is the problem
then extends JFrame class making the new derived class implementing the desired interface.
You can implement the ActionListener in your class that has the JFrame (or extends it):
class MyPanelClass {
public MyPanelClass(ActionListener al)
{
//...
JButton myButton = new JButton("Button");
myButton.addActionListener(al);
//...
}
}
class MainClass extends JFrame implements ActionListener {
public void someMethod() {
MyPanelClass mpc = new MyPanelClass(this);
}
#Override
public void ActionPerformed(ActionEvent ev) {
// your implementation
}
}

Categories