Solved

Boder layout model needed

Posted on 2004-04-13
16
340 Views
Last Modified: 2010-03-31
Hi every one,
         I need a boder layout model which looks something  like this :
 
                           (north)
                    command button

                     (center)       (west)
(east)          jtextarea-1      jsplitpane (vertical)
jcombo       jtextarea-2         jtree
box  

                           south
                      commandbutton
can anybody provide a coding for the above.I really have problem while using all the 5 regions of the boder layout.
Thank you.
                       
0
Comment
Question by:clearme1
  • 8
  • 8
16 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 10811595
Start with making a JPanel for all of your 5 regions. (Don't add the button, boxes,... directly into the border layout)

So,

e.g. for your buttons in the north/south

JPanel mainPanel = new JPanel( new BorderLayout() );

JPanel southPanel = new JPanel(); // is FlowLayout/Center by default
southPanel.add(new JButton("My button in the south") );
mainPanel.add(southPanel, BorderLayout.SOUTH);

JPanel northPanel = new JPanel(); // is FlowLayout/Center by default
northPanel.add(new JButton("My button in the north") );
mainPanel.add(northPanel, BorderLayout.NORTH);

Do the same for your west (=left!), center and east (=right!) panels.



0
 
LVL 37

Expert Comment

by:zzynx
ID: 10811620
Doing it this way you can decide to put your buttons in the center or at the left or right by

Center:
JPanel southPanel = new JPanel();

Left:
JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)));

Right:
JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)));
0
 

Author Comment

by:clearme1
ID: 10812221
Hi zzynx,
  I have tried the following code.I am getting blank screen as the result.
I have tried the follwing:

north:
1 jbutton
center
1 text area
west
1 jtree
south
1 jbutton

Can u plz make necessary corrections.
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;


public class client1 extends JFrame {


   JFrame clientframe;
   JButton open,top;
   JTextArea showfile;
   JTree tree3;
   JFileChooser chooserdialog;
   FileReader choosefile;
 
         DefaultMutableTreeNode root=new DefaultMutableTreeNode("chatting Clients");
    DefaultMutableTreeNode child1;
    DefaultMutableTreeNode child2;
    DefaultMutableTreeNode child3;
    DefaultMutableTreeNode child4;
    DefaultMutableTreeNode child5;

   client1(){
         clientframe=new JFrame("CLIENT");
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        


         JPanel clientpanel=new JPanel(new FlowLayout());
         JPanel clientpanel1=new JPanel(new FlowLayout());
         JPanel clientpanel2=new JPanel(new FlowLayout());
          
         JLabel framename=new JLabel("client frame");
       
       child1=new DefaultMutableTreeNode("jenifer");
       child2=new DefaultMutableTreeNode("britney");
       child3=new DefaultMutableTreeNode("maria");
       child4=new DefaultMutableTreeNode("christina");
       child5=new DefaultMutableTreeNode("jones");
       root.add(child1);
          root.add(child2);
       root.add(child3);
       root.add(child4);
       root.add(child5);
       tree3 = new JTree(root);
     

       showfile=new JTextArea(50,50);
       
       clientpanel2.add(tree3);
       getContentPane().add(new JScrollPane(showfile),BorderLayout.CENTER);
        getContentPane().add(clientpanel2,BorderLayout.WEST);


       open=new JButton("OPEN");
       top=new JButton("top");
     
       clientpanel.add(top);
       clientpanel1.add(open);
       

       
     
       getContentPane().add(clientpanel,BorderLayout.NORTH);
       getContentPane().add(clientpanel1,BorderLayout.SOUTH);
     

       show();
       setSize(640,480);

    }
       


    public static void main(String args[]){
            new client1();
      
    }
}


0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:clearme1
ID: 10812702
Hi zzynx,
  Are u their?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10812719
I'm back. Sorry.
Looking...
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10812742
add

               pack();

before

               show();
0
 

Author Comment

by:clearme1
ID: 10812751
No problem!happy that u r back.
0
 

Author Comment

by:clearme1
ID: 10812954
hI zzynx,
     i am getting the output now.I still need ur help.why the use of pack()?I need an explanation for that!I also want to add jsplitpane in the west and then put jtree in it.In my code i have simply added jtree in the west.Can u plz help me.
0
 

