简体   繁体   中英

How do I combine a JTextField and a JRadioButton?

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);

Place the third radiobutton without text and add JTextField . Eg 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

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 eg 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

在此处输入图片说明

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();
    }
}

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM