How to combine JTextField and JRadioButton?

I would like to have JRadioPanelwith three parameters. The first two are hard-coded options, and I want the third to be “different”. I would like to have JTextFieldthis button instead of text, but I'm not sure how to do it. I tried just putting the field as an argument for the switch, but he didn't like it. I did not find anything online to tell me, except, perhaps, through NetBeans, and this does not do me much good. Is there a way to easily do this, or do I need to do some fancy stuff with a layout?

Ok, a new problem. Buttons look right, but for some reason they are in a row instead of a column. Here is the code for it. I don’t know why this is done.

    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);
+2
3

JTextField. . GridBagLayout, 2 , "" = 2 col = 0 JTextField row = 2 col = 1

+4

, , setLabelFor(),

  • JRadioButtons/JCheckBoxes

  • JTextFields

    • , . JTextField (10) Gap betweens JTextField JRadioButtons/JCheckBoxes

    • setBorder ()

    • setBackground ()

    • setEditable ()

    • setOpaque (false)

  • JCmponents JPanel, LayoutManager, JPanel FlowLayout

enter image description here

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

. , ...

! netbeans - . IDE GUI . , IDE . LOT, eclipse, wysiwig gui.

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

0
source

Source: https://habr.com/ru/post/1746997/


All Articles