0

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();
    }
}
4
  • What is error/exception are you getting ? Commented Jul 14, 2019 at 16:17
  • No error. It just over itterate the second array. Starting count is: 2 After Click count is: 3 Size of JLabel Array labelList is: 3 Size of String Array stringArray is: 6 ArrayList is: [0, 0, 1, 0, 1, 2] Commented Jul 14, 2019 at 16:48
  • 1
    From the code you posted: I suspect the problem is here underneath Your suspicion is correct. According to the code you posted, every time you choose the Add row menu option, you add one new JLabel to labelList and then you add the text for ALL the labels to stringArray. Just add the text for the new label to stringArray. No need for a loop. Commented Jul 14, 2019 at 17:05
  • lol. Brilliant. It worked. Thanks. I will update the code to show for someone else if it needs help Commented Jul 14, 2019 at 17:09

3 Answers 3

1

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());
Sign up to request clarification or add additional context in comments.

Comments

1

you use this code for adding text to ArrayList:

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

replace that with this for the text of labels:

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

also you dont have iterate over the labellist all times, only instance new Label, add it to labellist and the text of label to the stringlist:

JLabel label = new JLabel("" + count);
                                                // 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.
stringArray.add(label.getText());

2 Comments

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
Returning to this issue I don't think the solution is correct. It seems it only gets the last value in the jlabel array but it does not recognize correctly all the jlabel in the array of labels
0

It was solved by Abra and ZamarianPatrick. I am posting the code if anyone else might be searching for this solution. It was so simple... Thanks guys!!!

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 javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;

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
                texts = "" + count; // count
                JLabel label = new JLabel(texts); // starts
                // correctly
                labelList.add(label); // adds new
                                        // JLabels in
                                        // the labelList

                stringArray.add(label.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();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.