Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: BorderLayout makes everything disappear

  1. #1
    Join Date
    May 2013
    Thanked 5 Times in 1 Post

    Exclamation BorderLayout makes everything disappear

    Hey, so when i attempt to use the BorderLayout, then run the program nothing is shown in the window, my slider/textboxes and labels are gone. Then when i revert back to using a FlowLayout, they come back.

    package calculations;
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    public class MyControlPanel extends javax.swing.JPanel
        private MyShape theShape;//creating instance of MyShape
        static int min = 0;//minimum value for jSlider
        static int max = 100;//maximum value for jSlider
        static int initial = 0;//inital value for jSlider
        //creating components
        JSlider jSlider;
        JLabel jLabel1;
        JLabel jLabel2;
        JLabel jLabel3;
        JLabel jLabel4;
        JTextField jText1;
        JTextField jText2;
        private int shapeSize;//used to hold sliderValue  
        DrawShape drawShape = new DrawShape(20,20,shapeSize,theShape);
        public MyControlPanel() 
            //JSlider setup
            jSlider = new JSlider(JSlider.HORIZONTAL, min, max, initial);
            jSlider.addChangeListener(new MyChangeAction());
            //create instance of jTexts          
            jText1 = new JTextField();
            jText2 = new JTextField();
            //setText for jTexts
            jText1.setText("          ");
            jText2.setText("          ");
            //create instance of labels
            jLabel1 = new JLabel();
            jLabel2 = new JLabel();
            jLabel3 = new JLabel();
            jLabel4 = new JLabel();
            //setText for jLabels
            jLabel1.setText("Shape Dimension = ");
            jLabel2.setText("     Boundary Length = ");
            jLabel3.setText("     Area = ");
            //adding components
            setLayout(new FlowLayout());
        public void setShape(MyShape suppliedShape)
            theShape = suppliedShape;
        public MyShape getShape()
            return theShape;
            public class MyChangeAction implements ChangeListener
                public void stateChanged(ChangeEvent e) 
                    DrawShape drawShape = new DrawShape(20,20,shapeSize,theShape);
                    int sliderValue = jSlider.getValue();
                    shapeSize = sliderValue;
                    double area;
                    double boundaryLength;
                    String str = Double.toString(sliderValue);
                    area = theShape.getArea(sliderValue);
                    boundaryLength = theShape.getBoundaryLength(sliderValue);
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
                .addGap(0, 400, Short.MAX_VALUE)
                .addGap(0, 300, Short.MAX_VALUE)
        }// </editor-fold>                        
        // Variables declaration - do not modify                     
        // End of variables declaration                   

    this is my code with the FlowLayout, can someone tell me how i would implement the BorderLayout correctly, so that my program looks something like this:


    What i would like:

    Slider/textboxes and labels at the bottom, but the paint panel at above it.

    Thanks in advance! Any help would be great!

  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Thanked 122 Times in 114 Posts

    Default Re: BorderLayout makes everything disappear

    Quote Originally Posted by jason3460 View Post
    can someone tell me how i would implement the BorderLayout correctly
    You should take some time to learn, at least, the most basic layout managers like BorderLayout, FlowLayout, GridLayout (and put also GridBagLayout). Each of these has its specific features, restrictions and so on.
    Without a good background on these concepts you will always have doubts and you won't understand "why" things happen in that way.

    FlowLayout is very simple, it can manage any number of components. As the name "Flow" implies, FlowLayout arranges components in one row and if it's not sufficient, components can "flow" on the next row. Exactly like words flow in a word processor or a web page.

    BorderLayout can manage only 5 components. They are arranged in 5 areas whose names are conceptually: "north", "south", "east", "west", "center". Only 1 component can appear in one area.
    When you want to add a component in a BorderLayout you should write code like this (container is the container with the BorderLayout, can be implicitly the this if you are extending eg. JPanel):

    container.add(comp1, BorderLayout.NORTH);
    container.add(comp2, BorderLayout.CENTER);

    NORTH, CENTER, EAST, etc... are constants in BorderLayout for "absolute" positioning (the east is always .... at east). There are other constants for "relative" positioning. But I don't want to go further. You need a background on these things, so please read:

    How to Use BorderLayout
    How to Use FlowLayout
    and in general: Laying Out Components Within a Container

    If you don't understand these concepts you won't go further.
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

Similar Threads

  1. [SOLVED] Sinple GUI Program which uses the BorderLayout.
    By javahelp123456 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 3rd, 2012, 03:25 PM
  2. Replies: 3
    Last Post: August 3rd, 2012, 10:40 AM
  3. BorderLayout and getHeight/getWidth
    By DOLZero in forum Java Theory & Questions
    Replies: 2
    Last Post: May 5th, 2012, 08:59 AM
  4. Replies: 6
    Last Post: February 21st, 2012, 09:41 PM
  5. BackgroundPanel.java cannot be placed in BorderLayout.NORTH
    By chopficaro in forum AWT / Java Swing
    Replies: 7
    Last Post: August 31st, 2010, 06:27 PM