-1

I've just uploaded my progress into the program. Please have a look and see what is lacking in it. Also, please ignore the lame joke from the strings in it while you're at it. It can be downloaded from this link: http://www.mediafire.com/?n0sp8v22egfsx7t

Note that I used NetBeans to easily make the program.

Edited:

How do I stop the ActionEvent method of a JButton from re-iterating the reading of the first layer of the nested if-else condition once the innermost line or last line of codes is read?

Also, as an additional question, how do I prompt the system to choose between choices thus literally branch out from the other once a choice is given? Apparently what I'm doing was just connect these block of strings together and not branch them out thus appear iterated once a new block of codes is to be displayed in connection to the previous block of code displayed. .

Snippet:

// Strings of text displayed on the JTextArea is shown here
// prompting to choose between two options: example, left or right

jButton1.addActionListener(new ActionListener()
{
    public void actionPerformed(ActionEvent e)
    {
       jButton1actionPerformed(e);
    }
});


private void jButton1actionPerformed(ActionEvent e) 
{                                             
    if(jRadioButton1.isSelected()) 
    {     
       // Display strings of text as response to the choice left
       // Another options to choose from here:, example, up or down
       JTextArea.setText("You've chosen: " );
       JTextArea.append("Now choose between: ");

       if (jRadioButton1.isSelected())
       {
         JTextArea.append("From first choice, choose between: ");
         // stop from here
       }

       else if (jRadioButton2.isSelected())
       {
         //same as above
       }
     }

     if (jRadioButton2.isSelected())
     {
        // Same as above
     }
}
Nikolai Shevchenko
  • 7,083
  • 8
  • 33
  • 42
dothackjhe
  • 29
  • 1
  • 2
  • 5

4 Answers4

0

Use JTextField.setText("") whenever you want to clean a text area. Regardind the main issue - Post some code..

user1249569
  • 83
  • 1
  • 3
  • 10
  • Actually, I don't need to clean the JTextArea so that I can show the previously displayed strings of text on it and also because of the JTextArea having a vertical scrollbar. – dothackjhe Oct 02 '12 at 01:31
0

I think if you are asking how to stop the action even once you hit the condition, all you need to do is add a return statement to your code. For example, if button 1 is pushed, display message, then add a return statement, that will result in the code exiting that method and not getting to the other if statement.

Maybe you want to use something like a Hash, or List to store your choices and each time a choice is made, add it to the List. Then you can always find the last choice made by getting our last Element of the list. I'm not real clear on what you are trying to make here, but maybe that will give you some ideas.


I wrote a small program here that has 2 radio buttons and a submit. When you click submit it updates the text in the window based on the radio button selection.

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;


public class MyWindow extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MyWindow frame = new MyWindow();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public MyWindow() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        final JRadioButton rdbtnNewRadioButton = new JRadioButton("Choice 1");
        rdbtnNewRadioButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        rdbtnNewRadioButton.setSelected(true);
        rdbtnNewRadioButton.setBounds(5, 7, 424, 23);
        contentPane.add(rdbtnNewRadioButton);

        JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("Choice 2");
        rdbtnNewRadioButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        rdbtnNewRadioButton_1.setBounds(5, 33, 109, 23);
        contentPane.add(rdbtnNewRadioButton_1);

        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(rdbtnNewRadioButton);
        buttonGroup.add(rdbtnNewRadioButton_1);

        final JTextArea textArea = new JTextArea();
        textArea.setBounds(57, 80, 321, 94);
        contentPane.add(textArea);

        JButton btnNewButton = new JButton("Submit");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (rdbtnNewRadioButton.isSelected()) {
                    textArea.setText("Choice 1 Selected.");
                } else {
                    textArea.setText("Choice 2 Selected.");
                }
            }
        });

        btnNewButton.setBounds(135, 212, 89, 23);
        contentPane.add(btnNewButton);
    }
}

Check out the Java Tutorials on using buttons. Specifically look at the How to use Radio Buttons section to see if that sheds any light on how you would design it.

How to use Radio Buttons

Logan
  • 2,369
  • 19
  • 20
  • I'm a little confused myself with the logic in terms of program design as this is my first time programming GUI in java. The main idea with the jButton is that it acts as the trigger to execute the choice chosen from two jRadioButtons: jRadioButton1 toggled -> OK'd by pressing jButton. – dothackjhe Oct 03 '12 at 08:29
  • I think you just need the first if/else then. If button1 is selected do this, else if button2 is selected do that. That way the button action is the trigger event you want, and that would check the selected button to perform an action. – Logan Oct 04 '12 at 02:51
  • Mind showing me a sample code on how to execute that? I think I'm doing it just the same albeit not properly. I maybe missing something in terms of semantics and logic that's why it won't work out. – dothackjhe Oct 05 '12 at 12:00
  • What is your code doing that you are not wanting it to do? I understand you have a radio button, so when you click your button you want it to see which radio button is selected and perform an action based on that. I have an application that does this, so I am familiar with wanting it to do that. Another thing you could do is put an action listener on the radio button, and when their values change, you can set another class variable that is used by your submit button action, so you only need 1 variable in that one. – Logan Oct 05 '12 at 23:44
  • Thanks for the sample code. I'll see how things goes with it. I'll keep this question open just in case someone has a good idea on this. – dothackjhe Oct 07 '12 at 01:19
  • I think my logic design and execution with the program however linear is just fine. The problem I'm facing right now is how do I make a prompt in similar fashion to a scanner's nextln, nextInt, etc. or a boolean to thus make a route of text strings. Also, how do I "break" this if-else condition given that the innermost if-else condition is reached and thus not iterate the whole nested if-else condition once the JButton is clicked from there? – dothackjhe Oct 07 '12 at 03:04
  • Have you tried just putting a "return" statement in your if/else statement? That's how you would return from the method and break the loop. Return will exit the method and go back to the calling method. That may be what you're wanting. Right after appending your text just put a "return;" in it. – Logan Oct 07 '12 at 14:28
  • I'm getting the idea of the keyword "return" with the program, but I'm still stuck with the idea of routing the text string flows. – dothackjhe Oct 08 '12 at 13:48
  • Maybe look into some kind of Queue, where it saves your order of in and out. Or maybe a Map of words and the next path. Something like key choice1 - value choice2, then key choice2 - value choice3. So then just take the last choice and look up the value mapped to it, it get the path of what to show next. – Logan Oct 08 '12 at 23:58
  • Check out this tutorial. They have a link about "putting it all together" that has some logic similar to what you may be looking for. http://www.javacoffeebreak.com/text-adventure/index.html – Logan Oct 10 '12 at 01:51
  • Mind showing me on how this is done? I'm lacking the proper syntax for this even if I do understand how it runs. – dothackjhe Oct 10 '12 at 02:02
