Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 12th, 2008, 5:04 AM   #1
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 358
Rep Power: 12 Fall Back Son is on a distinguished road
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();
            }
        });
    }
}
Fall Back Son is offline   Reply With Quote
Old Jul 12th, 2008, 5:07 AM   #2
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 358
Rep Power: 12 Fall Back Son is on a distinguished road
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
Fall Back Son is offline   Reply With Quote
Old Jul 13th, 2008, 4:44 PM   #3
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 358
Rep Power: 12 Fall Back Son is on a distinguished road
Re: GUI problem.

come on lol. no one?
Fall Back Son is offline   Reply With Quote
Old Jul 21st, 2008, 1:57 AM   #4
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 358
Rep Power: 12 Fall Back Son is on a distinguished road
Re: GUI problem.

for real lol. i've waited a week for any help and i help plenty of people here. please?

Fall Back Son is offline   Reply With Quote
Old Jul 21st, 2008, 8:41 PM   #5
The Dark
Programming Guru

 
Join Date: Jun 2005
Posts: 1,631
Rep Power: 15 The Dark will become famous soon enoughThe Dark will become famous soon enough
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.
The Dark is offline   Reply With Quote
Old Jul 21st, 2008, 9:37 PM   #6
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 358
Rep Power: 12 Fall Back Son is on a distinguished road
Re: GUI problem.

Quote:
Originally Posted by The Dark View Post
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.
So you are saying that I should do a println to print out the type of PropertyChangeEvent that is occurring? That seems reasonable, but if that is the case, I don't see why that problem didn't occur anywhere else in my program. Also, I grabbed this code off of the Java tutorials and only made small modifications so maybe those messed something up. I dunno.
Fall Back Son is offline   Reply With Quote
Old Jul 21st, 2008, 11:07 PM   #7
casesensitive
0000 0101 0011 1001
 
Join Date: Oct 2007
Location: Upstate, SC
Posts: 71
Rep Power: 11 casesensitive is on a distinguished road
Send a message via MSN to casesensitive
Re: GUI problem.

Quote:
Originally Posted by Fall Back Son View Post
So you are saying that I should do a println to print out the type of PropertyChangeEvent that is occurring? That seems reasonable, but if that is the case, I don't see why that problem didn't occur anywhere else in my program. Also, I grabbed this code off of the Java tutorials and only made small modifications so maybe those messed something up. I dunno.
Its not actually your porgram as stated.

Perhaps you should re-read the tutorial and ask questions about it that you don't understand.

Quote:
So why does it display automatically as soon as the thing is started?
Cause you have it programmed to do so...

Im just messing with ya btw...
casesensitive is offline   Reply With Quote
Old Jul 22nd, 2008, 12:55 PM   #8
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 358
Rep Power: 12 Fall Back Son is on a distinguished road
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.
Fall Back Son is offline   Reply With Quote
Old Jul 22nd, 2008, 1:12 PM   #9
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 358
Rep Power: 12 Fall Back Son is on a distinguished road
Re: GUI problem.

Quote:
Originally Posted by The Dark View Post
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.
That is a good thought, but I'm pretty sure the change in that case has to be user generated. But I'll keep that in mind.
This is the "source" that caused the property change event. I don't know what it is really. lol. Looks like you were right though.

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]
Fall Back Son is offline   Reply With Quote
Old Jul 22nd, 2008, 6:32 PM   #10
The Dark
Programming Guru

 
Join Date: Jun 2005
Posts: 1,631
Rep Power: 15 The Dark will become famous soon enoughThe Dark will become famous soon enough
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.
The Dark is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Challenging Programming Problem - "Pinball Ranking" Sane Coder's Corner Lounge 38 Jan 15th, 2008 6:16 PM
Problem solving ReggaetonKing Software Design and Algorithms 7 Jan 4th, 2008 2:49 PM
Storing BLOBs in a database - problem jonyzz Other Programming Languages 8 Jan 31st, 2007 5:38 AM
Changing icons problem Pedja C# 8 Mar 25th, 2006 9:03 AM
cgi/perl script + IE problem joyceshee Perl 2 Jan 24th, 2006 12:10 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:05 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC