![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: Oct 2006
Posts: 257
Rep Power: 2
![]() |
GUI problem.
This class is contained within a larger GUI. The larger GUI just consists of a window with several options. Clicking on one of the options calls the method in this class which fires up this classes GUI. There is a problem: when I click on the button which fires up this classes GUI, when the GUI is displayed, it ALREADY has the text "Invalid Team name: re-enter" in the text field. As you can see, there is supposed to be an event before this happens. My intention is that the text will only be displayed if indeed the user enters an invalid team name. So why does it display automatically as soon as the thing is started?
public class SetManualMatch extends JPanel
implements PropertyChangeListener{
// Strings to get the text input to the text field
private String team1;
private String team2;
//Labels to identify the fields
private JLabel teamLabel1;
//Fields for data entry
private JFormattedTextField teamField1;
private JFormattedTextField teamField2;
public SetManualMatch() {
super(new BorderLayout());
//Create the labels.
teamLabel1 = new JLabel("vs.");
//Create the text fields and set them up.
teamField1 = new JFormattedTextField();
teamField1.setText("");
teamField1.setColumns(20);
teamField1.addPropertyChangeListener("value", this);
teamField2 = new JFormattedTextField();
teamField2.setText("");
teamField2.setColumns(20);
teamField2.addPropertyChangeListener("value", this);
//Tell accessibility tools about label/textfield pairs.
teamLabel1.setLabelFor(teamField1);
//Lay out the labels in a panel.
JPanel labelPane = new JPanel(new GridLayout(0,1));
labelPane.add(teamLabel1);
//Layout the text fields in a panel.
JPanel fieldPane = new JPanel(new GridLayout(0,1));
fieldPane.add(teamField1);
fieldPane.add(teamField2);
//Put the panels in this panel, labels on left,
//text fields on right.
setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
add(labelPane, BorderLayout.CENTER);
add(fieldPane, BorderLayout.LINE_END);
}
/** Called when a field's "value" property changes. */
public void propertyChange(PropertyChangeEvent e) {
Object source = e.getSource();
if (source == teamField1) {
team1 = teamField1.getText();
scrabble.Team theTeam = new scrabble.Team(team1);
int index = scrabble.Matchups.allTeams.indexOf(theTeam);
if (index == -1)
{
teamField1.setText("Invalid team name. Re-enter.");
}
else
{
scrabble.Matchups.manualMatched.add(scrabble.Matchups.allTeams.get(index));
scrabble.Matchups.allTeams.remove(index);
}
} else if (source == teamField2) {
team2 = teamField2.getText();
scrabble.Team theTeam2 = new scrabble.Team(team2);
int index = scrabble.Matchups.allTeams.indexOf(theTeam2);
if (index == -1)
{
teamField1.setText("Invalid team name. Re-enter.");
}
else
{
scrabble.Matchups.manualMatched.add(scrabble.Matchups.allTeams.get(index));
scrabble.Matchups.allTeams.remove(index);
}
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event dispatch thread.
*/
public static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Match these two teams");
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
//Add contents to the window.
frame.add(new SetManualMatch());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//Turn off metal's use of bold fonts
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
}
});
}
} |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Oct 2006
Posts: 257
Rep Power: 2
![]() |
Re: GUI problem.
I'm very tired and its late so I hope that was clear. Basically there are two GUIs. One is the main GUI which you can click on. Clicking on it can cause this classes GUI to open up. When this classes' GUI is displayed, it should NOT say "Invalid team name: re-enter" in the text field. However, it does for some reason. I'm trying to figure out why this is possible, since there shouldn't have been any type of Event yet, and the code that causes that message is contained within the code for the Event. Maybe I am just missing some key aspect of GUI programming, I started this project a long time ago when I sorta knew what I was doing - now I am at a loss as to what is happening.
thank you |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Oct 2006
Posts: 257
Rep Power: 2
![]() |
Re: GUI problem.
come on lol. no one?
|
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Oct 2006
Posts: 257
Rep Power: 2
![]() |
Re: GUI problem.
for real lol. i've waited a week for any help and i help plenty of people here. please?
![]() |
|
|
|
|
|
#5 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 852
Rep Power: 4
![]() |
Re: GUI problem.
I'd say that the problem is that the property that is changing isn't the value - it is one of the other properties (probably the "focusable Window state", but it could be the font, or the foreground or background colour).
Probably the easiest way to check is to pop up (or log) a message with the property name when you get the property change event. Then you can just do your checking code when the right property change comes through. Note that popping up a message inside the property change handler might cause a property change itself, which might then call the handler again. |
|
|
|
|
|
#6 | |
|
Professional Programmer
Join Date: Oct 2006
Posts: 257
Rep Power: 2
![]() |
Re: GUI problem.
Quote:
|
|
|
|
|
|
|
#7 | ||
|
Programmer
Join Date: Oct 2007
Posts: 41
Rep Power: 0
![]() |
Re: GUI problem.
Quote:
Perhaps you should re-read the tutorial and ask questions about it that you don't understand. Quote:
Im just messing with ya btw... |
||
|
|
|
|
|
#8 |
|
Professional Programmer
Join Date: Oct 2006
Posts: 257
Rep Power: 2
![]() |
Re: GUI problem.
Obviously, as it is programmed, that is what it does. But it is not intentionally programmed to do so. If you look at where the statement that prints it is, it would only be printed if there was a PropertyChangeEvent. And I already have done a decent amount of GUI work and I already understand the concept how the code works. Just go away if you aren't going to help. Lol.
|
|
|
|
|
|
#9 | |
|
Professional Programmer
Join Date: Oct 2006
Posts: 257
Rep Power: 2
![]() |
Re: GUI problem.
Quote:
javax.swing.JFormattedTextField[,0,0,224x20,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@e80a59,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=20,columnWidth=11,command=,horizontalAlignment=LEADING] |
|
|
|
|
|
|
#10 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 852
Rep Power: 4
![]() |
Re: GUI problem.
Don't print out the source - you already know that it is teamField1, use the getPropertyName() method on the PropertyChangeEvent to get the property name.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Challenging Programming Problem - "Pinball Ranking" | Sane | Coder's Corner Lounge | 38 | Jan 15th, 2008 5:16 PM |
| Problem solving | ReggaetonKing | Software Design and Algorithms | 7 | Jan 4th, 2008 1:49 PM |
| Storing BLOBs in a database - problem | jonyzz | Other Programming Languages | 8 | Jan 31st, 2007 4:38 AM |
| Changing icons problem | Pedja | C# | 8 | Mar 25th, 2006 8:03 AM |
| cgi/perl script + IE problem | joyceshee | Perl | 2 | Jan 24th, 2006 11:10 AM |