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.
Related
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 ...
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.
I have three classes I am working with here. The first is an admin page where the user can select to either add, update or delete an employee on the system via a drop down box. When the user selects one of the three options from the combo box, a JFrame (employees) comes up with the necessary fields to perform their task and the admin frame will still be displayed behind it. On this frame there is a "Cancel" button. When they click the cancel button only this frame must close but keep the admin frame open still. The button is generated from a separate class (empClass) to be displayed on the employee frame. My problem now is I am struggling to get the button to dispose of the employee frame, but out of the several ways of trying this it cannot work. One way produced an error each time I ran the application, another method caused the application to crash/freeze whenever I try select an option to perform on the employee frame and the code I have currently implemented performs no action at all. I think the problem is a an issue with communicating with the forms but I am not entirely sure. Please help as I have been struggling with this for hours and the internet is providing absolutely nothing useful. Most resources refer to the dispose() method which I have tried in various ways but all the ways I have tried do not work, crash the application or cause errors to occur. Even the other questions similar to this on here have not helped me out at all.
I have tried calling the button from the employee frame to try and link the function to the "Cancel" button. Here is the code I have implemented in the empClass:
public void disposeof()
{
employees empp = new employees();
empp.dispose();
}
private void cancelActionPerformed(java.awt.event.ActionEvent evt)
{
disposeof();
}
Here is the employee coding:
public class employees extends javax.swing.JFrame {
empClass ec = new empClass();
adminPage ap = new adminPage();
public employees() {
initComponents();
getContentPane().add(ec.getpanel());
this.add(ec.getpanel());
this.add(ec.lbltitle);
this.add(ec.cancel);
this.add(ec.bfunction);
this.add(ec.empList);
}
As you didn't provide the code of your JFrame so I guess problem is in the code of your JFrame. You might be setting setDefaultCloseOperation(JFrame .EXIT_ON_CLOSE) for your employee class as it is static property so it will close all JFrames. You should set it setDefaultCloseOperation(employees.DISPOSE_ON_CLOSE) OR setDefaultCloseOperation(employees.HIDE_ON_CLOSE). And after that while triggering your event you can call empp.dispose(); OR setVisible(flase).
First of all only use one JFrame and use JDialogs for the underlying other windows you may want to see appearing. On JDialog, use setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE).
Another way you can do this is to hide the frame using setVisible(false).
I'm trying to be creative with a class project in Java.
I have a JFrame which takes in some input from the user, and I want to create a sort of pop up frame that will show a graph from the input.
The second frame is only going to be showing a graph. I'm having trouble realizing this.
I'm using the GUI builder(Netbeans) for the main JFrame and at first I tried to just create an empty JPanel and set it visible from the button, but that didn't work and I soon found out I couldn't.
I had to use a JFrame or some other container apparently(am I wrong?).
So now I'm thinking, how will I pass the information to the second pane for the graphing information?
Anyways, I'm just looking for some input or good practices in order to accomplish this. I'm reading up on CardLayout now but that's not the solution I want since it would require me to create a pane to hold the graph component. I just want the graph button to open up a graph pane(or frame) and close when the user wants to. The main frame is only used to take the input for the graph. Thank you for any input.
There will be a question soon on how I can actually graph something in Java, but ill tackle one problem at a time
You could create another class for the pop-up JFrame which would probably be the best idea. You could do something like:
public class PopupFrame extends JFrame{
//Declare variable you wish to use for your graph here.
public PopupFrame(/* Pass in variable(s) that you will need to display the information. */)
{
super("Graph");
//Set the graph variable equal to the variable to passed into the constructor.
add(/* Put in the variable that you just initialized above and that you declared outside of the constructor. */);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,500);// Change to the dimension that you want.
setLocation(50,50);// You could also have the constructor give it these values if you want to center it on your parent window.
setVisible(true);
}
}
and in your main class, have something like:
PopupFrame f = new PopupFrame(/* Graph variable(s) passed in here. */);
f.show();
Hopefully this was of some use to you. Good luck!
From the already opened jPanel I open the jFrame "FastSearch" search window using the following method:
new FastSearch().setVisible(true);
which lists the search results. When I select one of the search results I want to send the value back to the parent jPanel which opened the child jFrame "FastSearch".
I thought it would look something like this:
ParentWindow.targetVariable = theValueFromTheOpenedForm;
But of course, it doesn't work.
How do I pass data between opened jPanel and jFrame?
Thanks in advance
You'll need to pass the object through to the FastSearch class in some way. Either in the constructor or through some other method. I'm assuming there is probably a listener interface.