Communicating between open classes - java

I have three classes. One is a worker class that does all the hard work but doesn't display anything. The other two are GUI classes one of which calls the other. The one that calls the second GUI class has the worker class open.
The first GUI calls the second with this method:
protected void openAdd() {
AddPet add = new AddPet(ADD_PROMPT, FIELDS, DATE_PROMPT);
add.setVisible(true);
}
The second GUI class is used to get information from the user that is used in the worker class but, as I already have the worker class open in the first GUI I don't want to open it again and I want to use some of the information in the first GUI.
What I need to do is pass that information in the second GUI back to the first GUI so that it can work with it and pass it to the open worker class.
How do I do this?
EDIT:
I think the best option would be to call a method in the first GUI from the second GUI but I don't know if this is even possible.

On second thought, it looks like your second window is being used essentially as a dialog off of the first window, and that you're using it for user data entry and little else. If so, then make sure that the second window is not a JFrame but is rather a modal JDialog. If so, then it will block user interaction with the first window when it's open, and extracting information out of it will be easy, since you're know exactly when the user is done with it, since program flow will resume in the first GUI immediately following your code that sets the second window visibile.
e.g.,
// in this example, AddPet is a modal JDialog, not a JFrame
protected void openAdd() {
// I'm passing *this* into the constructor, so it can be used ...
// ... in the JDialog super constructor
AddPet add = new AddPet(this, ADD_PROMPT, FIELDS, DATE_PROMPT);
add.setVisible(true);
// code starts here immediately after the AddPet modal dialog is no longer visible
// so you can extract information from the class easy:
String petName = add.getPetName(); // I know you don't use these exact methods
String petBreed = add.getPetBreed(); // but they're just a "for instance" type of code
// ... etc
}
....

Related

Handling button event from another class in Jframe

I am using netbeans to make a GUI. To make it simple, let say what I want to do is that when I press a button it just opens a new JFrame and print a text on a JPanel, a text entered by the user.
It sounds easy if I just complete the automatically generated function from Netbeans :
private void popUpButtonActionPerformed(java.awt.event.ActionEvent evt) {
// I added these three lines
JFrame newFrame = new myPopUpWindow();
myPopupWindow.getTextLabel().setText("Hello" + enteredText.getText());
this.dispose();
}
And everything works as I want. Now what I want to do is having a cleaner code. I want to put these two line in a class ButtonHandler.java in a function handleHelloPopUp(String enteredStringText) Now comes the dilemma:
If I make handleHelloPopUp static, I get some error when I access some non static variable
If I don't make handleHelloPopUp I just have to pass it as argument in my Jframe -> horrible code structure
What is the best way to do this ? Please help ...

Displaying a JOption Pane from a functions class

I have two classes one that I have in my program. One I have drawn my Graphical user interface and another that has methods to be executed by buttons in GUI class. Basically it's a program to add records to a database. When I click button register it calls the functions class to execute add to database method. In the add to database method I need to add a jOption pane to display successful or not.. How do I ho about it .
I have tried
JOptionPane.showMessageDialog(this,"successful"); the code is not working since it is not in the GUI class
the code is not working since it is not in the GUI class
You can pass the "frame" as a parameter to your "functions" class.
Then the "functions" class can use that value as the "frame" for the JOptionPane.

actionPerformed inside the actionPerformed method in Java

