Data transfer between Java Swing forms - java

I have 5 forms, one of which opens when the other is closed. 2nd member login screen. The last one is the ticket purchase screen. Reservations are made on the ticket purchasing screen. Member information is kept in the database. If the e-mail and password on the login screen match, login with those in the database. I want to access the current user in the last form.
Unfortunately, I did not take action for this from the beginning. Now, if the login is successful, I can take the user's e-mail and forward it to the last form. I can access the member id from this email and add the member information in the final reservation process.
I cannot forward your member email to the last form! That's the problem. I've done a lot of switching between forms, but I can only do this with forms that are opened and closed consecutively. If I create an object with the constructor and type setVisible(true) in the following way, the email is transferred, but the last form opens after the 1st form. If I remove the setVisible method or make false, even if the data is transferred at the last part, I cannot use it because I cannot print it on the label.
private void btnMemberEnterActionPerformed(java.awt.event.ActionEvent evt) {
BiletAl pencere2 = new BiletAl(txtUyeEPosta.getText(),lbl.getText());
pencere2.setVisible(true);}
public class BiletAl extends javax.swing.JFrame {
public BiletAl() {
initComponents();
}
public BiletAl(String gelenEPosta,String lbl) {
initComponents();
System.out.println(gelenEPosta);
lblGelenEposta.setVisible(true);
lblGelenEposta.setText(gelenEPosta);
}
If this happens: the form opening order is broken. but "nil" text, i.e. the entered e-mail is transferred to the label:
private void btnMemberEnterActionPerformed(java.awt.event.ActionEvent evt) {
BiletAl pencere2 = new BiletAl(txtUyeEPosta.getText(),lbl.getText());
}
If the first part changes just like this:Switching between forms is correct, but even if the data coming as constructor parameter with System.out.println is correct, it does not add it to the label when setLabel. The label does not write "nil" in the incoming email.

Then try to use this commands:
1)this.setVisible(true);
2)this.setVisible(false);

Related

Creating a Log-In Screen with Validation?

I'm creating a Java log-in screen for a scheduling program that uses one-way encryption. The program should work as follows:
If it's the first time opening the program, ever, a window will show up asking the user to enter a username and password, as well as a security question. This first time, the password will not be hidden by black circles. Instead, it will be shown in plain text.
Then, the three fields will be encrypted using a caesar/shift cipher of 3 spots, and the 'encrypted' values will be saved into a file called info.txt in the source folder of the program.
The user will then be redirected to a log-in page, where they will re-enter their username and password to log in. Here, the password field is fixed so that the letters show up as black circles(not sure what this is called). A new int variable called loginAttempts is declared and initialized with the value 0.
After the user hits 'log-in', the system will use the same encryption method for the log-in details entered and compare them to the encrypted information in the text file. If both values match, then the user will be re-directed to the main screen of the program, having been granted access.
If not, then loginAttempts increments. After the 5th time, this happens, the user will be asked their security question. If the answer this wrong the first time, they will be booted from the program.
I have a few questions, as I'm having trouble creating this.
The dialog boxes I'm using to produce error messages are very confusing, and not seeming to work. I got the code to use them straight from the Oracle website and I'm still having trouble-- NetBeans has informed me that it 'cannot find symbol' for showInputDialog(the method I'm using to display the error message), but it's not suggesting that I'm missing any imports.
String errorMessage = "Security Question: What was your favorite class in college?";
String s = (String) JTextField.showInputDialog(
null,
errorMessage,
"ERROR",
JOptionPane.PLAIN_MESSAGE,
JOptionPane.WARNING_MESSAGE,
null,
securityAnswer);
The part that's given me an error is the word 'showInputDialog' itself, and I'm unsure why..? I would like for this particular dialog to appear with a text field in it so that the user can enter the answer to the security question in there. However, the code doesn't even seem to be working.
On the Oracle page, this is the code that is given that is supposed to show a dialog box with an input field:
String s = (String)JOptionPane.showInputDialog(
frame,
"Complete the sentence:\n"
+ "\"Green eggs and...\"",
"Customized Dialog",
JOptionPane.PLAIN_MESSAGE,
icon,
null,
"ham");
//If a string was returned, say so.
if ((s != null) && (s.length() > 0)) {
setLabel("Green eggs and... " + s + "!");
return;
}
//If you're here, the return value was null/empty.
setLabel("Come on, finish the sentence!");
But the word frame creates an error in the Netbeans GUI, and I'm not sure why it doesn't work. All I want is to create a text box that looks like this:
but I don't know how to do that in a way that would work.
The way that I've set up my program, I've made multiple jFrame components(different classes), and I've set it up so that if you were to try and access a different frame, the frame you were currently on would turn invisible. For example:
(a user is on the main screen, and they click on a button that says 'Add Appointment.')
addAppointment a = new addAppointment();
a.setVisible(true);
a.setAlwaysOnTop(true);
this.dispose();
And this works for literally all of my program except for the log-in page!
public createUserPass() throws IOException {
String path="\\src\\Schedulemanager\\pkg\\info.txt";
file = new File(path); //creates new File
if (file.exists()) { //make a new file if it's not already existent
logIn loginPage = new logIn();
loginPage.setVisible(true);
loginPage.setAlwaysOnTop(true);
this.dispose();
this.setVisible(false);
} else {
file.createNewFile(); //creates the new file
initComponents();
}
}
This is the 'main' class in the program that will run if the .jar file is clicked. What it's supposed to do is check if the info.txt file exists. If it does, then obviously the user has already set login information, and it should close itself and open an instance of the logIn class, which is the jFrame that handles the log-in functions once the information has been set. However, it doesn't work! It opens the new logIn class, but it doesn't dispose of the original one where you set the information, to begin with.
It's crucial that the original jFrame is hidden so that the user can't just set a new password every single time they run the program! How can I fix this?
Please let me know if I need to clarify anything.

Prevent user to move to next text field unless valid input is entered [duplicate]

This question already has answers here:
What is a simple way to create a text field (or such) that only allows the user to enter ints/doubles in Java?
(3 answers)
Closed 7 years ago.
I am developing a Java Swing Application.
The application ends with a form to take the customers information (name, last name, email, gender, etc)
I am trying to validate the text fields in a way that when the application is launched focus will go on the first text box (first name). I want the user to not be able to leave the box until they have entered a valid name (no numbers, punctuation and so on).
So far I am using JOptionPane.showMessageDialog(null, "") to output messages to the user, and the textfields are rigged with lost focus listeners.
Unfortunately even after looking online, I am not able to find a solution to validating these boxes.
Action Plan:
Focus on first textfield >>> Unable to leave unless valid input is entered >> messages outputted to user >> validation completed >> focus set on next field >> and so on.
Help would be appreciated.
Try something along the lines of the following code for listener
class CustomFocusListener implements FocusListener
{
#Override
public void focusGained(FocusEvent e)
{
}
#Override
public void focusLost(FocusEvent e)
{
validateText();
if(validateFailed) {
myJTextField.grabFocus();
}
}
}
As for upon launch just try
myFirstJTextField.grabFocus();
Look into JComponent.grabFocus() for more information.

GWT: How can i get the updated values of textbox which i am making dynamically

I am creating a TextBox dynamically ,I get the values from server ,values could be 5,some times 10 etc , so No of textboxes i create will be different..
After all textboxes created .
There is an update button in page , When i click on this update button , Whatever changes user may have entered in any of the textBox,should Need to go to the server .. That I am not able to do ..
below is the code where i create the textBoxes
public void fetchData(){
public void onSuccess(ArrayList<Details> result) {
for(int i =0;i<result.size();i++){
name = new TextBox();
name.setText(result.get(i).getName());
verticalPanel.add(name);
namesList.add(name);
}}
Suppose name value is at this time : admin
Now user goees to the UI and change admin to adminNew
then click update button
Here what i do on update Button
public void Update(){
for(int i =0;i<namesList.size(); i++){
String updatedNanme = namesList.get(i).getText());
}
}
Now how will I get the updated name which user have changed from UI(i.e adminNew), in updatedName field.
Right now i am getting the old name(i.e admin) which i got from fetchData Method.
thanks
You should get textbox's value by calling getText(), not the "old value". I think there's a problem with your textbox list. maybe instantiate the textbox two times!
I suggest you debug your code, put a breakpoint on when textbox is instantiated and when the update occures. See if both are the same instance (for example if you're using eclipse watching a variable, id of the variable is shown in front of it. this is the object's id for VM. check if id of textbox that was instantiated is the same as id of textbox you are getting value from)
Could it be possible that you override the content of text box with the user entry ? Your update method will always set updatedNanme to the content of the last element from your namesList array.
And how do you access the updatedNanme string ? This string is only available in the for loop ?

How to access the variables inside a class which has been called more than once as a tab in JTabbedPane (Java)

I am creating a java app.
I have one class q2a2 which is a jpanel whose design is shown as follows: -img-
Suppose if an item is selected from the combo-box and "Create Account" button is clicked. One tab is added to the jTabbedPane. every item has a common tab. so what i did is created one class and adding that everytime on button click. The class name is q2a2_add. This is a panel as well. The image for this is as follows...
After having some three items the app looks like
The code for this is:
public void addclass(int a) {
if(jTabbedPane1.getTabCount()<13) { //variable name of TabbedPane
String s=(String) mainCB.getItemAt(a); //mainCB is the variable name of main combobox
int dont=0;
for(int j=0;j<tabname.length;j++){ //just to ensure two accounts should not be same
if(s.equals(tabname[j])){
dont=1;
break;
}
}
if(dont==0){
for(int j=0;j<12;j++) {
if(index[j]==0){
q2a2_add subpanel2=new q2a2_add(this); //calling the second class
jTabbedPane1.add(s,subpanel2); //here adding panel
subpanel2.heading(s); // heading() method is defined in q2a2_add() which rename the jTextField to be same as argument s;
tabname[j]=s;
index[j]=1;
break;
}
}
}
else {
JOptionPane.showConfirmDialog(null, (String) mainCB.getItemAt(a)+" is already created","Information", JOptionPane.PLAIN_MESSAGE);
}
}
else {
JOptionPane.showConfirmDialog(null, "Account Overload. Delete wrong account and then create","Caution", JOptionPane.PLAIN_MESSAGE);
}
}
Now my question is. As seen in the function. everytime same class has been called and added. How can i access the various comboboxes and textboxes in different tabs. I want to store and opearate values entered by the user. Like for example- how to read inputs from Accounts Receivable, Accounts Payable and Office Supplies differently.
Please reply.
I would expose the functionality that you require within your q2a2_add class. For instance, if you want to change the textbox value, add a function inside the q2a2_add class called setTextBoxValue() that takes a String parameter. Inside that function, you can set the textbox value. The same goes for retrieving information from it. The only remaining problem is how to keep track of the different tabs. What I would recommend (which may simplify what you already have) is to create a HashMap which maps String types to q2a2_add types. Then, when you want to add a new tab panel, you can just check if the String exists in the HashMap instead of searching through to check the titles. If it doesn't exist, you can add it to a HashMap stored inside of your outer JPanel class. Then, when you want to access the tabpanels, you can simply access them by string inside the HashMap and get/set their properties as you please.

How to process forms in Java?

Using Swing in Java I wrote a form containing radio buttons, text fields and so on. In the very end I have a "Submit" button.
Now I want to "send" the information given by the user to the program. How do I do that? Is there a good tutorial about that?
Is it kind of similar to PHP? (I am asking just because I know how to do it in PHP. To avoid confusions I probably need to mention that I do NOT program a web application).
Processing data in Swing is way different from the typical web REQUEST/RESPONSE paradigm.
To Take something you may know, it's more in the fashion of Javascript actions in an HTML page : each time user performs an operation, one or more events are sent, and the application developper can update application content according to it.
In your case, if you register an ActionListener to the button, it will be called each time button is clicked. You'll then have the possibility to perform any operation you want.
But that's not all !
Each time a component is keyboard focused, or receives the mouse, events are sent, as well as when a key is stroked or when widget's model is updated.
I would really suggest you to read documents such as Swing tutorial (which dives in greater details than I could do in 1 month).
Not completely sure what you mean by "send to the program". You are in the program so I assume that you have a dialog that renders this form? Just pass the dialog the object that you want to use to store the data. For example, your dialog's constructor can take an argument.
public class MyDialog extends JPanel {
private UserInfo userInfo;
private JTextField name;
/**
* The main area of the dialog.
*/
protected JPanel panel;
public MyDialog(UserInfo userInfo) {
this.userInfo = userInfo;
}
public showDialog() {
// Some code to create the form which it looks like you already know how to do
// Create a name field
JLabel nameLabel = new JLabel("Name:");
panel.add( nameLabel );
JButton submit = new JButton("Submit");
submit.addActionListener( new ActionListener() {
public void actionPerformed (ActionEvent event)
{
this.userInfo.setName(name.getText().trim());
} } );
panel.add( submit );
}
}

Categories

Resources