Displaying a JOption Pane from a functions class - java

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.

Related

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

How to pass variables from one JFrame to multiple JFrame in Java?

I have been trying to do this a while back but failed to find a way.
My Java program contains four JFrames. JFrame1 contains a table and a button that on clicking opens JFrame2. In JFrame2 there is a table and two buttons that on clicking opens JFrame3 and JFrame4 which contain a table and Save button each.
Now what I want to do is that when a user enters data in JFrame3 and clicks the save button the data is saved in the table of JFrame2 and JFrame1 repectively. Same thing for JFrame4 but the data should be appended not deleted.
The data that is saved in the JFrame1 from JFrame3 and JFrame4 is to be used to insert into the database.
These are my requirements. Can anyone help me out with this problem? Thanks in advance.
What you are doing is usually achieved through two approaches:
You can use the Card Layout to avoid creating a new JFrame each time, but rather have one JFrame and replace its components. This would allow you to have one class which stores this data and then the logic to show it on your different screens.
If (1) does not do it for you, the other option would be to pass on a reference of the parent frame to the child. Thus, when you create an instance of JFrame2, you also pass it along an instance of JFrame1. Each JFrame class will have methods which are responsible for accepting information and displaying it according to your requirements.
Option 1 is usually the most recommended.
You can use parameterized constructor or public variables or even public methods to pass values from 1 JFrame to another also using public static variables(not preferred) will be okay.

Calling A JFrame method from Another JFrame

I'm looking to find a solution too my issue. Currently I have 2 JFrames and 1 utilities class in my netbeans project. I'm no expert on java so please bear with me. I've tried looking through the java docs and on this site but cannot seem to find a solution to my problem.
Here is the scenario:
My launcher class launches the JFrame called MainForm.java the form then initializes components onto the screen. On this form I have a button that launches a new form called ConfigEditor.java. This form is used to edit a config file. I have a Save button on this form, and what I basically want to do is once I click save get the MainForm.java to call a method to fill in the right components with the new values.
Heres an example, heres some of the code from my Save button on the ConfigEditor.java:
if(reply == JOptionPane.YES_OPTION){
try {
Utilities.writeConfigFileBasic(ExecutionLists.getText(),DefaultResultsFile.getText(),
DefaultTestDir.getText(), Environments.getText(), ResultsViewerFile.getText());
ConfigTextArea.append(Utilities.readConfigFile());
JOptionPane.showMessageDialog(rootPane, "Saved");
Now just after the last line I want to call somthing like MainForm.initMyComponents(); as this method exists in the MainForm JFrame but it wont let me call this. The purpose of that method is to populate some of the fields with data extracted from a config file.
I'm sorry if I haven't explained it very well, I'm fairly new to Java if you need any clarification please let me know and I'll do my best to clarify it.
Can you simply pass a reference of MainForm to ConfigEditor when it is constructed? For instance:
... //Code fired by clicking the button you mentioned which is in class MainFrame
ConfigEditor configEditor = new ConfigEditor(this); //This would be a reference to your MainFrame
With this reference you can then call your desired method in the MainFrame class.

Java with JFrame and class interaction

I have two classes in the same Package. The package is called main. Main contains two classes a Main class and a Display class. The display class was created to display a GUI with text boxes and buttons. I gave the buttons a listener that fires when the user clicks one of the button. In the main class is a vector of object that i am storing and need to display one a time in the display class' text boxes.
My Question is: can the mouse clicked action call a method in the main class that gathers the needed info an passes that back to a method in the display class to modify those text boxes> Do i need to combine my two classes into one? How would i do within the classes?
From testing i have made the main class extend the display class. I am able to start the display class from here but when i try to call a method in display from main it does nothing. If i try to call a main method from the display class it also seems to do nothing.
If you want your mechanic to fix your car (by starting it, diagnosing the problem, opening the hood, etc.), you give your car to the mechanic, don't you?
It's the same in Java. If you want the Display object (the mechanic) to access to information available in the Main object (the car), you need to give the Main object to the Display:
Main main = new Main(); // main contains data
Display display = new Display(main) // Display is constructed, and is given the main object
in Display:
public void someButtonClicked() {
String someInformation = this.main.getSomeInformation();
this.someTextField.setText(someInformation);
}

Swing - Organization of JFrames (or JDialogs???) in my program

I have a main controller class that shows a JFrame containing a JTable and, for each row in this table, I have to show a specific "form" on doubleclick.
This secondary window will need information about the specific row selected on the main JTable, as well as some objects saved as fields in the controller class.
An conceptual example of what I need to do is the following:
I have a set of Shops (listed in the JTable in the main JFrame) and, on double click on a row, another window has to appear, allowing for a management of the Shop (sending orders, checking deliveries, etc...).
My question, me being such a newbie with Swing, is: what is the best organization for a common pattern like this one?
Should I model another JFrame and pass as arguments all the data that I could happen to need (I really don't like this), or should I pass only a reference to the Controller class (this would be against the MVC pattern, I think).
Or maybe I should use a JDialog instead of another JFrame? The thing is that, really, the functionality that I need from this second window are a little too big for a dialog, I think...
I am confused, any tip/suggestion/advice will be much appreciated!
Thank you
Regards
Or maybe I should use a JDialog instead of another JFrame?
Bingo.
I actually don't like the idea of having a listener inside my Model class (aka Shop) – implementing the ActionListener. I think I would extend the JDialog class (let’s call it MyJDialog) then when a row is double clicked … create a new instance of MyJDialog class and pass in the Shop object in the constructor. Within the MyJDialog class you can modify the Shop object by calling mutators (setters). Moreover, the Shop class should have a way of notifying observers when a property is changed – take a look at PropertyChangeSupport.

Categories

Resources