2

I would like to have a JRadioPanel with three options. The first two are hardcoded options, and I want the third to be an 'Other' button. I would like to have a JTextField instead of text as the title of that button, but I'm not sure how to do that. I tried simply putting the field as the argument for the radio button, but it didn't like that much. I haven't found anything online to tell me how, except maybe through NetBeans, and that doesn't do me much good. Is there any way to easily do this, or will I have to do some fancy stuff with the layout?

Okay, new problem. The buttons are all looking right, but for some reason they're in a row instead of in a column. Here's the code for it. Not sure why it's doing this.

    tf2 = new JTextField("Other", 20);
    newName.setActionCommand("newname");
    fulfillment.setActionCommand("fulfillment");
    fulfillment.setSelected(true);
    type.add(fulfillment);
    type.add(newName);
    fulfillment.addActionListener(this);
    newName.addActionListener(this);
    GridBagConstraints rC = new GridBagConstraints();
    JPanel radioPanel3 = new JPanel(new GridBagLayout());
    rC.gridwidth = 2;
    radioPanel3.add(fulfillment);
    rC.gridy = 1;
    radioPanel3.add(newName);
    rC.gridy = 2;
    rC.gridwidth = 1;
    radioPanel3.add(other);
    rC.gridx = 1;
    radioPanel3.add(tf2);
    c.gridx = 10;
    c.gridy = 4;
    pane.add(radioPanel3, c);
SaintWacko
  • 854
  • 3
  • 14
  • 35
  • Why do you want a text field as the label? Won't you still need the label to indicate "other"? – cdeszaq Nov 30 '11 at 17:10
  • I wasn't planning to put any text on that button. I think the empty text field will be enough to say "Create your own". – SaintWacko Nov 30 '11 at 17:22

3 Answers3

4

Place the third radiobutton without text and add JTextField. E.g. Use GridBagLayout where the first and the second radiobuttons take 2 columns and in the third row the "empty" radiobutton has row=2 col=0 and JTextField row=2 col=1

David Hall
  • 32,624
  • 10
  • 90
  • 127
StanislavL
  • 56,971
  • 9
  • 68
  • 98
2

you can to create a Custom Component that would be Editable and you can add method setLabelFor(), but why reinventing the wheel

  1. remove any narrative for JRadioButtons/JCheckBoxes

  2. add put there JTextFields

    • to set inital size e.g. JTextField(10) for create initial Gap betweens JTextField and next JRadioButtons/JCheckBoxes

    • setBorder(null)

    • setBackground(null)

    • setEditable(false)

    • setOpaque(false) for translucent effect

  3. then all there JCmponents put to the JPanel, there no reason for change LayoutManager, because by default JPanel has FlowLayout

enter image description here

from code

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

public class AddComponentsAtRuntime {

    private JFrame f;
    private JPanel panel;
    private JCheckBox checkValidate, checkReValidate, checkRepaint, checkPack;
    private JTextField checkValidateTex, checkReValidateTex, checkRepaintTex, checkPackTex;

    public AddComponentsAtRuntime() {
        JButton b = new JButton();
        b.setBackground(Color.red);
        b.setBorder(new LineBorder(Color.black, 2));
        b.setPreferredSize(new Dimension(600, 10));
        panel = new JPanel(new GridLayout(0, 1));
        panel.add(b);
        f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(panel, "Center");
        f.add(getCheckBoxPanel(), "South");
        f.setLocation(200, 200);
        f.pack();
        f.setVisible(true);
    }

    private JPanel getCheckBoxPanel() {
        checkValidateTex = new JTextField(10);
        checkValidateTex.setText("validate");
        //checkValidateTex.setBorder(null);
        //checkValidateTex.setBackground(null);
        //checkValidateTex.setOpaque(false);
        //checkValidateTex.setEditable(false);
        checkValidate = new JCheckBox();
        //checkValidate = new JCheckBox("validate");
        checkValidate.setSelected(false);
        checkReValidateTex = new JTextField("revalidate");
        checkReValidateTex.setBorder(null);
        checkReValidateTex.setBackground(null);
        checkReValidateTex.setOpaque(false);
        checkReValidateTex.setEditable(false);
        checkReValidate = new JCheckBox();
        //checkReValidate = new JCheckBox("revalidate");
        checkReValidate.setSelected(false);
        checkRepaintTex = new JTextField("repaint");
        checkRepaintTex.setBorder(null);
        checkRepaintTex.setBackground(null);
        checkRepaintTex.setOpaque(false);
        checkRepaintTex.setEditable(false);
        checkRepaint = new JCheckBox();
        //checkRepaint = new JCheckBox("repaint");
        checkRepaint.setSelected(false);
        checkPackTex = new JTextField("pack");
        checkPackTex.setBorder(null);
        checkPackTex.setBackground(null);
        checkPackTex.setOpaque(false);
        checkPackTex.setEditable(false);
        checkPack = new JCheckBox();
        //checkPack = new JCheckBox("pack");
        checkPack.setSelected(false);
        JButton addComp = new JButton("Add New One");
        addComp.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JButton b = new JButton();
                b.setBackground(Color.red);
                b.setBorder(new LineBorder(Color.black, 2));
                b.setPreferredSize(new Dimension(600, 10));
                panel.add(b);
                makeChange();
                System.out.println(" Components Count after Adds :" + panel.getComponentCount());
            }
        });
        JButton removeComp = new JButton("Remove One");
        removeComp.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int count = panel.getComponentCount();
                if (count > 0) {
                    panel.remove(0);
                }
                makeChange();
                System.out.println(" Components Count after Removes :" + panel.getComponentCount());
            }
        });
        JPanel panel2 = new JPanel();
        panel2.add(checkValidate);
        panel2.add(checkValidateTex);
        panel2.add(checkReValidate);
        panel2.add(checkReValidateTex);
        panel2.add(checkRepaint);
        panel2.add(checkRepaintTex);
        panel2.add(checkPack);
        panel2.add(checkPackTex);
        panel2.add(addComp);
        panel2.add(removeComp);
        return panel2;
    }

    private void makeChange() {
        if (checkValidate.isSelected()) {
            panel.validate();
        }
        if (checkReValidate.isSelected()) {
            panel.revalidate();
        }
        if (checkRepaint.isSelected()) {
            panel.repaint();
        }
        if (checkPack.isSelected()) {
            f.pack();
        }
    }

    public static void main(String[] args) {
        AddComponentsAtRuntime makingChanges = new AddComponentsAtRuntime();
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
0

You'll have to do fancier stuff with the layout. There is nothing built-in that does it in swing...

It shouldn't be that hard to implement though! Using netbeans is a good idea for GUIs. You should consider using the IDE for the GUI part of the project. It will generate the code for you so at worst you should consider using the generated code in your favorite IDE in the form of actual source code or a generate jar. I've done this a LOT as my favorite editor is eclipse and I prefer to have a wysiwig gui editor.

Eclipse also has many wysiwyg editor plugins you can check out but I've never done so. Here's one: http://www.ibm.com/developerworks/opensource/library/os-ecvisual/

vinnybad
  • 2,102
  • 3
  • 19
  • 29
  • I've never really liked using gui editors. I've always preferred to write the code myself. I think you learn more that way. I use eclipse myself, it's a great program. – SaintWacko Nov 30 '11 at 17:51
  • @SaintWacko Absolutely, eclipse is amazing. I'm very impressed how far it's come along in the past five years. And yes, you do learn more that way. Thing is...is it really worth learning swing programming when there are much better gui frameworks out there? (and more contemporary frameworks) – vinnybad Nov 30 '11 at 18:37