0

I am trying to do a project for my class. I am supposed to build 3 columns of data. here is the code for a GUI class:

public void displayArray(String[] wordArray) {
    Container myContentPane = project1JFrame.getContentPane();
    TextArea arrayArea = new TextArea();
    for (int i = 0; i < wordArray.length; i++) {
        if (wordArray[i] != null) {
            arrayArea.append(wordArray[i] + "\n"); // add the words of the array into the TextArea
        }
    } //for
    myContentPane.add(arrayArea, BorderLayout.WEST);
    project1JFrame.setVisible(true);

} //displayArray

public void displaySortedArray(String[] wordArray) {
    Container myContentPane = project1JFrame.getContentPane();
    TextArea arrayArea = new TextArea();
    for (int i = 0; i < wordArray.length; i++) {
        if (wordArray[i] != null) {
            arrayArea.append(wordArray[i] + "\n"); // add the words of the array into the TextArea
        }
    } //for
    myContentPane.add(arrayArea, BorderLayout.CENTER);
    project1JFrame.setVisible(true);

} //displaySortedArray

public void displaySortedList(WordList myList) {

    Container myContentPane = project1JFrame.getContentPane();
    TextArea listArea = new TextArea();
    WordListIterator myIt;
    listArea.setText("");
    myIt = myList.reset();

    while (myIt.hasNext()) {
        myList.append(myIt.next() + "\n");
    }
    myContentPane.add(listArea, BorderLayout.EAST);
    project1JFrame.setVisible(true);
}

When i try to run this code along with my main program, i only get 2 columns. I want 3 columns. I am guessing it has something to do with border layout but i cant seem to do it. Help please!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • To start with, you should be using a `javax.swing.JTextArea` not `java.awt.TextArea`, may not solve your immediate problem, but will probably solve some others – MadProgrammer Mar 29 '16 at 23:50
  • 1
    Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses – MadProgrammer Mar 29 '16 at 23:51

1 Answers1

3

Seems to work okay for me...

BorderLayout

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

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

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new BorderLayout());
            add(new JScrollPane(makeTextArea("On the left")), BorderLayout.WEST);
            add(new JScrollPane(makeTextArea("Jam in the middle")), BorderLayout.CENTER);
            add(new JScrollPane(makeTextArea("On the right")), BorderLayout.EAST);
        }

        protected JTextArea makeTextArea(String text) {
            JTextArea ta = new JTextArea(10, 20);
            ta.setText(text);
            return ta;
        }

    }

}

Consider providing a runnable example which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses

Although, I'd consider using a GridLayout insead...

GridLayout

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

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

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridLayout(1, 3));
            add(new JScrollPane(makeTextArea("On the left")), BorderLayout.WEST);
            add(new JScrollPane(makeTextArea("Jam in the middle")), BorderLayout.CENTER);
            add(new JScrollPane(makeTextArea("On the right")), BorderLayout.EAST);
        }

        protected JTextArea makeTextArea(String text) {
            JTextArea ta = new JTextArea(10, 20);
            ta.setText(text);
            return ta;
        }

    }

}

Have a look at How to Use GridLayout for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 1
    @Harshad Kapoor: Also consider `BoxLayout`, seen [here](http://stackoverflow.com/a/15104660/230513). – trashgod Mar 30 '16 at 01:35