Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 33

Thread: Newbish Question...(s)

  1. #1
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Newbish Question...(s)

    So I'm using the code below to to make a frame with radio buttons and what not. I've pretty much just modeled this after some code in my programming book so it looks weird. =/
    I've implemented this into my other code so that it displays when I need to prompt the user for a response, which I originally got via scanner. I want this to update the users response automatically with a preset response based by selection of one of the radio buttons. As you can see I've done this in itemStateChanged method. But my problem is the program won't stop and wait for the user to give a response via selecting a radio button. What kind of options do I have to make it wait for that response? I honestly have no clue what to do? I could say if the response == 0 instead of 1 2 or 3 then pause somehow? but then how would it know to go again?

    On a side note, I'm using ItemListener and fonts and ItemEvent that change the text above. Which is completely useless to me for what I'm trying to accomplish.. -.- It was part of the other program I modeled this after.
    But I decided to use them since the font is an Item I guess? That works with the stuff mentioned above. Soo.. any hints or samples I could use to pass the response from the radio buttons to the itemStateChanged method correctly? Can I create other kinds of objects like this: loginFont = new Font("Serif", Font.PLAIN, 14); with string or something different? cause fonts is a little ridiculous.
    .. I swear I had another question. But it's not coming to me right now..

    edit: Oh yes, how can I ensure the frame closes when I'm done with it? What kind of code is there to do this?

    Thanks for any help! =)

    import java.awt.*;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import java.awt.event.*;
     
    import javax.swing.*;
     
    public class NewLogin extends JFrame
    {
    	private Font loginFont;
    	private Font registerFont;
    	private Font exitFont;
    	private JTextField textField;
    	private JRadioButton loginButton;
    	private JRadioButton registerButton;
    	private JRadioButton exitButton;
    	private ButtonGroup radioGroup;
     
    	Login Login = new Login();
     
    	public NewLogin()
    	{
    		super("Super Game");
    		setLayout(new FlowLayout()); // set frame layout
     
    		textField = new JTextField("Which action would you like to perform?",
    				30);
    		add(textField); // add textField to Jframe
     
    		// create radio buttons
    		loginButton = new JRadioButton("Login", false);
    		registerButton = new JRadioButton("Register", false);
    		exitButton = new JRadioButton("Exit", false);
    		add(loginButton);
    		add(registerButton);
    		add(exitButton);
     
    		// create logical relationship between JRadioButtons
    		radioGroup = new ButtonGroup(); // create button group
    		radioGroup.add(loginButton);
    		radioGroup.add(registerButton);
    		radioGroup.add(exitButton);
     
    		// create font objects
    		loginFont = new Font("Serif", Font.PLAIN, 14);
    		registerFont = new Font("Serif", Font.BOLD, 14);
    		exitFont = new Font("Serif", Font.ITALIC, 14);
     
    		// register events for JRadioButtons
    		loginButton.addItemListener(new RadioButtonHandler(loginFont));
    		registerButton.addItemListener(new RadioButtonHandler(registerFont));
    		exitButton.addItemListener(new RadioButtonHandler(exitFont));
     
    	}// end RadioButtonFrame
     
    	private class RadioButtonHandler implements ItemListener
    	{
    		private Font font;
     
    		public RadioButtonHandler(Font f)
    		{
    			font = f;
    		}
     
    		public void itemStateChanged(ItemEvent event)
    		{
    			textField.setFont(font);
    			if (font == loginFont)
    			{
    				Login.response1 = 1;
    			}
    			else if (font == registerFont)
    			{
    				Login.response1 = 2;
    			}
    			else if (font == exitFont)
    			{
    				Login.response1 = 3;
    			}
    		}
     
    	}// end RadioButtonHandler
    }// end NewLogin


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Newbish Question...(s)

    Is there a Login class and a class with a main method so your code could be compiled and executed?

    problem is the program won't stop and wait for the user to give a response via selecting a radio button.
    Display the GUI with the radio buttons and exit. The RB need to have listeners.
    When the user selects a RB the listener will be called.
    Last edited by Norm; May 27th, 2011 at 08:36 PM.

  3. #3
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: Newbish Question...(s)

    1. yes of course I'm just trying to switch it over to a gui
    2. will putting in a listener stop the rest of the program from running until it gets a response? or do I need to loop it in the background somehow until its got a working answer?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Newbish Question...(s)

    No, listeners do not stop the program.
    You need to show the GUI to the user with listeners set for all the buttons etc and then exit the method that built the GUI. You do NOT put in a loop to wait for the answer. The JVM does that for you. When the user presses/selects/clicks on something, the JVM will call your listener and then your code reacts to what the user did.

  5. #5
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: Newbish Question...(s)

    hmm.. well wont this change the user response like i want? without listeners? because the JVM definitely isn't stopping the rest of my program while its displaying this GUI.
    public void itemStateChanged(ItemEvent event)
    		{
    			textField.setFont(font);
    			if (font == loginFont)
    			{
    				Login.response1 = 1;
    			}
    			else if (font == registerFont)
    			{
    				Login.response1 = 2;
    			}
    			else if (font == exitFont)
    			{
    				Login.response1 = 3;
    			}
    		}

    I guess I need to break up my program into more methods so that the rest of the program doesn't keep going until i specifically call its method via listeners or changes in the itemStateChanged
    Cause like I said before, it was only stopping before to get input via scanner. then it would load the rest of the information immediately after as part of the process. Login.response1 is the integer I was adjusting with scanner before to know what the user chose.
    Sorry if my logic seems illogical, this is my first real attempt at working with a GUI, aside from dialog boxes.
    Last edited by Hallowed; May 27th, 2011 at 09:22 PM.

  6. #6
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: Newbish Question...(s)

    Oh and how would I specifically exit a method like the one that called the GUI Without coming to the actual end of the method }?
    Cause as far as I can tell, if a method calls a method (say method1 calls method2), it just opens that method (method2) and stops(method1) until that method(method2) ends and then it goes back and finishes the rest of the method(method1). Right?

    I'm trying to figure out how I could use DISPOSE_ON_CLOSE to end it somewhere in here?
    if (font == loginFont)
    			{
    				Login.response1 = 1;
    			}
    			else if (font == registerFont)
    			{
    				Login.response1 = 2;
    			}
    			else if (font == exitFont)
    			{
    				Login.response1 = 3;
    			}
    But I don't know how to make it work?
    __________________________________________________ ___________________
    Edit:
    Well I got it to mostly work so far..
    But I still don't know how to make those stupid extra frames close when I'm done with them..
    Here Is What It Looks Like
    Also if you try it out, you will notice that the program loads in the top left corner.. I'm sure there is a way to get it to load in the middle but I can't find code for it?
    And another thing. since I'm going to be working on it soon... Is there a way to make a method reload/redisplay from switching tabs? I want
    to display different information on different tabs, but can they be re-loaded somehow just from switching the tabs?
    Last edited by Hallowed; May 28th, 2011 at 03:41 AM.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Newbish Question...(s)

    Is there a Login class and a class with a main method so your code could be compiled and executed?

  8. #8
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: Newbish Question...(s)

    Quote Originally Posted by Norm View Post
    Is there a Login class and a class with a main method so your code could be compiled and executed?
    Yup. Look at the link I posted above its a jar file with working so far.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Newbish Question...(s)

    There are no .java source files in the GameGUI.jar file.

    I'm not going to run anyones jar file without the source.

    When I tried to execute your jar file in a Secure environment I get this:
    Exception in thread "main" java.security.AccessControlException: access denied (java.security.AllPermission <all permissions> <all actions>)
    at java.security.AccessControlContext.checkPermission (Unknown Source)
    Why does your code try to get ALL Permissions?
    Reduce the required permissions required to exactly what is needed.
    Last edited by Norm; May 28th, 2011 at 09:06 AM.

  10. #10
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: Newbish Question...(s)

    Sorry here is the source
    Source
    I have no idea if I'm setting this up 'properly' and I can't figure out why after you login it wont load the next window. It's just white on my computer. =/
    As far as permissions go I have no clue how to even manage that stuff. I just told eclipse to make me a jar.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Newbish Question...(s)

    after you login it wont load the next window
    What is the next piece of code that is executed after you login? Trace the logic flow from that point. Add printlns if you don't have an interactive debugger.

  12. #12
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: Newbish Question...(s)

    It heads over to CheckInfo, there it sees that it needs more info so it loads up NewCheckInfo class. But that class just freezes. I added println statements like you said and it will go until the end of public NewCheckInfo(int aP) method. But when it doesn't ever seem to get to public void actionPerformed(ActionEvent event). And thats because the user never enters information. Which is because the screen is just white and you cant! =/ so I have no clue why its doing that all of the sudden. Is there a problem with having to many methods inside methods with frames and what not? I don't really see how I could do it a different way..

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Newbish Question...(s)

    But that class just freezes
    What code called the NewCheckInfo constructor ( = new ...)
    What happens after the constructor returns?

  14. #14
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: Newbish Question...(s)

    NewCheckInfo is called near the beginning of CheckInfo... Oh i forgot i changed it just to test something. I told it to call the login stuff again in CheckInfo lines 29-32. Before I did that it use to be:
    So you could just replace 29-32 with this code, to see what I'm trying to do.
    And its not returning anything specifically? I was just going to have it call another method once it got done. and move onto the next frame and what not.
                                    NewCheckInfo newCheckInfoFrame = new NewCheckInfo(aP);
    				newCheckInfoFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    				newCheckInfoFrame.setSize(400, 100); // set frame size
    				newCheckInfoFrame.setVisible(true); // display frame

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Newbish Question...(s)

    You've posted way too much code to look thru. I prefer working with a single source with ALL the code in one file.
    And now I see that what is posted won't work.

    The only thing I can suggest is to continue working thru the execution path until you find the problem.

    If you find a problem and can create a small program that executes and demonstrates the problem post it with your questions.

  16. #16
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Newbish Question...(s)

    I could probably help if the OP could post the relevant source code (the 'Source' link previously posted shows no code), and explain clearly what the problem is.

  17. #17
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: Newbish Question...(s)

    Source

    Alrighty first off here is this for fun:

    Anyway here is how the program flows:
    The user will first be given an option of what to do: Login.java -> LoginP1()
    LoginP1() will load the Gui from here - > NewLogin.java
    Once NewLogin receives a response via TextFieldHandler implements ActionListener in NewLogin.java
    NewLogin.java will load a method based on the user selection. i.e. Register will load Login.LoginP3();
    This is how pretty much I'm doing all the GUI and flow of the program, because I don't know a better way to do it .

    My problem however is this: Once the user logs in for the first time after successfully creating an account, it will need even more information from them so just like I did before I tried to load another Frame/GUI to collect the input. But this time it just freezes. even though I did the exact same things I've been doing before. This is where this occurs:
    See: CheckInfo.java -> Running(int aP) lines 29-32
    Then see: NewCheckInfo.java I put in print statements that do print all the way up until the end of the NewCheckInfo(int aP) method. But after that the program just freezes and displays a white GUI box.. No clue why? and I don't really know what my alternatives may be to setting up my GUI.
    Last edited by Hallowed; May 29th, 2011 at 01:09 PM.

  18. #18
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: Newbish Question...(s)

    Also, I'd really like to know how to make the other frames disappear once I've collected the Users response. Any ideas how to set it up like this?

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Newbish Question...(s)

    Have you tried using a modal dialog window to get data vs a Frame?

    The program freezing may be some conflict with the way you executing code on the Swing event display thread (EDT).

  20. #20
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: Newbish Question...(s)

    Yes, a modal dialog windows would works just fine. But they are so ugly and unattractive with so minimal customization that it kind of defeats the point of making a GUI in the first place. Plus I wanted to be able to control the users response via GUI so that when I'm asking for menu options, they don't have to put in things like 1 or 2 or 3 as a selection, and then have to try catch everything just to make sure they don't put in a character etc.

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Newbish Question...(s)

    hey are so ugly
    Did you use the JDialog class for that? You should be able to have EXACTLY what you want.

    I made these changes to NewLogin:
    public class NewLoginDialog extends JDialog 
    	public NewLoginDialog(Frame owner, String title, boolean modal)
    	{
          super(owner, title, modal);
       ...
         NewLoginDialog nld = new NewLoginDialog(null, "New Login", true);  // for testing

  22. The Following User Says Thank You to Norm For This Useful Post:

    Hallowed (May 29th, 2011)

  23. #22
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: Newbish Question...(s)

    Well I haven't heard of or seen JDialog before. But I tried the cod you provided in the program, and it works, except that the dialogbox wont let me work with the other jframes I already made.. It wont release control of the program anyway until its resolved. I'm guess if I changed all of them to dialogboxes like that one, they would take precedence over the first one.
    But, what really is the difference between using these kinds of dialog boxes and the JFrames I was already using? Maybe it wont glitch like the other one did?

  24. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Newbish Question...(s)

    dialogbox wont let me work with the other jframes
    That's what modal mode means.

    Why would your design require having multiple windows open/displayed with any of them being alive for input? Seems like a strange design.

  25. #24
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: Newbish Question...(s)

    Yea true. I will try the dialog boxes, and see if it doesn't freeze anymore.

  26. #25
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: Newbish Question...(s)

    Wow it works! =D and it runs a lot smoother!
    Thanks so much for showing me that

Page 1 of 2 12 LastLast