Hi all, I'm making a GUI for college, and I've to do some checks. Basically it's a competition entry form thing, and I've to check they entered a valid email ( checking for a @ (I realise it could still be invalid but this is what I'm asked for ) ), and the way I've created the GUI, they've to press buttons to pick a winner and runner up in a competition, I'm gonna run a check to make sure they didn't choose the same team for both.
How do I go about doing this? Do I make it so when they click the send entry button, I check all the different fields? Can anyone edit this bit of code to show me how it should be done? I'm thinking it should be done something like this..
else if (e.getActionCommand().equals("Enter Competition!"))
{
if(Wfield == RUfield)
{
JPopup("Error, the Wfield and the RUfield need to be different !");
}
if(email doesn't contain an @)
{
JPopup("Invalid email address entered!");
}
}
Is that how it should be done? When the button is clicked, then I do checks to see if all the fields are right? Or should it be done a different way? Sorry, it's my first week doing swing in college and so far haven't had a Java class so I'm trying to teach myself this
7 replies to this topic
#1
Posted 01 February 2012 - 03:09 PM
|
|
|
#2
Posted 01 February 2012 - 03:17 PM
This flow seems good to me. Why would you have doubts about it?
The only difference I could see is to check whether or not Wfield == RUfield AS the user chooses them. But I think your approach is as good as the latter.
The only difference I could see is to check whether or not Wfield == RUfield AS the user chooses them. But I think your approach is as good as the latter.
#3
Posted 01 February 2012 - 03:29 PM
lethalwire said:
This flow seems good to me. Why would you have doubts about it?
The only difference I could see is to check whether or not Wfield == RUfield AS the user chooses them. But I think your approach is as good as the latter.
The only difference I could see is to check whether or not Wfield == RUfield AS the user chooses them. But I think your approach is as good as the latter.
Thanks, I've a test next week worth 20% overall, and I'm just worried that my code isn't good enough to get an A since I haven't had a class yet, and havne't been given an opinion from anyone else yet. Thanks for your input, it really helped. Another thing is, is there a different way to do a check for the action listener, or please explain why ( if this is the right way ) I'm using it.
private JButton FWbutton;
FWbutton = new JButton("France #1");
FWbutton.addActionListener(this);
else if (e.getActionCommand().equals("France #1"))
{
Wfield.setText("France");
}
this is the code I'm using, basically what I get from this is, if the action command is equal to the text of the button? Can I not get it to check if FWbutton was pressed instead of checking if a button with "whatever text" was pressed? If so, please reply with code, thanks :)
#4
Posted 01 February 2012 - 03:40 PM
If I understand correctly you can use an anonymous inner class.
Instead of using so many else-ifs, AND if you're using JDK 7 you can use The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics) .
The main point is readability. If your code looks confusing and is hard to understand, change it.
Sometimes you need to write more complex code for performance, so you'll have to ask yourself what kind of code you want to write and why you're writing it.
Example:
FWbutton.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
Wfield.setText("France");
}
});
Instead of using so many else-ifs, AND if you're using JDK 7 you can use The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics) .
The main point is readability. If your code looks confusing and is hard to understand, change it.
Sometimes you need to write more complex code for performance, so you'll have to ask yourself what kind of code you want to write and why you're writing it.
#5
Posted 01 February 2012 - 04:48 PM
Quote
get it to check if FWbutton was pressed
e.getSource() == FWButton
#6
Posted 01 February 2012 - 05:09 PM
lethalwire said:
If I understand correctly you can use an anonymous inner class.
Instead of using so many else-ifs, AND if you're using JDK 7 you can use The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics) .
The main point is readability. If your code looks confusing and is hard to understand, change it.
Sometimes you need to write more complex code for performance, so you'll have to ask yourself what kind of code you want to write and why you're writing it.
Example:
FWbutton.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
Wfield.setText("France");
}
});
Instead of using so many else-ifs, AND if you're using JDK 7 you can use The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics) .
The main point is readability. If your code looks confusing and is hard to understand, change it.
Sometimes you need to write more complex code for performance, so you'll have to ask yourself what kind of code you want to write and why you're writing it.
Thanks, the code is now completely cleaned up, however I'm running a check to see if the winner field and runner up field are the same, and if so, display a popup, and it wont work ?
enter.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (Wfield == RUfield)
{
JFrame jf = new JFrame();
JOptionPane.showMessageDialog(jf,"Winner and Runner Up cannot be the same!");
}
}
});
And lastly, it's giving an error that there are unimplemented methods ( the ActionPerformed one, since I've moved them into the constructor of the frame ), however it still runs, is this okay or should I have atleast one method in the class and outside the constructor ?
---------- Post added at 01:09 AM ---------- Previous post was at 01:08 AM ----------
I've just taken out implements ActionPerformed isteand this cleared the error, but it still runs.
#7
Posted 01 February 2012 - 05:18 PM
JOptionPane.showMessageDialog([b][COLOR="#FF0000"]null[/COLOR][/b],"Winner and Runner Up cannot be the same!");
#8
Posted 01 February 2012 - 05:37 PM
Thanks, your reply in the last thread fixed this for me. Cheers !
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









