How do I add JCheckBox next to each name that has been entered into an arraylist from a JTextField?

How do I add JCheckBox next to each name that has been entered into an arraylist from a JTextField?

Content Index :

How do I add JCheckBox next to each name that has been entered into an arraylist from a JTextField?
Tag : java , By : SilverRuby
Date : November 29 2020, 09:01 AM

Hope this helps You could use a JPanel with BoxLayout as the layout manager.
That way, for each new name entered in the JTextField, you can add a new JCheckBox to the JPanel and it automatically lists lays them out in order vertically.
public class ManageUsersGUI1 extends JPanel {
    public static ArrayList<AddUsers> users = new ArrayList<>();

    private JLabel addNewUserLabel;
    private JTextField addNewUserTextField;
    private JLabel deleteUsersLabel;
    private JButton addButton;
    private JButton deleteButton;
    private JPanel namePanel;

    public ManageUsersGUI1() {
        //construct components
        addNewUserLabel = new JLabel ("Add new User here:");
        addNewUserTextField = new JTextField (0);
        deleteUsersLabel = new JLabel ("Select which User(s) you would like to delete:");
        addButton = new JButton ("Add");
        deleteButton = new JButton ("Delete");
        namePanel = new JPanel();
        namePanel.setLayout(new BoxLayout(namePanel, BoxLayout.Y_AXIS));

        //set components properties
        addNewUserTextField.setToolTipText ("Enter name and click on Add button.");
        addButton.setToolTipText ("Click here to Add new user.");
        deleteButton.setToolTipText ("Click here to delete User(s) selected.");

        //adjust size and set layout
        setPreferredSize (new Dimension (580, 485));
        setLayout (null);

        //add components
        add (addNewUserLabel);
        add (addNewUserTextField);
        add (deleteUsersLabel);
        add (namePanel);
        add (addButton);
        add (deleteButton);

        //set component bounds (only needed by Absolute Positioning)
        addNewUserLabel.setBounds (85, 130, 120, 25);
        addNewUserTextField.setBounds (235, 130, 125, 25);
        deleteUsersLabel.setBounds (135, 225, 281, 25);
        addButton.setBounds (385, 130, 100, 25);
        namePanel.setBounds(225, 270, 140, 0);
        deleteButton.setBounds (230, 335, 100, 25);

        addButton.addActionListener(new AddButtonListener());


    private class AddButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            String text = addNewUserTextField.getText();
            users.add(new AddUsers(text));

            // Display the charges.
            JOptionPane.showMessageDialog(null, text + " has been added.");

            JCheckBox nameCheckBox = new JCheckBox();
            namePanel.setBounds(225, 270, 140, namePanel.getHeight() + 25);
            deleteButton.setBounds(230, deleteButton.getY() + 25, 100, 25);
            JFrame frame = (JFrame) getRootPane().getParent();
            frame.setSize(frame.getWidth(), frame.getHeight() + 25);

    public static void main (String[] args) {
        JFrame frame = new JFrame ("AddUsersPanel1");
        frame.setTitle("Manage Users");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add (new ManageUsersGUI1());
        frame.setVisible (true);

No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Using an itemListener with JCheckBox to show/hide JTextField

Tag : java , By : nd27182
Date : March 29 2020, 07:55 AM
will be helpful for those in need I'm trying to create an application that allows a user to choose insurance options in JCheckBoxes. For each option that is selected, the name and price are supposed to appear in a text field. My problem is that even when I select it, it doesn't display the Name & Price. At the moment I'm just trying to make the HMO checkbox work. , Add the following code in your if block and it will work as expected

How to Add JTextField & JCheckBox to ArrayList Object

Tag : java , By : Pradeep Gowda
Date : March 29 2020, 07:55 AM
seems to work fine Use your addPlantBtn ActionListener to gather the information you need when it's called
addPlantBtn.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent e) {
        Flower flower = new Flower(nameField.getText(), idField.getText(), colorField.getText(), thornsBox.isSelected(), smellBox.isSelected());
public class FlowerPane extends JPanel {

    JTextField nameField = new JTextField(15);
    JTextField idField = new JTextField(10);
    JTextField colorField = new JTextField(10);
    JCheckBox smellBox = new JCheckBox("Smell Present");
    JCheckBox thornsBox = new JCheckBox("Thorns Present");

    public FlowerPane() {
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(2, 2, 2, 2);

        JLabel nameLabel = new JLabel("Name:");
        JLabel idLabel = new JLabel("ID:");
        JLabel colorLabel = new JLabel("Color:");

        add(nameLabel, gbc);
        add(idLabel, gbc);
        add(idLabel, gbc);

        gbc.gridy = 0;
        add(nameField, gbc);
        add(idField, gbc);
        add(colorField, gbc);

        gbc.gridx = 0;
        gbc.gridwidth = GridBagConstraints.REMAINDER;

        add(thornsBox, gbc);
        add(smellBox, gbc);

    public Flower create() {
        return new Flower(nameField.getText(), idField.getText(), colorField.getText(), thornsBox.isSelected(), smellBox.isSelected());

FlowerPane flowerPane = new FlowerPane();
switch (JOptionPane.showConfirmDialog(null, flowerPane, "Flower", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE)) {
    case JOptionPane.OK_OPTION:
        Flower flower = flowerPane.create();

How can I enable/disable a JTextField with a JCheckBox? or what's wrong with my code?

Tag : java , By : Longchao Dong
Date : March 29 2020, 07:55 AM
Hope that helps I'm newbie in programming java, I have an array of JCheckBox next to an array of JTextfield. , Here is a quick solution with an ItemListener.
 private void setupEvents() {
        for (int i=0; i<10; i++) {
            final int finalI = i;
            cb[i].addItemListener(new ItemListener() {
                public void itemStateChanged(ItemEvent e) {
                    text[finalI].setEnabled(e.getStateChange() == ItemEvent.SELECTED);
 private void setupEvents() {
        for (int i=0; i<10; i++) {
            final int finalI = i;
            cb[i].addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    text[finalI].setEnabled(!text[finalI].isEnabled() && e.getID() == ActionEvent.ACTION_PERFORMED);

How can I enable/disable my JTextField depending of the state of a JCheckBox?

Tag : java , By : woxorz
Date : March 29 2020, 07:55 AM

Search an arraylist by the contents of a JTextField - arraylist.contains(jtextfield)

Tag : java , By : SA.
Date : March 29 2020, 07:55 AM
Related Posts Related QUESTIONS :
  • Why not use ListIterator for full LinkedList Operation?
  • Android Webview EvaluateJavascript sometimes does not return a response
  • Matcher java doesn't work but regex seems to be good
  • Finding dimensions of a .gif file
  • Java Number format how to change +/- sign to custom text
  • Entity partially saved when using JOINED inheritance strategy and setting spring.jpa.properties.hibernate.jdbc.batch_siz
  • Stored Procedure in Java Spring Boot Project returns null as Output
  • How to solve org.hibernate.MappingException which is causing due to inheriting a class
  • Clean Archtecture. Understanding of scheme
  • Processing 3 triangle not showing in Javafx 8 Window tab
  • How to specify a sequence-based generated value in Hibernate 5 via legacy mapping
  • Spring-boot application not getting auto-deployed on startup
  • How to only pass strings that the user select
  • Is there a way to SELECT using "GREATEST(field1, field2)" where field1 and field2 are aggregate sums in the sa
  • How to handle JSON objects wrapped into one JSON object with retrofit2?
  • Configure Hazelcast CPSubsystem Retries Timeout
  • how to use onBindViewHolder with multiple items in android RecyclerView
  • No ParameterResolver registered for parameter in BeforeAll method
  • Finding the path in a graph with the least casualties according to the lanchester square law
  • MongoWriteException when inserting into Mongodb with composite custom _id
  • Fetch Oracle procedure metadata with Java when multiple procedure signatures
  • Value modification of key-pair in HashMap and impact for a HashCode
  • Migration from solrj to spring-data-solr
  • How to check if you're still connected to the database with jpa
  • Use Date type in the graphql scheme
  • Split and add the string based on length
  • Is "main" method of spring boot application required when deploy as war
  • Getting the average within specific numbers in an array
  • how to use izpack to make my jar application to installer?
  • What is meant by src in Java Eclipse?
  • Create a mirrored linked list in Java
  • Examples of good JPA Java Desktop Application
  • Translate Java to Python -- signing strings with PEM certificate files
  • Algorithm Analysis tool for java
  • Java serial comm API - what does inputstream.read() return if a timeout occurs?
  • How do I make a background thread in Java that allows the main application to exit completely? This works in Linux, but
  • How to add an image dynamically at runtime in java
  • Java App on Mac asking for allow network connections everytime
  • Best actively maintained Java XMPP Library?
  • Multi-Threaded Application - Help with some pseudo code!
  • Scoping a StringBuilder inside a for loop
  • How to specify hash algorithm when updating LDAP via Java?
  • Class not found exception (org.apache.openjpa.enhance.PersistenceCapable) thrown in a client of WLS 10
  • In Java ,where in memory are class functions put?
  • How do I test expectedExceptionsMessageRegExp (exception message) using TestNG?
  • Help In understanding Multi Dimentional Arrays
  • No bean named '...' is defined and Spring @Resource annotation
  • Singleton design pattern vs Singleton beans in Spring container
  • flashvars object was not working in mozilla browser
  • Shell script to import mysql dump file
  • What are the best practices to separate data from users
  • May I use com.google.code prefix for my packages?
  • How to set classpath in manifest file , while creating JAR from eclipse?
  • dealing with voice in java
  • Error: java.security.AccessControlException: Access denied
  • How to verify the root trust of a ceritifcate
  • Searching for a standalone JSP compiler
  • Spring 2.5 managed servlets: howto?
  • iBatis get executed sql
  • GRAPH: find an algorithm to determine the shortest path from one point to another in a rectangular maze?
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com