Search code examples
javaarraysstringswingjlabel

Storing Strings from JLabel Array to String Array


I am struggling to make a small program in swing that needs to do the following:

  1. Right click on panel, click on "add" option. It needs to add JLabels dynamically inside a JLabel ArrayList.
  2. In the same time store that JLabel text inside an ArrayList of Strings.
  3. Print that arrayList to test that it did succesfully store the text inside the array.
  4. Later I will further save that text somehow. Not the problem so far.

I am stuck since I don't seem to find out how to transfer that text from one array to another.

How would that be done?

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Arrays;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;
import javax.swing.MenuElement;

public class A {

    JFrame frame;
    Toolkit toolkit;
    JPopupMenu quickMenu;
    JTextField field1;
    JPanel panel;

    ArrayList<JLabel> labelList;
    ArrayList<String> stringArray;
    String texts;

    JLabel label1, label2, label3;
    ArrayList<JTextField> fieldList;
    int count;

    A() {

        frame = new JFrame("A class");

        panel = new JPanel();
        panel.setLayout(new GridLayout(2, 3, 5, 5));

        JLabel title1 = new JLabel("Title 1");
        panel.add(title1);

        setDimm(frame);

        quickMenu = new JPopupMenu();
        JMenuItem addRow = new JMenuItem("Add row");
        JMenuItem removeRow = new JMenuItem("Remove row");

        quickMenu.add(addRow);
        quickMenu.add(removeRow);
        panel.add(quickMenu);

        panel.addMouseListener(new MouseAdapter() {
            public void mouseReleased(MouseEvent e) {
                if (e.getButton() == e.BUTTON3) {
                    quickMenu.show(e.getComponent(), e.getX(), e.getY());
                }

            }
        });

        labelList = new <JLabel>ArrayList();

        // ADD ROWS
    count = 0;
    stringArray = new ArrayList();

    addRow.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            System.out.println("Starting count is: " + count); // check if
                                                                // count
            JLabel label = new JLabel ("" + count);                                                 // starts
                                                                // correctly
            labelList.add(label); // adds new
                                                            // JLabels in
                                                            // the labelList
                                                            // array

            // I suspect the problem is here underneath, not sure how to
            // first get the texts inside the arraylist of JLabels and then
            // transfer this text to the array of Strings that I want to
            // print.
            for (int i = 0; i < labelList.size(); i++) {

                stringArray.add(labelList.get(i).getText());

            }

            panel.add(labelList.get(count));
            panel.revalidate();
            panel.repaint();

            count++;

            System.out.println("After Click count is: " + count);
            System.out.println("Size of JLabel Array labelList is: " + labelList.size());
            System.out.println("Size of String Array stringArray is: " + stringArray.size());               
            System.out.print("The content of the String ArrayList is: " + stringArray);
            System.out.println();
            System.out.println();               


        }

    });

        removeRow.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ev)

            {
                /*
                 * panel.remove(labelList.size() - 1); // Will it delete the
                 * last JLabel from the panel?
                 * labelList.remove(labelList.size()-1);
                 * 
                 * 
                 * panel.revalidate(); panel.repaint(); count--;
                 */
                System.out.println(count + " and does nothing yet");

            }

        });

        frame.add(panel);

        // panel.add(button);
        frame.setVisible(true);
    }

    void setDimm(JFrame frame) {
        this.frame = frame;
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.getContentPane().setBackground(Color.gray);
        frame.setSize(800, 600);
        toolkit = frame.getToolkit();
        Dimension size = toolkit.getScreenSize();
        frame.setLocation((size.width - frame.getWidth()) / 2, (size.height - frame.getHeight()) / 2);

    }

    public static void main(String[] args) {
        new A();
    }
}

Solution

  • Hello. I tried also that and apparently now it iterates multiple times the array of Strings. For the purpose of debugging I have added the println to know the length of both arrays and the string one goes 1,3,6,10,15 while the JLabel array is going correctly from 1,2,3,4,5. Example underneath: Size of JLabel Array labelList is: 6 Size of String Array stringArray is: 15 ArrayList is: [0, 0, 1, 0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 4] . I have updated initial code with this error

    Yes only replace this code:

    for (int i = 0; i < labelList.size(); i++) {
    
        stringArray.add(labelList.get(i).getText());
    
    } 
    

    with this:

    stringArray.add(label.getText());