I am working on a java swing project and I have a frame with multiple buttons and when someone clicks any of that button,user is asked for an input(say the user enters n) and a new frame is created with n text fields with another button "Ok"
{new frame is created inside the actionPerformed method.So my question is how to handle the action events for this Ok button.I tried using an anonymous class but I can't do much with variables inside it.
Source File:http://www.mediafire.com/file/9ed2h0yeyis5tk6/OSProject.txt
Use a modal JDialog or JOptionPane. See How to Make Dialogs
Define the functionality for the second window within it's own class, so you can isolate the functionality and management in more easily manageable manner. Provide setters and getters to allow information to passed between it and the class which needs to use it. If you're using a JOptionPane, this step makes it easier to define a custom component which can be displayed by it

java: how can i pass a variable from the starter JFrame to the last without having to pass it through all JFrames?

I have this problem: I have an ArrayList that contains a few items that I want to use in a particular frame, the problem is that the array list gets full by initializing it in the main class of my project. In this class I also launch my starting frame that is chained with other frames (login frame -> middle frame --> last frame). I want to carry this ArrayList without having to carry it on through all frames and get it directly usable from main --> last frame. How can I do this?
EDIT
Wat I did it was like first frame start with this ArrayList as parameter:
Jframe jf = new LoginFrame(arraylistvariable,"Login Window");
Then in all ActionListener calls on the buttons that create new frames, disposing old ones, I set it like:
Jframe jo = new MiddleFrame(arraylistvariable,"Middle Window");
Passing this variable all over the frames but I want this to be like called only by the frame that needs this, because login frame doesn't need this variable. However, it is necessary to start the program by the login frame.
"However, it is necessary to start the program by the login frame."
No it's not.
public class MiddleFrame() {
private LoginFrame;
}
...
public static void main(String[] args){
new MiddleFrame();
}
Make the middle frame not visible upon instantiation, but make the LoginFrame visible. If the login is successfful, but make the MiddleFrame visible.
Note You don't need to use to many frame. Make use of JDialogs. See this answer for How to make a Login with a JDialog
one from the easiest way it to pass it/them trough system properties
You might want to create some class like ArrayListVariableProviderService and make it accessible either to all classes (JFrames) or via static call.
This can act as a container to many things, yet it'll abstract out all peculiarities.
Good luck.

Passing variables within several JFrames and classes

sorry guys, but I got confused after many hours sitting ang writting the code.
Situation:
I have two JFrames. These are different Java classes - one of them is the FirstGUI, from which we can call the other JFrame called SecondGUI. OK - that's clear.
I also have one class called Processor in which i have specific methods like "connectToPort" or "disconnectFromPort".
Also, in the FirstGUI (which has the main method) I'm creating SecondGUI object (and set setVisible to false) and Processor object with FirstGUI and SecondGUI as parameters.
Problem:
From the FirstGUI in I want to call out SecondGUI (by setVisible to true) - OK, done. But what about calling out the created at the beginning Processor object from the SecondGUI JFrame? It's important to call the SAME object, because Processor methods can for example set text in the FirstGUI JFrame.JTextPane component, and add items to JComboBox of the SecondGUI.
I don't know how to solve this, I'm always getting NullPointerException.
EDIT:
I want to add that I can't pass the Processor object as an argument while creating SecondGUI, because Second GUI is created earlier and it is an argument while creating Processor... That's the problem.
When constructing the second GUI (child), the initiating class (FirstGUI) can pass self in constructor, and also retain the reference to the constructed object. Now both GUIs have the reference to each other:
class F1 extends JFrame {
F2 child;
void createF2() {
child = new F2(this);
child.setVisible(true);
}
}
class F2 extends JFrame {
final F1 parent;
F2(F1 parent) { this.parent = parent; };
}
If you've searched out your problem or for similar problems on this site, you'll know that the most common recommendation is not to use multiple JFrames, that it is suggestive of a bad design. Better to either swap views with a CardLayout or use JDialogs of the appropriate modality.
As for your question, having one class pass information dynamically to another class, there are two main options depending on program structure.
If one class is in a modal JDialog, then the first class can pull information from the second modal class by calling appropriate getter methods after the second window is no longer visible.
If one class is displayed non-modally, then you'll want to use some type of listener such as a PropertyChangeListener to have the listening class be notified by the observed class when state changes occur.
Edit
Regarding:
From the FirstGUI in I want to call out SecondGUI (by setVisible to true) - OK, done. But what about calling out the created at the beginning Processor object from the SecondGUI JFrame? It's important to call the SAME object, because Processor methods can for example set text in the FirstGUI JFrame.JTextPane component, and add items to JComboBox of the SecondGUI.
Audrius gives you an answer for that. 1+ up-vote given to his answer.
I don't know how to solve this, I'm always getting NullPointerException.
If you get a NPE, should carefully inspect the line that throws the NPE to see which variable is null and then trace back in your code to see why. If your still stuck on a NPE and need our help, then you'll want to show pertinent code and give us more detail on the problem including noting which variable is null and why you think that it shouldn't be null.
EDIT: I want to add that I can't pass the Processor object as an argument while creating SecondGUI, because Second GUI is created earlier and it is an argument while creating Processor... That's the problem.
This is a non-issue. Since the dependent window is displayed dynamically, you can always pass a reference just prior to displaying it using a setter method.

Categories

Resources