Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Customizing Layouts of JComponents
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Krocker




PostPosted: Fri Feb 14, 2014 2:35 pm   Post subject: 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);     
        }



gui.PNG
 Description:
The gui that i need to make
 Filesize:  141.28 KB
 Viewed:  143 Time(s)

gui.PNG


Sponsor
Sponsor
Sponsor
sponsor
Krocker




PostPosted: Sat Feb 15, 2014 10:38 pm   Post subject: 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




PostPosted: Sun Feb 16, 2014 2:29 pm   Post subject: 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:

Posted Image, might have been reduced in size. Click Image to view fullscreen.

Java:

    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();
    }
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: