views:

69

answers:

5

I'm trying to add a JList to a GUI, but am wondering how to position it? I want it to appear on the right hand side of the TextArea for data that will be sent to the GUI for selection.

Can anyone suggest how to do this? Here is the code (note: very new to Java and GUI's)

protected static void createAndShowGUI() {
        GUI predict = new GUI();
        JFrame frame = new JFrame("Phone V1.0");

        frame.setContentPane(predict.createContentPane());

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setMinimumSize(new Dimension(300, 400));
        frame.setVisible(true); // Otherwise invisible window
}

private JPanel createContentPane() {
    JPanel pane = new JPanel();

    TextArea = new JTextArea(5, 10);
    TextArea.setEditable(false);
    TextArea.setLineWrap(true);
    TextArea.setWrapStyleWord(true);
    TextArea.setWrapStyleWord(true);
    pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS)); 
    //Adds the buttons from Top     to Bottom

    String[] items = {"dsfsdfd"};
    list = new JList(items);
    JScrollPane scrollingList = new JScrollPane(list);
    int orient = list.getLayoutOrientation();

    JPanel window = new JPanel();
    pane.add(window);

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

    JButton[] buttons = new JButton[] {
        new JButton("Yes"),
        new JButton(""),
        new JButton("Clr"),
        new JButton("1"),
        new JButton("2abc"),
        new JButton("3def"),
        new JButton("4ghi"),
        new JButton("5jkl"),
        new JButton("6mno"),
        new JButton("7pqrs"),
        new JButton("8tuv"),
        new JButton("9wxyz"),
        new JButton("*+"),
        new JButton("0_"),
        new JButton("^#")
    };  // Array Initialiser

    for (int i = 0; i < buttons.length; i++) {
        buttonPanel.add(buttons[i]);
        buttons[i].addActionListener(this);
    }
    pane.add(TextArea);
    pane.add(list);
    pane.add(buttonPanel);


    return pane;
}
+1  A: 

You could use a layout manager like Mig Layout for that kind of positionning.

alt text

VonC
A: 

I could recommend you FormLayout. Before I found this layout I had a real pain with GridBagLayout. FormLayout is more powerful and much more convenient to learn and use and it is free. Give it a chance.

wax
+1  A: 

Wrap your TextArea and list in a new panel with a BorderLayout manager. Basically the BorderLayout manager lets you arrange components using north, south, east, west and center coordinates. The components at the center takes all available space as the parent container has more space available to it.

private JPanel createContentPane() {
    JPanel pane = new JPanel();  //this is your main panel
    JPanel textAreaPanel = new JPanel(new BorderLayout()); //the wrapper

    //Some more code...

    //Then at the end
    //Make your TextArea take the center
    textAreaPanel.add(TextArea, BorderLayout.CENTER);
    //And the list to the east
    textAreaPanel.add(list, BorderLayout.EAST);
    pane.add(textAreaPanel);
    pane.add(buttonPanel);

    return pane;
}

The cool thing is that you can nest panels inside other panels, adding them different layout managers to get your desired layout.

On an unrelated note, try to follow Java naming conventions. Instead of JTextArea TextArea use JTextArea textArea. It makes it easier for you and people reading your code to understand it.

Cesar
+2  A: 

Read the section from the Swing tutorial on Using Layout Mananger. There is no need to only use a single layout manager. You can nest layout managers to get the desired effect.

camickr
A: 

As others suggested, familiarize yourself with the concept of layout managers. There are several that come with the standard Swing API and several good 3rd party ones out there.

In addition, you will want to add the JList to a scroll pane (JScrollPane). You may want to consider adding it to a split pane (JSplitPane). And by consider I don't mean "do it because some guy on the net said so" I mean "do it if it makes sense for your end users".

Avrom