
-----------------------------------
Krocker
Fri Feb 14, 2014 2:35 pm

Customizing Layouts of JComponents
-----------------------------------
Hi, so im trying to create a gui that looks like this the image attached. I made the buttons, and the gui itself, but im hacing trouble positioning the text area and making the scroll bar. Can someone please show me how i can do this.

This is what i have so far in terms of the creating the gui:
[code]
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;


public class FileSummeryGui extends JFrame {
	
	private JButton open, clear, save;
	private JLabel label;
	private JPanel panel;
	private JTextArea text;
	
	/**
	 * Constructs the frame and components of the GUI; initializes the variables.
	 */
	public FileSummeryGui(){
		createComponents();
		
	}
	
	/**
	 * Creates and adds all the required components for the GUI and attaches the Actionlistener
	 */
	public void createComponents(){
		open = new JButton("Open File");
		clear = new JButton("Clear Display");
		save = new JButton("Save File");
		text = new JTextArea (10 , 10);
		text.setEditable(false);
		panel = new JPanel();
		label = new JLabel();

		panel.add(open);
		panel.add(clear);
		panel.add(save);
		add(panel);	
	}
[/code]

-----------------------------------
Krocker
Sat Feb 15, 2014 10:38 pm

RE:Customizing Layouts of JComponents
-----------------------------------
Anyone? I know it might be a basic concept, but this is my 1st time using guis in java. Please someone help?

-----------------------------------
Zren
Sun Feb 16, 2014 2:29 pm

Re: Customizing Layouts of JComponents
-----------------------------------
Designing GUIs is one of the reasons I love the Netbeans IDE. It comes with a fairly awesome swing editor. I find that looking at the code it generates instils good coding practice, even though it's probably going to throw a lot of stuff at you on first glance.

To solve your problem though, I made a quick GUI in the editor. I used an actual menu instead of buttons though, so you can't copy it exactly ;]. It looks like what it generated wrapped the TextArea in a ScrollPane.

For the layouts, it seems to use a GroupLayout.

In order to get the title + boder thing it's got going on, you need to put the ScrollPane inside a Panel. Then set the border of the Panel to something like BorderFactory.createTitledBorder("Blarg").

What this code looks like:

http://i.imgur.com/sh8DZNM.png


    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Blarg"));

        jTextArea1.setColumns(20);
        jTextArea1.setLineWrap(true);
        jTextArea1.setRows(5);
        jTextArea1.setText("1\n2\n3\n4\n5\n6\n7\n8\n9\n09\n2\n34\n4\n54\n565\n6\n6");
        jScrollPane1.setViewportView(jTextArea1);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(20, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 268, Short.MAX_VALUE))
        );

        jMenu1.setText("File");

        jMenuItem1.setText("Open");
        jMenu1.add(jMenuItem1);

        jMenuItem2.setText("Save");
        jMenu1.add(jMenuItem2);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Clear");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }

