I am having problems with retaining previous JFrame data when pressing a return button. Essentially, the return button is supposed to return the user to the previous JFrame which contains the user's shopping details (held in the mainscreen class), which is stored in a MySQL Database. However, since I am creating a new instance of the mainscreen class, all of the information regarding the user's shopping details are gone. I cannot seem to find the logic to solve this problem. This is what I've got so far, but the 'new' keyword creates a new instance which removes all the shopping details.
private void returnbtnActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose();
mainscreen mainscreen1 = new mainscreen();
mainscreen1.setVisible(true);
}
Related
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.
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).
Working on a GUI that is for allowing Customers to register their information on 1 GUI (Button click to add new customer) whereby after clicking the add button to register their information it will collect up all their data (forename, surname, address Line 1/2, city & postcode) and then upload it onto a JList in the previous GUI in which it displays the customer's first & last name. From which a user can then select that particular customer and click to update the customer's details in an new GUI.
The problem I'm having is trying to figure out how I can pass all the customer's data from the text-fields in the 2nd GUI and transfer them into the JList in the 1st GUI with only the first & last name being displayed. The 2nd problem I'm also facing is how to then open up another GUI which will then reverse the process when that particular customer has been selected so that changes can be made to their details.
In my Person class in the data-model package I have got the method getFullName which goes as follows:
public String getFullName()
{
String result = this.forename + " " + this.surname;
return result;
}
Is does anyone know how or could show me how I would be able to apply a solution to these problems?
I have tried experiementing with the getFullName method within the 2nd GUI upon pressing btnAdd, as shown below:
public void addItem()
{
String result = this.forename + " " + this.surname;
return result;
}
But alas I have had no luck.
Regarding your questions:
The problem I'm having is trying to figure out how I can pass all the customer's data from the text-fields in the 2nd GUI and transfer them into the JList in the 1st GUI...
There are usually two problems in this situation --
How to get the information held by the fields of add new customer dialog window (and yes, this should be a dialog window of some form, either a JDialog or a JOptionPane) into the parent window object, and
How to be notified exactly when this transfer of information should take place, and this is usually the more problematic of the two.
Again the first issue is usually easy to solve, since it is nothing more than a specific version of the more general problem of how to pass information between two classes. The most simple way to solve it is to have the parent window class call getter methods in the dialog window class to extract its state. For instance, the dialog window could have public getter methods for the data held in each JTextField, which the calling parent window could call, or the dialog class could have a getCustomer() method within which it gathers all the information held by its input fields, creates a Customer object, and then returns that object.
Perhaps a better and more robust way of transferring your data is to structure your program as a Model-View-Control or MVC type program, have your add new customer dialog window update the model when its submit button is pressed, and have the parent main window be notified by the model that its data has been changed, and so it knows that it must update its view of the data. This may be overkill for your program, and so I am not going to push this recommendation at this time, but in the future, when you are creating larger more complex programs, this is really what you will want to be doing since it will more easily allow your program's classes to have high cohesion (code that deals with the same things are close together) and low coupling (code that deals with unlike things are not tightly bound which decreases code complexity), making it much easier to debug and improve your program.
Again, the 2nd issue is more difficult to solve. If the dialog window is to close after obtaining and submitting the information that constitutes one customer, than the easiest solution to this is to make the new customer dialog window a modal JDialog or a JOptionPane (which in really is nothing more than a specialized modal JDialog). Why this helps is that Swing has a special mechanism for modal dialogs where it freezes code flow in the calling window starting immediately after setting the dialog visible. And so the calling code will always know when the dialog is no longer visible since its code's program flow will resume only when the dialog isn't visible. So you would want to extract the dialog window's data in the lines immediately following the one that sets the dialog or JOptionPane visible. And before you discard JOptionPanes as being too simplistic, understand that their second parameter, the one of type Object can take any Swing GUI component as a parameter, including a JPanel that holds a very large and complex GUI, making these tools extremely useful.
If the second dialog window will not be closed on the submission of a Customer and is to be left open, for instance if you want to leave it open so that the user can enter multiple Customers, then you'll likely want it to be a non-modal JDialog and will need to set up some kind of notification scheme so that the calling code will be notified when the user has submitted a Customer. One possible way is to allow the calling class to add an ActionListener to the dialog's submit button by giving the dialog class a public addActionListener(....) method. Another is to use Swing's PropertyChangeSupport and have the calling class add a PropertyChangeListener to the add customer dialog, and thus be notified of significant changes to its properties (i.e., that the submit button has been changed, or that a new Customer has been created). If you use an MVC design for your program, then the notification will be from the model to the view. In other words, the dialog will create the new Customer, the submit button will have the Control add the Customer to the Model, and the main GUI's Model listeners will be notified of the newly created object and will display it.
... how I can pass all the customer's data from the text-fields in the 2nd GUI and transfer them into the JList in the 1st GUI with only the first & last name being displayed
Your JList should be a JList<Customer> so that it holds a collection of complete Customer objects. You can easily change how the JList displays its data by overriding the Customer toString() method (not recommended since this method is more for debugging than for end user display), or better by assigning the JList a custom ListCellRenderer (recommended). The Swing JList tutorial will explain how to do this, and you can come back here with your code attempt if you're stuck at this step.
The 2nd problem I'm also facing is how to then open up another GUI which will then reverse the process when that particular customer has been selected so that changes can be made to their details.
This would be solved in largely the same way as above, except rather than creating a new Customer and adding it to the collection displayed by the JList, you would replace one Customer in the JList with the edited one.
For more detailed help, consider creating and posting a Minimal, Complete, and Verifiable Example Program. We don't want to see your whole program, but rather you should condense your code into the smallest bit that still compiles, has no extra code that's not relevant to your problem, but still demonstrates your problem.
you can pass firstname and last name or other data in main(args[]);
call you Jframe like this Yourclassname.main(new string[]{"firstname","lastname"});
let's know how would you like to set a data in JList.
each item should contains one entity or each item contains one entity property.?
to display you data nicely you have to use jtable instead.
About your two jframes one contains textfield for input other one display the data.
I wanna to idvice you to use a jpanel inside a joptionPane, like this code snippet :
private Customer customer;
private void newCustomer() {
JLabel labelCode = new JLabel("Customer Code *");
JTextField jTextFieldCode = new JTextField();
JLabel labelName = new JLabel("Customer Name *");
JTextField jTextFieldName = new JTextField();
JLabel labelPrincipalAdress = new JLabel("Principal Adress *");
JTextField jTextFieldPrincipalAdress = new JTextField();
JLabel labelSaleAdress = new JLabel("Sale Adress *");
JTextField jTextFieldSaleAdress = new JTextField();
JLabel labelActivity = new JLabel("Activity ");
JTextField jTextFieldActivity = new JTextField();
JLabel labelDiscountC = new JLabel("Discount Code *");
JLabel labelCountryC = new JLabel("Country Code *");
JComboBox<String> jComboBoxCountryC = new JComboBox<>(Locale.getISOCountries());
jComboBoxCountryC.setSelectedIndex(-1);
JPanel panel = new JPanel();
panel.add(labelCode);
panel.add(jTextFieldCode);
panel.add(labelName);
panel.add(jTextFieldName);
panel.add(labelPrincipalAdress);
panel.add(jTextFieldPrincipalAdress);
panel.add(labelSaleAdress);
panel.add(jTextFieldSaleAdress);
panel.add(labelActivity);
panel.add(jTextFieldActivity);
panel.add(labelCountryC);
panel.add(jComboBoxCountryC);
panel.setLayout(new GridLayout(8, 2, 1, 3));
for (Component c : panel.getComponents()) {
c.setFont(new Font("", Font.PLAIN, 14));
c.setPreferredSize(new Dimension(150, 25));
}
if (JOptionPane.showConfirmDialog(this, panel, "New Customer",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE) == JOptionPane.OK_OPTION) {
Customer newCustomer = new Customer();
newCustomer.setCode(jTextFieldCode.getText());
newCustomer.setName(jTextFieldName.getText());
newCustomer.setPrincipalAdresse(labelPrincipalAdress.getText());
newCustomer.setShippingAdresse(jTextFieldSaleAdress.getText());
newCustomer.setActivity(jTextFieldActivity.getText());
newCustomer.setCountry_code((String) jComboBoxCountryC.getSelectedItem());
MainFrame.customerSBRemote.addCustomer(newCustomer);
customersList.add(newCustomer);
this.customer = newCustomer;
jTableCustomer.changeSelection(customersList.indexOf(newCustomer), 0, false, false);
this.hashcode = customer.hashCode();
} catch (Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
}
I have 2 forms in Netbeans, 1 form will launch and the user will input information. Then if the user wants to, will open up the other jframe. The jframe that the user was working on will close. Now from the newly opened jframe, the user decides to go back to the previous frame. The problem is when the user does decide to go back to the previous frame, the text in the textboxes are gone, the buttons that were enabled are disabled, and the combobox selection resets. Does anyone know how I can save the state of all these elements? Thank you.
Would something like this help?
public class MyFrame extends JFrame{
private JCheckBox box1;
public MyFrame(){
box1 = new JCheckBox();
}
public void work(){
if(getbox().isSelected){
//work
}else{
//do else
}
}
public JCheckBox getbox1(){
return this.box1;
}
public JCheckBox setSelectedbox1(boolean set){
getbox1().setSelected(set);
}
If you want to keep that object alive you can use:
myFrame.setVisible(false)
myFrame.setVisible(true)
An application should generally only have a single JFrame.
You can probably use a Card Layout to swap different panels. Read the Swing tutorial on How to Use CardLayout for more information and example.
Other options or to use a JTappedPane or JInternalFrames. The tutorial also has section on these components.
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);
}
});