Results 1 to 7 of 7

Thread: Layout Question

  1. #1
    vittoire is offline Member
    Join Date
    Jan 2010
    Posts
    9
    Rep Power
    0

    Default Layout Question

    Hi. im new to the java world. i would like to enquire if it is possible to hav a gridlayout in another gridlayout. for example. i add a grid of 1,2 into the borderlayout. is it possible that i insert another grid into the grid?

    example
    jp1.add(jp2);

    is this possible?

  2. #2
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    If you set a grid layout in a panel, you can add that panel to another panel that has a grid layout, effectively nesting them.

  3. #3
    vittoire is offline Member
    Join Date
    Jan 2010
    Posts
    9
    Rep Power
    0

    Default

    Thanks for the reply & sorry for my late reply. i did the code but i found that there is something wrong with my code. i could compile but the gui does not appear. can someone help me see what is wrong with my code & direct me please. the error happens on jp3.add(jlProcess);. Thanks in advance.

    /**
    * @(#)CpuGenerate.java
    *
    *
    * @author
    * @version 1.00 2010/1/13
    */


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

    public class CpuGenerate extends JFrame{

    JButton jbBack, jbReset, jbGenerate, jbNext;
    JLabel jlProcess, jlArrival, jlPriority, jlBurst, jl1, jl2, jl3;
    JSpinner jsProcess, jsArrival1, jsArrival2, jsPriority1, jsPriority2, jsBurst1, jsBurst2;
    JCheckBox jcbEnable;
    JPanel jp1, jp2, jp3, jp4, jp5, jp6;

    public CpuGenerate() {

    setSize(800,600);

    setTitle("CPU Scheduling");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setLayout(new BorderLayout());

    //initialize panels
    jp1= new JPanel();
    jp2= new JPanel();
    jp3= new JPanel();
    jp4= new JPanel();
    jp5= new JPanel();
    jp6= new JPanel();

    //set panel layout
    jp1.setLayout(new GridLayout(1,2));
    jp2.setLayout(new GridLayout(3,1));
    jp3.setLayout(new GridLayout(1,2));
    jp4.setLayout(new GridLayout(4,4));
    jp5.setLayout(new GridLayout(1,3));
    jp6.setLayout(new FlowLayout());

    //add panels to frame
    add(jp1, BorderLayout.CENTER);
    /* add(jp2, BorderLayout.NORTH);
    add(jp3, BorderLayout.WEST);
    add(jp4, BorderLayout.SOUTH);
    add(jp5, BorderLayout.EAST);*/
    jp1.add(jp2);
    jp1.add(jp6);
    jp2.add(jp3);
    jp2.add(jp4);
    jp2.add(jp5);

    //add components into panel
    jp3.add(jlProcess);
    jp3.add(jsProcess);
    jp4.add(jlArrival);
    jp4.add(jsArrival1);
    jp4.add(jl1);
    jp4.add(jsArrival2);
    jp4.add(jlPriority);
    jp4.add(jsPriority1);
    jp4.add(jl2);
    jp4.add(jsPriority2);
    jp4.add(jcbEnable);
    jp4.add(jlBurst);
    jp4.add(jsBurst1);
    jp4.add(jl3);
    jp4.add(jsBurst2);
    jp5.add(jbBack);
    jp5.add(jbReset);
    jp5.add(jbNext);
    jp6.add(jbNext);

    // Create spinner
    JSpinner jsProcess = new JSpinner();
    JSpinner jsArrival1 = new JSpinner();
    JSpinner jsArrival2 = new JSpinner();
    JSpinner jsPriority1 = new JSpinner();
    JSpinner jsPriority2 = new JSpinner();
    JSpinner jsBurst1 = new JSpinner();
    JSpinner jsBurst2 = new JSpinner();

    //Create Buttons
    jbBack = new JButton("Back");
    jbReset = new JButton("Reset");
    jbGenerate = new JButton("Generate");
    jbNext = new JButton("Next");

    //Create label
    jlProcess = new JLabel("No. Of Process:");
    jlArrival = new JLabel("Arrival Time:");
    jlPriority = new JLabel("Priority:");
    jlBurst = new JLabel("Burst Time:");
    jl1 = new JLabel("To");
    jl2 = new JLabel("To");
    jl3 = new JLabel("To");

    //create combobox
    jcbEnable = new JCheckBox("Enable");



    // Set spinner value
    jsProcess.setValue(new Integer(1));
    jsArrival1.setValue(new Integer(1));
    jsArrival2.setValue(new Integer(2));
    jsPriority1.setValue(new Integer(1));
    jsPriority2.setValue(new Integer(2));
    jsBurst1.setValue(new Integer(1));
    jsBurst2.setValue(new Integer(2));



    setVisible(true);

    }

    public static void main(String[] args)
    {


    JFrame.setDefaultLookAndFeelDecorated(true);
    CpuGenerate frame = new CpuGenerate();
    }

    }

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    1.) Use code tags when posting code
    2.) Did you check the console for error messages/exceptions? You should be getting a Nullpointer on jp3.add(jlProcess); because jlProcess will be null at that point.

  5. #5
    vittoire is offline Member
    Join Date
    Jan 2010
    Posts
    9
    Rep Power
    0

    Default

    Hi, sorry im quite new here. so i didnt know that im suppose to use code tags when posting my codes.

    im quite confused about the checking console for error messages. Do you mind explaining about the nullpointer?

    the error i gt was

    Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl(Container.java:1041)
    at java.awt.Container.add(Container.java:365)
    at CpuGenerate.<init>(CpuGenerate.java:59)
    at CpuGenerate.main(CpuGenerate.java:128)

  6. #6
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by vittoire View Post
    Do you mind explaining about the nullpointer?
    You can't add the components to the container until you've initialised them. jlProcess is null until you initialise it with a new JLabel. If you try to use it before it has been initialised, for example by adding it to a panel, you'll get a NullPointerException.

  7. #7
    vittoire is offline Member
    Join Date
    Jan 2010
    Posts
    9
    Rep Power
    0

    Default

    Thanks alot dlorde and r035198x. i realise what my mistakes were.

Similar Threads

  1. Edit layout Layout please help me
    By manhtungtnk28@gmail.com in forum New To Java
    Replies: 4
    Last Post: 11-23-2009, 08:41 AM
  2. Card Layout
    By Gilbee in forum NetBeans
    Replies: 3
    Last Post: 03-03-2009, 09:37 PM
  3. Border Layout
    By mark-mlt in forum Java Applets
    Replies: 5
    Last Post: 05-12-2008, 09:48 AM
  4. JGraph Layout Pro 1.4.0.3
    By levent in forum Java Software
    Replies: 0
    Last Post: 07-31-2007, 06:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •