Question

Working on an assignment right now and I'm essentially writing my first GUI. Everything is working fine except my scrollbar- upon adding it not only will it not display, but even after I've commented it out, the LAST element added(via .add function call) covers the ENTIRE GUI. Setting the layout to null seemed to fix that, but I'm not sure if that will help or hinder the first issue. I've been through Oracle's documentation and examples already, and didn't see what I needed. Here is the pertinent code(variables a-g that seem to be floating in space are part of a separate section and I haven't pasted it so as to isolate the GUI code):

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class Example
        {
        public static void main(String[] args) 
        {       

    JFrame mygui = new JFrame();
    mygui.getContentPane();
    mygui.setBounds(100, 100, 700, 500);
    mygui.setLayout(new BorderLayout()); //playing with layout here

    JLabel first = new JLabel("integer:");
    JLabel second = new JLabel("float:");
    JLabel third = new JLabel("short:");
    JLabel fourth = new JLabel("long:");
    JLabel fifth = new JLabel("byte:");
    JLabel sixth = new JLabel("double:");
    JLabel seventh = new JLabel("boolean:");
    JTextField myint = new JTextField("" + a);
    JTextField myfloat = new JTextField("" + b);
    JTextField myshort = new JTextField("" + c);
    JTextField mylong = new JTextField("" + d);
    JTextField mybyte = new JTextField("" + e);
    JTextField mydbl = new JTextField("" + f);
    JTextField mybool = new JTextField("" + g);
    JTextArea myarea = new JTextArea();
    JScrollPane scrolla = new JScrollPane(myarea);  

    first.setOpaque(true);
    first.setBounds(20, 20, 50, 20);
    second.setOpaque(true);
    second.setBounds(20, 70, 50, 20);
    third.setOpaque(true);
    third.setBounds(20, 120, 50, 20);
    fourth.setOpaque(true);
    fourth.setBounds(20, 170, 50, 20);
    fifth.setOpaque(true);
    fifth.setBounds(20, 220, 50, 20);
    sixth.setOpaque(true);
    sixth.setBounds(20, 270, 50, 20);
    seventh.setOpaque(true);
    seventh.setBounds(20, 320, 50, 20);

    myint.setBounds(70, 20, 50, 20);
    myint.setOpaque(true);
    myfloat.setBounds(70, 70, 50, 20);
    myfloat.setOpaque(true);
    myshort.setBounds(70, 120, 50, 20);
    myshort.setOpaque(true);
    mylong.setBounds(70, 170, 80, 20);
    mylong.setOpaque(true);
    mybyte.setBounds(70, 220, 50, 20);
    mybyte.setOpaque(true);
    mydbl.setBounds(70, 270, 50, 20);
    mydbl.setOpaque(true);
    mybool.setBounds(70, 320, 50, 20);
    mybool.setOpaque(true);

    myarea.setBounds(200, 50, 250, 200);
    myarea.setOpaque(true);
    scrolla.setPreferredSize(new Dimension(250, 200));

    mygui.add(first);
    mygui.add(second);
    mygui.add(third);
    mygui.add(fourth);
    mygui.add(fifth);
    mygui.add(sixth);
    mygui.add(seventh);

    mygui.add(myint);
    mygui.add(myfloat);
    mygui.add(myshort);
    mygui.add(mylong);
    mygui.add(mybyte);
    mygui.add(mydbl);
    mygui.add(mybool);
    mygui.add(scrolla);

    myarea.append("" + errcheck + "\n");
    myarea.append("" + valcheck + "\n");

    while(y < 2)
    {
    myarea.append("" + a + "\n");
    myarea.append("" + b + "\n");
    myarea.append("" + c + "\n");
    myarea.append("" + d + "\n");
    myarea.append("" + e + "\n");
    myarea.append("" + f + "\n");
    myarea.append("" + g + "\n");
    myarea.append("" + w + "\n");
    myarea.append("" + u + "\n");
    myarea.append("" + v + "\n");
    myarea.append("" + secondary + "\n");
    myarea.append("" + whilecount + "\n");;
    y++;
    a--;
    }

    mygui.setVisible(true);
    mygui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
 }

I'm sure it's something simple but any feedback is appreciated.

Was it helpful?

Solution

BorderLayout has five defined locations onto which components can be added...

enter image description here

The default location is CENTER.

Basically what is happening, when you add each component to the frame/content pane, it's removing the last one and placing it self in the middle, as only one component can exist within a give location.

Start by changing the layout manager. Take a look at A Visual Guide to Layout Managers for some ideas

OTHER TIPS

When using BorderLayout setBounds() method does not have any effect. Using BorderLayout,
you can fix your components only in five position North, South, East, West and Center
If you does not specify position like add(comp, BorderLayout.EAST), it will render your components
randomly. Adding more components without specifying position will get overlapped and it wont give clear view.

You are using more than five components and you are not specifying positions, so scrollpane
is not coming.

Use GridBagLayout to yield correct result. Refer http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

Remove this line:

mygui.getContentPane();

cause it does not thing. Also, change

mygui.setLayout(new BorderLayout()); //playing with layout here

to

mygui.getContentPane ().setLayout (null); // playing with layout here

cause you want to specify bounds for all components yourself. Also change

myarea.setBounds(200, 50, 250, 200);

to

scrolla.setBounds(200, 50, 250, 200);

and remove the following line:

scrolla.setPreferredSize(new Dimension(250, 200));

because without layout manager you don't need preferred size. Also change

mygui.add(first);
mygui.add(second);
mygui.add(third);
mygui.add(fourth);
mygui.add(fifth);
mygui.add(sixth);
mygui.add(seventh);

mygui.add(myint);
mygui.add(myfloat);
mygui.add(myshort);
mygui.add(mylong);
mygui.add(mybyte);
mygui.add(mydbl);
mygui.add(mybool);
mygui.add(scrolla);

to

mygui.getContentPane ().add(first);
mygui.getContentPane ().add(second);
mygui.getContentPane ().add(third);
mygui.getContentPane ().add(fourth);
mygui.getContentPane ().add(fifth);
mygui.getContentPane ().add(sixth);
mygui.getContentPane ().add(seventh);

mygui.getContentPane ().add(myint);
mygui.getContentPane ().add(myfloat);
mygui.getContentPane ().add(myshort);
mygui.getContentPane ().add(mylong);
mygui.getContentPane ().add(mybyte);
mygui.getContentPane ().add(mydbl);
mygui.getContentPane ().add(mybool);
mygui.getContentPane ().add(scrolla);

cause components should not be added directly to JFrame, but only to its content pane. After these changes it should work fine. You will see scroll bars once you have enough text in your text area. Here is fixed verion of your code that works for me:

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Example
{
    public static void main (String [] args)
    {

        JFrame mygui = new JFrame ();
        mygui.getContentPane ();
        mygui.setBounds (100, 100, 700, 500);
        mygui.getContentPane ().setLayout (null); // playing with layout here

        JLabel first = new JLabel ("integer:");
        JLabel second = new JLabel ("float:");
        JLabel third = new JLabel ("short:");
        JLabel fourth = new JLabel ("long:");
        JLabel fifth = new JLabel ("byte:");
        JLabel sixth = new JLabel ("double:");
        JLabel seventh = new JLabel ("boolean:");
        JTextField myint = new JTextField ("a");
        JTextField myfloat = new JTextField ("b");
        JTextField myshort = new JTextField ("c");
        JTextField mylong = new JTextField ("d");
        JTextField mybyte = new JTextField ("e");
        JTextField mydbl = new JTextField ("f");
        JTextField mybool = new JTextField ("g");
        JTextArea myarea = new JTextArea ();
        JScrollPane scrolla = new JScrollPane (myarea);

        first.setOpaque (true);
        first.setBounds (20, 20, 50, 20);
        second.setOpaque (true);
        second.setBounds (20, 70, 50, 20);
        third.setOpaque (true);
        third.setBounds (20, 120, 50, 20);
        fourth.setOpaque (true);
        fourth.setBounds (20, 170, 50, 20);
        fifth.setOpaque (true);
        fifth.setBounds (20, 220, 50, 20);
        sixth.setOpaque (true);
        sixth.setBounds (20, 270, 50, 20);
        seventh.setOpaque (true);
        seventh.setBounds (20, 320, 50, 20);

        myint.setBounds (70, 20, 50, 20);
        myint.setOpaque (true);
        myfloat.setBounds (70, 70, 50, 20);
        myfloat.setOpaque (true);
        myshort.setBounds (70, 120, 50, 20);
        myshort.setOpaque (true);
        mylong.setBounds (70, 170, 80, 20);
        mylong.setOpaque (true);
        mybyte.setBounds (70, 220, 50, 20);
        mybyte.setOpaque (true);
        mydbl.setBounds (70, 270, 50, 20);
        mydbl.setOpaque (true);
        mybool.setBounds (70, 320, 50, 20);
        mybool.setOpaque (true);

        myarea.setOpaque (true);
        myarea.setBackground (Color.yellow);
        scrolla.setBounds (200, 50, 250, 200);
        scrolla.setPreferredSize (new Dimension (250, 200));

        mygui.getContentPane ().add (first);
        mygui.getContentPane ().add (second);
        mygui.getContentPane ().add (third);
        mygui.getContentPane ().add (fourth);
        mygui.getContentPane ().add (fifth);
        mygui.getContentPane ().add (sixth);
        mygui.getContentPane ().add (seventh);

        mygui.getContentPane ().add (myint);
        mygui.getContentPane ().add (myfloat);
        mygui.getContentPane ().add (myshort);
        mygui.getContentPane ().add (mylong);
        mygui.getContentPane ().add (mybyte);
        mygui.getContentPane ().add (mydbl);
        mygui.getContentPane ().add (mybool);
        mygui.getContentPane ().add (scrolla);

        myarea.append ("sdfgsdfgsdfgsf\n");

        mygui.setVisible (true);
        mygui.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top