Matthew Phaneuf
Matthew Phaneuf

Reputation: 35

Display Array in a JTextArea with ability to scroll

I am creating a GUI that will allow the user to input Lake information for the state of Florida and then has the ability to display that lake information. I want the display information to be in a JOptionPane.showMessageDialog that has the ability to scroll through the ArrayList of all the lake names. I am able to add the lakes into the ArrayList but they will not display in my JOptionPane and it is blank. I know it is reading something in the ArrayList since it is opening that window. Here is the code below in snippets as the whole thing would be cra.

public static ArrayList<Lakes> lake = new ArrayList<Lakes>();
private JTextArea textAreaDisplay;
private JScrollPane spDisplay;

// this is called in my initComponent method to create both 
    textAreaDisplay = new JTextArea();

    for (Object obj : lake)
    {
        textAreaDisplay.append(obj.toString() + " ");
    }

    spDisplay = new JScrollPane(textAreaDisplay);

    textAreaDisplay.setLineWrap(true);
    textAreaDisplay.setWrapStyleWord(true);
    spDisplay.setPreferredSize(new Dimension(500, 500));

    // this is called in my createEvents method. After creating lakes in the database
    // it will display the else statement but it is empty

    btnDisplayLake.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try
            {
                if (lake.size() == 0) 
                {
                    JOptionPane.showMessageDialog(null, "No Lakes in database!");
                }
                else
                    JOptionPane.showMessageDialog(null, spDisplay, "Display Lakes", JOptionPane.YES_NO_OPTION);
            }
            catch (Exception e1)
            {

            }

        }
    });

Thank you for any help you can provide. I have been racking my brain for a few days on this. Been able to get other stuff accomplished but coming back to this issue.

Upvotes: 0

Views: 181

Answers (2)

Matthew Phaneuf
Matthew Phaneuf

Reputation: 35

I was setting textAreaDisplay before anything was entered into the ArrayList and it would not run again after anything was entered. I moved the for loop down and into the actionPerformed event and works well now.

    btnDisplayLake.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try
            {
                for (Object obj : lake)
                {
                    textAreaDisplay.append(obj.toString() + "\n");
                }
                if (lake.size() == 0) 
                {
                    JOptionPane.showMessageDialog(null, "No Lakes in database!");
                }
                else
                    JOptionPane.showMessageDialog(null, spDisplay, "Display Lakes", JOptionPane.YES_NO_OPTION);
            }
            catch (Exception e1)
            {

Upvotes: 0

camickr
camickr

Reputation: 324118

Some obvious issues:

textAreaDisplay = new JTextArea();

A JTextArea should be created with code like:

 textAreaDisplay = new JTextArea(5, 20);

By specifying the row/column the text area will be able to calculate its own preferred size. Scrollbars should appear when the preferred size of the text area is greater than the size of the scroll pane.

spDisplay.setPreferredSize(new Dimension(500, 500));

Don't use setPreferredSize(). The scroll area will calculate its preferred size based on the preferred size of the text area.

textAreaDisplay.append(obj.toString() + " ");

I would think you want each Lake to show on a different line, so I would append "\n" instead of the space.

Upvotes: 1

Related Questions