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.


>> REGISTER NOW TO START POSTING


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
    Member
    Join Date
    May 2013
    Posts
    42
    Thanks
    0
    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() 
        {
            initComponents();
     
            //JSlider setup
            jSlider = new JSlider(JSlider.HORIZONTAL, min, max, initial);
            jSlider.setMajorTickSpacing(10);
            jSlider.setMinorTickSpacing(1);
            jSlider.setPaintLabels(true);
            jSlider.setPaintTicks(true);
            jSlider.setValue(50);
     
            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 = ");
            jLabel4.setText("");
     
            //adding components
            this.add(jSlider);
            this.add(jLabel1);
            this.add(jLabel4);
            this.add(jLabel2);
            this.add(jText1);
            this.add(jLabel3);
            this.add(jText2);
            //this.add(drawShape);
     
            setLayout(new FlowLayout());
            repaint();
        }
     
        public void setShape(MyShape suppliedShape)
        {       
            theShape = suppliedShape;
        }
        public MyShape getShape()
        {
            return theShape;
        }
     
            public class MyChangeAction implements ChangeListener
            {
                @Override
                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);
                    jLabel4.setText(str);
     
                    area = theShape.getArea(sliderValue);
                    boundaryLength = theShape.getBoundaryLength(sliderValue);
     
                    jText1.setText(Double.toString(Math.round(boundaryLength)));
                    jText2.setText(Double.toString(Math.round(area))); 
                    add(drawShape);
                }            
            }
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
            this.setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 400, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .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:

    Currently:


    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
    Location
    Italy
    Posts
    443
    Thanks
    4
    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.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy 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