0

How do I stop the ActionEvent method of a JButton from re-iterating the reading of the first layer of the nested if-else condition once the innermost line or last line of codes is read?

If I understand what you are asking, this is the whole point of an if ... else if ... else if ... (etc) ... else statement. When one of the conditions is matched, none of the other blocks is executed. So it looks like you just need to add elses to match your ifs. For example:

// Strings of text displayed on the JTextArea is shown here
// prompting to choose between two options: example, left or right

jButton1.addActionListener(new ActionListener()
{
    public void actionPerformed(ActionEvent e)
    {
       jButton1actionPerformed(e);
    }
});


private void jButton1actionPerformed(ActionEvent e) 
{                                             
    if(jRadioButton1.isSelected()) 
    {     
       // Display strings of text as response to the choice left
       // Another options to choose from here:, example, up or down
       JTextArea.setText("You've chosen: " );
       JTextArea.append("Now choose between: ");

       if (jRadioButton1.isSelected())
       {
         JTextArea.append("From first choice, choose between: ");
         // stop from here
       }

       else if (jRadioButton2.isSelected())
       {
         //same as above
       }
     }

     else if (jRadioButton2.isSelected()) // ***** Note the "else" added here.
     {
        // Same as above
     }
}

However, there is a problem with your code. Why are you checking two times if jRadioButton1 is selected? After the first if statement, you already know it is selected and don't need to check again.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • It is actually a nested if-else condition or an if-else condition within an if-else condition. It's linearly designed so it came out like that. The string to be displayed on the JTextArea should literally vary depending on one's route of choices. – dothackjhe Oct 07 '12 at 00:37
  • Please have a look on this simple application I was working on to have a better perspective on what this program is intended to be. The link for it is posted above. – dothackjhe Oct 07 '12 at 04:10
  • @dothackjhe Can you simplify your code enough so that you can post a complete, compilable program directly in your question? Check [here](http://www.sscce.org) for some tips on how to create simple code for your question. – Code-Apprentice Oct 07 '12 at 17:26
  • I can't make it any simpler than it already is. In fact the idea is very linear and simple, I just happen to have been lacking some necessary code / syntax / ideas to implement it. – dothackjhe Oct 08 '12 at 13:50
  • Here's the abstract idea: Given a set of introductory strings on the JTextArea, an initial choice is given for you to choose between two characters: Mario or Luigi. You, as a player, chooses from either of the two by toggling a JRadioButton from two JRadioButtons which will be implemented by clicking on the JButton. From there will be given a set of choices in pairs which can be answerable, again, by toggling a designated JRadioButton plus clicking on a JButton. The last pair of choices from the set of choices provided acts as the finishing statement for the program given that it's linear. – dothackjhe Oct 08 '12 at 13:57
  • @dothackjhe "In fact the idea is very linear" I think this is your primary problem. Swing programming is very *non*-linear. After reading your description, I suggest that you rethink your design. For each choice, you may want to create a separate `JPanel`. Using a `CardLayout` will also be helpful. – Code-Apprentice Oct 08 '12 at 23:26
  • 1
    Mind showing me a snippet on how that works given the logic in my simple program? Really, I'm new to Java GUI programming. – dothackjhe Oct 08 '12 at 23:53
  • @dothackjhe Check out [this tutorial](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html). – Code-Apprentice Oct 09 '12 at 00:06
  • I think using this for the logic design kinda refreshes the design overall. Thanks for the idea though. . – dothackjhe Oct 09 '12 at 23:06
0

If I can understand what you are asking

Maybe this will suffice:

private void jButton1actionPerformed(ActionEvent e) 
{                                             
    if(jRadioButton1.isSelected()) 
    {     
       // Display strings of text as response to the choice left
       // Another options to choose from here:, example, up or down
       JTextArea.setText("You've chosen: " );
       JTextArea.append("Now choose between: ");

//       if (jRadioButton1.isSelected())
//       {
//         JTextArea.append("From first choice, choose between: ");
//        // stop from here
//       }

       else if (jRadioButton2.isSelected())
       {
         //same as above
       }
     }
// The above else if block covers what you are doing bellow
//     if (jRadioButton2.isSelected())
//     {
//        // Same as above
//     }
}

If you want to do branching from the two different jRadioButton and having in mind the given code, I would suggest 4 possible paths

  1. if ( jRadioButton1.isSelected() && jRadioButton2.isSelected())

  2. if( jRadioButton1.isSelected())

  3. if( jRadioButton2.isSelected())

  4. else