Author Comment

by:clearme1
ID: 10813278
Hi zzynx,
     r u their!!!!!!!!!!!
0
 
LVL 37

Accepted Solution

by:
zzynx earned 50 total points
ID: 10813319
I am!
But... - you'll never guess - I have some other work to do too... ;)
Be patient.

about the pack().
This is what the java help says:

"Causes this Window to be sized to fit the preferred size and layouts of its subcomponents.
If the window and/or its owner are not yet displayable, both are made displayable before calculating the preferred size. The Window will be validated after the preferredSize is calculated."

If you don't pack, I think all your components are of size 0.

About splitpane:
This might help, although I'm not completely satisfied. (the scrollpane around the tree doesn't "work" yet)

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


public class client1 extends JFrame {


   JFrame clientframe;
   JButton open,top;
   JTextArea showfile;
   JTree tree3;
   JFileChooser chooserdialog;
//   FileReader choosefile;
 
        DefaultMutableTreeNode root=new DefaultMutableTreeNode("chatting Clients");
    DefaultMutableTreeNode child1;
    DefaultMutableTreeNode child2;
    DefaultMutableTreeNode child3;
    DefaultMutableTreeNode child4;
    DefaultMutableTreeNode child5;

   client1(){
        clientframe=new JFrame("CLIENT");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       


        JPanel clientpanel=new JPanel(new FlowLayout());
        JPanel clientpanel1=new JPanel(new FlowLayout());
        JPanel clientpanel2=new JPanel(new FlowLayout());
         
        JLabel framename=new JLabel("client frame");
       
       child1=new DefaultMutableTreeNode("jenifer");
       child2=new DefaultMutableTreeNode("britney");
       child3=new DefaultMutableTreeNode("maria");
       child4=new DefaultMutableTreeNode("christina");
       child5=new DefaultMutableTreeNode("jones");
       root.add(child1);
       root.add(child2);
       root.add(child3);
       root.add(child4);
       root.add(child5);
       tree3 = new JTree(root);
     

       showfile=new JTextArea(50,50);
       
       JScrollPane theScrollPane = new JScrollPane();
       theScrollPane.setViewportView(tree3);
       clientpanel2.add(theScrollPane);
       getContentPane().add(new JScrollPane(showfile), BorderLayout.CENTER);
       
       
       JSplitPane theSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
       theSplitPane.setResizeWeight(0.33);
       theSplitPane.setBottomComponent(clientpanel2);
       theSplitPane.setTopComponent(new JLabel("Top here"));
       
       getContentPane().add(theSplitPane, BorderLayout.WEST);


       open=new JButton("OPEN");
       top=new JButton("top");
     
       clientpanel.add(top);
       clientpanel1.add(open);
       

       
     
       getContentPane().add(clientpanel,BorderLayout.NORTH);
       getContentPane().add(clientpanel1,BorderLayout.SOUTH);
     
       pack();
       show();
       setSize(640,480);

    }
       


    public static void main(String args[]){
          new client1();
     
    }
}
0
 

Author Comment

by:clearme1
ID: 10813372
Very sorry for not being patient.My friends told me that reponses are quick,that's why i got anxious.
I will see the code send by u and return.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10813428
>> My friends told me that reponses are quick
Very often they are. Sometimes they aren't.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10813443
>> Very sorry for not being patient
Never mind.
I like this more than people asking questions and afterwards you don't hear *anything* of them.
0
 

Author Comment

by:clearme1
ID: 10813586
Being a stranger u r helping me.So,it's my duty to give u full respect and ofcourse time to solve questions.I run the program send by u.It's working.Through this i will start learning
about JSplitpane.Information regarding pack()
was very timely as i have been facing some problems for 3 days now.The session with u was very knowledgeable and i hope to learn few things from it.
Thank you.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10813743
Thanks for accepting.
:°)
That makes us answering your future questions too.
0
 

Author Comment

by:clearme1
ID: 10813810
I am very glad that u r willing to answer my future questions.I really need support from EE for learning java.Ur
last comment was really encouraging and i am looking forward to have good time with EE.Thank you once again for
participating in my question.
Thank you.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
hibernate insert example 13 34
use lov values 2 63
How to convert from xls to xlsx using java 7 55
Java basic valueOf question 1 29
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question