I have made a queue linkedlist, my main jFrame window is called "UI" on which there is a button "Donate" after pressing it a new jFrame window called "Donate" opens setting Visibility of previous jFrame(UI) to false (setVisible(false)).
jFrame "Donate" contains some text field and a "Donate Blood" button in the end, after filling out the text fields we need to press the "Donate Blood" button so that the entered values in text field should be stored in linkedlist and then setting jFrame "Donate" Visibility to false and jFrame "UI" to true to return back to our main jFrame window.
The problem is that every time I click "Confirm Donate" my data is not linking, for e.g: 3 people donated
John
Matt
Harry
When I traverse it I can only see the last entered name, where did John and Matt disappear to?
Donate button in "UI" action listener code:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
Donate d = new Donate();
this.setVisible(false);
d.setTitle("Donate - Blood Bank");
d.setVisible(true);
}
Donate Blood button action listener code which calls to donate jFrame window:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
UserInterface ui = new UserInterface();
BloodBank bb = new BloodBank();
enQueue(jTextField1.getText(), (int)jSpinner1.getValue(), (String)jComboBox1.getSelectedItem(), (String)jComboBox2.getSelectedItem());
first.Display();
this.setVisible(false);
ui.setTitle("Blood Bank");
ui.setVisible(true);
}
After debugging it many times I found out that if I remove this line of code from the button action listener and stop the jFrame "Donate" window from going invisible my queue linked list works fine, is setVisible(false) discarding my previous save data? And how can i fix this?
this.setVisible(false);
To help better understand here are some screenshots:-
Donate button in "UI" jFrame:
"Donate" jFrame window:
Each time you are creating a new instance of Donate in the first actionlistener and that of UserInterface in the second actionlistener. If your list is related to the instances of these frames, then you are loosing them. Also, it is not and advisable thing to do. You could create the instances of both the frames in some way so that it can be accessed from both the actionlisteners and then just call setVisible() on the same instances. This should solve your problem.
Related
I'm new to Java and Swing. I created a jframe and I added a menubar and MenuItem in it.
On clicking a menu item, a jdialog should open. Now the jdialog has a jtextfield in it and a jlabel. Now the problem for me is 'when dialog is opened for first time, the textfield is empty and thats correct. Now i close the jdialog and i open it again but now instead of getting an empty textfield in jdialog, i get the data entered previously' which is not what should happen as the jdialogs 'default close operation' property is set to 'dispose'. but that is not happening for me...
I dont know what i'm doing wrong. I have never tried applet/swing before in any other way (consider this as my first demo learning programme)
Second Image here
The JTextField is retaining it's value because it isn't being affected by the JDialog closing, instead it is being hidden as it's parent (the JDialog) is invisible
Setting the dialog to dispose isn't re-initialising the child components, so they keep their values. Some additional information on this behaviour is available here:
JDialog setVisible(false) vs dispose()
JDialog
One way you can prevent / control this is by "informing" the dialog to wipe the textfield as it is closing by adding a WindowEvent and providing the necessary functionality in the windowClosing() method
Netbeans gui-builder will generate this for you with the following:
Right click Dialog
Events
Window
WindowClosing
Providing:
private void jDialog1WindowClosing(java.awt.event.WindowEvent evt) {
// TODO add your handling code here:
}
In which you can add: textfield.setText(""); to clear the textfield
Another approach is to create your own dialog and setting up the components in the constructor. As creating a new instance will contain the components with their default values, effectively resetting it
I'm relatively new to Java and I'm trying to make some kind of quiz. I created 3 JFrames, all in the same package. On my main frame, there are two buttons (one for the english version and the other one for the german version). I want to switch JFrames after pressing these buttons (so that I can, by pressing "English", see and interact with my english quiz frame). Looking it up didn't help me the slightest, because I'm not really experienced with it. Is it even possible to do it like this? If not, how could I do it?
The standard approach is to use the Card Layout, which allows you to use the same JFrame as you populate it with different things at different points in your application. So initially, your JFrame would show the loading screen, then the user presses a button and you load a new set of components without discarding the current JFrame you have. In some cases, you might also need to make some size adjustments.
It is difficult to say without seeing any code, but usually, what is done is that you do something like so:
new Frame(args);
this.dispose();
The code above assumes that the constructor of Frame takes care of launching and making the components visible. The this.dispose(); disposes of the current JFrame (assuming your class extends JFrame).
You have two buttons in your frame 1 right? So first, double click the button which says "English". Lets say the variable name for that button is jButton1. Inside that button type this.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose();
EnglishFrame eng = new EnglishFrame();
eng.setVisible(true);
}
Then double click the other button which says "German" (jButton2). Inside that type this.
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose();
GermanFrame german = new GermanFrame();
german.setVisible(true);
}
this.dispose() - This will cause the jFrame window to close
Then you create an object of the other two forms. (In your case the form for English and Germany)
.setVisible(true) - This will show you the form.
Create a single Jframe window. After that create JPanels with all the compenents such as buttons, textfields and labels you want. Make sure the panel is the same size as your Jframe. Panel's work about the same as JFrame's, code wise.
This code will stitch everything together for you:
panel.setSize(Jframe.getSize()) //That wont
panel.add(button); //Just remember you need to add more code to position the buttons correctly.
//If you using netbeans builder:
//You just have to use this one line in the constructor/intialiser method
frame.add(panel); //This will add the panel to the Jframe/Window
//No need to add extra code for positioning.
If you want to swap between the panels. In the button press method, use this
frame.setContentPane(panel); //panel = panel you want to change too.
frame.repaint(); //Ensures that the frame swaps to the next panel and doesn't get stuck.
frame.revalidate(); //Ensures that the frame swaps to the next panel and doesn't get stuck.
When you first start the java application you have to set the content pane or else it will appear as a blank window.
frame.setContentPane(panel); //Starting Panel
frame.setVisible(true); //Make the frame visible
Sorry if the explanation is bad, I don't have enough time to explain it fully.
So I have a UI that I made with Swing using Java. It is very complex and I want to save the user some time by making the UI simpler.
Imagine you have a page with many CheckBox. Once you select whatever CheckBox you desire, the next button makes a JFrame appear with the same tabs as you selected inside a Tabbed Pane.
Here is a small sample of my code which should give an idea of what I want the button to do:
btnNewButton_7.addActionListener(new ActionListener(){
#SuppressWarnings("static-access")
public void actionPerformed(ActionEvent e){
Admin.set.frame.setVisible(false);
Admin.adv.setVisible(true);
Admin.adv.jp2.setVisible(chckbxNewCheckBox_42.isSelected());
Admin.adv.jp3.setVisible(chckbxNewCheckBox_43.isSelected());
Admin.adv.jp4.setVisible(chckbxNewCheckBox_44.isSelected());
}
});
So the next Button should set the JFrame (set.frame) invisible. Then, it should open adv (a new frame). So far, everything is working. Now, my tabe are JPanel inside adv. I would love to make them disappear whenever the CheckBox 42,43 and 44 are selected or unselected, should I add an onChangeListener?
I have designed two JFrames in NetBeans.
When I click the "rules" button (i.e placed on JFrame1) then it opens a second JFrame (but JFrame2 opens over JFrame1's window, that's what I dont want).
In the second JFrame there is a "close" button. But when I click this button, I want JFrame1 to be opened and it is working too, but JFrame2 is actually not closed and JFrame1 is appearing over JFrame2.
In short the main form is JFrame1. When I click the "rules" button from JFrame1 it opens JFrame2 over JFrame1, and in JFrame2 there is a "close" button when it gets clicked the main form (i.e JFrame1) is lauched but it is launched over JFrame2.
The scenerio is JFframe1 -> JFrame2 -> JFrame1
Now my question is after clicking the "rules" button, JFrame1 should be closed and JFrame2 displayed on the screen and vice versa.
Assuming your button has an actionListener, after clicking the "rules button" put in:
JFrame1.dispose(); //Remove JFrame 1
JFrame2.setVisible(true) //Show other frame
And then reverese them for the opposite reaction
Somethig like this should be on the constructor or method which create JFrame2:
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//call another method in the same class which will close this Jframe
CloseFrame();
}
});
It's method which should close JFrame2
public void CloseFrame(){
super.dispose();
}
Well, if you already have a actionListener, you should add this:
JFrame1.dispose(); // This will close the frame
The JFrame1 is the name of your frame.
And if you want to open another frame that you have, add this:
JFrame2.setVisible(true); // This will put the other frame visible
I'm not an expert by any means, however, I ran into this problem as well. If you set your second JFrame to hidden, when you hit "Cancel", it will close the second JFrame.
//this is the code for the "cancel" button action listener
public void actionPerformed(ActionEvent e) {
setVisible(false);//hides the second JFrame and returns to the primary
this worked for me (Frame1 Called RegScreen and Frame2 Called MainScreen):
RegScreen.this.setVisible(false);
new MainScreen().setVisible(true);
Hope that this helps :) Regscreen was the original frame open at startup.
If this doesn't work, try this
JFrame1.dispose(); //Remove JFrame 1
JFrame2.setVisible(true) //Show other frame
JFrame2.setVisible(true);
this.dispose();
Have a MainClass with a main() method.
Have the MainClass that has the main() method encapsulate your JFrame1 and JFrame2 reference variables. Don't have JFrame1 or JFrame2 contain main() unless you have a specific reason.
After something is clicked in one of the JFrame objects, instantiate/make visible the other JFrame object and dispose of itself via your MainProgram.JFrame object methods.
Example:
//btn event inside 1st JFrame/window
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
MainProgram.openResultsForm(); //MainProgram opens 2nd window
MainProgram.queryEntryForm.dispose(); //MainProgam closes this,
//the 1st window
}
Let's say we have a JFrame frame that contains two JPanels, buttonPanel and dataPanel, and in this panel a single JButton button. When clicked, button creates and shows a JDialog dialog in its own window (as usual). Using several JTextFields and a submit button, the JDialog dialog creates a new Object dataObject encapsulating these input data. If we wish our dataPanel JPanel in the main application frame to display this dataObject, how should dataObject be appropriately passed to the JPanel residing in a foreign JFrame?
That was a mouthful even to me while writing it, so I'll attempt to clarify:
JFrame frame
JPanel dataPanel - meant to display data from Objects created in the JDialog
JPanel buttonPanel - contains a button to open the JDialog, into which some information will be entered and with said information our Object dataObject is constructed.
The goal here is to pass this dataObject (and it's constituent fields) to the dataPanel to be displayed. What is the most appropriate way to handle this? I considered keeping the Objects in dataPanel static and then calling a static method from the JDialog to add the new object, but it doesn't seem the proper thing to do.
Some guidance?
Much will depend on the structure of your program, including how the dialog is supposed to behave:
If the JDialog is modal and disappears when the submit button has been pushed, then the solution is easy -- extract the data from the dialog-related class after it returns which will be the line of code right after where you display the dialog. The dialog's submit JButton's listener could simply make the dialog no longer visible.
Otherwise if the JDialog is non-modal and disappears when the submit button has been pushed, then you may wish to attach a Listener to its Window, I believe a WindowListener, and then have your calling code extract the information when the listener indicates that the dialog has been closed or is closing.
Otherwise, if the JDialog is non-modal and is not supposed to become invisible when the submit button has been pressed but you need to update the calling program with new data, then I would have the calling class add a PropertyChangeListener onto the dialog-related class so that the dialog-related class can notify any listeners that submit has been pressed. This code would be in the dialog's submit JButton's listener.
I would give a dialog-related class a public DataObject getDataObject() method that the calling code can call once the dialog returns, allowing the class that displays the dialog to extract the pertinent information when needed.
Whatever you do, there is no reason to use static fields and many reasons not to. I strongly urge you to not even consider this.
For example of a modal dialog that returns:
// caveat: code has not been tested by compilation or running.
JButton myButton = new JButton(new AbstractAction("Show Dialog Button") {
public void actionPerformed(ActionEvent evt) {
MyDialogPanel myDialogPanel = new MyDialogPanel();
JDialog myDialog = new JDialog(myJFrame, "My Dialog",
ModalityType.APPLICATION_MODAL);
myDialog.add(myDialogPanel);
myDialog.pack();
myDialog.setLocationRelativeTo(myJFrame);
myDialog.setVisible(true);
// dialog now returns and we can get the data
// assuming that the wrapper object for your data
// is called "DataObject"
DataObject dataObject = myDialogPanel.getDataObject();
// and perhaps use it. Assume setDataObject is a method
// of the main GUI that displays the data object
setDataObject(dataObject);
}
});