0

I'm fairly new to GUI. I'm trying to make it so that depending on which radio button is selected, a JLabel changes its value. For example, if "id" is selected, it'll display "http://steamcommunity.com/id/" and if "profile" is selected, it'll display "http://steamcommunity.com/profiles/". I have some code up and running and it's nearly complete:

package sgt;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;


public class RadioButtonPrompt extends JPanel
                             implements ActionListener {

 private static final long serialVersionUID = 1L;
 static String idString = "ID";
    static String profileString ="Profile";
    static String type = idString;

    public RadioButtonPrompt() {
        super(new BorderLayout());

        // Create radio buttons.
        JRadioButton idButton = new JRadioButton(idString, true);
        idButton.setMnemonic(KeyEvent.VK_I);
        idButton.setActionCommand(idString);

        JRadioButton profileButton = new JRadioButton(profileString);
        profileButton.setMnemonic(KeyEvent.VK_P);
        profileButton.setActionCommand(profileString);

        // Group radio buttons.
        ButtonGroup group = new ButtonGroup();
        group.add(idButton);
        group.add(profileButton);

        idButton.addActionListener(this);
        profileButton.addActionListener(this);

        JPanel radioPanel = new JPanel(new GridLayout(0, 1));
        radioPanel.add(idButton);
        radioPanel.add(profileButton);

        JPanel textPanel = new JPanel ();
        JLabel URL = new JLabel(setJLabelValue());

        JTextField text = new JTextField("sampletextfield");
        text.setPreferredSize(new Dimension(100, 20));

        textPanel.add(URL);
        textPanel.add(text);

        JPanel buttonPanel = new JPanel(new GridLayout(1, 0));
        JButton submit = new JButton("Submit");
        submit.setMnemonic(KeyEvent.VK_S);

        buttonPanel.add(submit);


        add(radioPanel, BorderLayout.LINE_START);
  add(textPanel, BorderLayout.CENTER);
  add(buttonPanel, BorderLayout.PAGE_END);

        setBorder(BorderFactory.createCompoundBorder());
    }

    private String setJLabelValue() {
     if (type.equals("ID")) {
      return "http://steamcommunity.com/id/";
     }
     return "http://steamcommunity.com/profiles/";

 }

    public void actionPerformed(ActionEvent e) {

     // Returns either "Profile" or "ID"
        type = ((JRadioButton)e.getSource()).getText();
        System.out.println(type);


    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("Steam Game Tracker");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JComponent newContentPane = new RadioButtonPrompt();
        newContentPane.setOpaque(true); //content panes must be opaque

        frame.setContentPane(newContentPane);

        // Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
Stan Kurilin
  • 15,614
  • 21
  • 81
  • 132
Alex
  • 149
  • 1
  • 3
  • 10

2 Answers2

2

Take a look at this SO thread.

Community
  • 1
  • 1
npinti
  • 51,780
  • 5
  • 72
  • 96
  • Wow, I could've sworn I checked to see if JLabel had a setText method, thanks for the help. – Alex Dec 09 '10 at 19:08
  • If this post solved your problem, then mark it so ;) You should be able to see a 'very good' near each answer. – npinti Dec 09 '10 at 19:10
0

in actionPerformed() you need to textpanel.setText() to whatever you want based on which button was clicked. I'm guessing at the method name, haven't done any UI stuff with Java for a while.

jcomeau_ictx
  • 37,688
  • 6
  • 92
  • 107