Results 1 to 5 of 5
  1. #1
    sandysm is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default displaying JSplitPane in JPanel

    hello everybody,
    i wanted Jsplitpane to be added in a Jpanel, which i further added to frame,

    when i add jsplitpane directly to frame, it works well.

    here is the code,

    import javax.swing.*;
    class splitpanedemo{
    JSplitPane jp;
    splitpanedemo(){
    JFrame jf=new JFrame("demo");
    jp=new JSplitPane();
    jf.show();
    jf.setVisible(true);
    jf.add(jp);
    jf.setSize(1500,1500);
    }
    public static void main(String[] args){
    new splitpanedemo();
    }
    }

    but when i want jsplitpane to be added on a panel, which i did through this code

    import javax.swing.*;
    class splitpanedemo{
    JSplitPane jp;
    JPanel jpa;
    splitpanedemo(){
    JFrame jf=new JFrame("demo");
    jpa=new JPanel();
    jp=new JSplitPane();
    jf.show();
    jf.setVisible(true);
    jpa.add(jp);
    jf.add(jpa);

    jf.setSize(1500,1500);
    }
    public static void main(String[] args){
    new splitpanedemo();
    } }

    this won't work well, the output differs,
    the divider won't work well,

    plz help me out of this.
    thank you in advance.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    First of all edit your posting and repost your code using the "Code tags" so the posted code retains its formatting and is readable.

    In the first case the split pane is added to the content pane which uses a BorderLayout. The split pane is added to the CENTER which means it gets all the space available to the BorderLayout.

    In the second case you add the split pane to the panel which uses a FlowLayout by default. In this case the FlowLayout uses the preferred size of the the split pane. Since you haven't added any components to the split pane or set its preferred size you get the results you see. Try setting the preferred size.

    Read Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing) for more information.

  3. #3
    sandysm is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default

    Hello camickr,
    What preferred size should i use, Please reply with a code snippet.
    Thank you
    Last edited by sandysm; 07-24-2010 at 04:54 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Please do us all a favor and avoid use of non-standard abbreviations in your posts here. For many here, English is not their primary language, and also since clarity of communication is paramount if you want to make it easy for others to help you, you really don't want to make others struggle to understand you.

  5. #5
    Prajin's Avatar
    Prajin is offline Senior Member
    Join Date
    Jun 2010
    Location
    Ktm, Nepal
    Posts
    120
    Rep Power
    0

    Default

    Java Code:
    JSplitPane jp;
    JPanel jpa;
    splitpanedemo(){
    JFrame jf=new JFrame("demo");
    jpa=new JPanel();
    jp=new JSplitPane();
    jf.show();
    jf.setVisible(true);
    jpa.add(jp);
    jf.add(jpa);
    jf.setSize(1500,1500);
    This is what you did.

    this won't work well, the output differs,
    the divider won't work well,
    output does differ, you made frame visible first and then added JSplitpane later. Not good idea, add everything first then show frame at the last and yeah use frame.pack().

    -Regards

Similar Threads

  1. Jpanel and displaying graphics
    By jdsflash in forum New To Java
    Replies: 6
    Last Post: 11-21-2009, 02:14 AM
  2. Managing jPanels in jSplitPane
    By calexander in forum Advanced Java
    Replies: 6
    Last Post: 11-13-2008, 08:06 PM
  3. JSplitPane Arranging Components(JTree) Doubt
    By hemanthjava in forum AWT / Swing
    Replies: 0
    Last Post: 08-03-2008, 09:29 AM
  4. JSplitPane nightmare
    By SwinGirl in forum SWT / JFace
    Replies: 1
    Last Post: 05-01-2008, 09:24 PM
  5. I need create a JSplitPane
    By Daniel in forum AWT / Swing
    Replies: 1
    Last Post: 07-05-2007, 07:12 AM

Posting Permissions

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