Solved

Boder layout model needed

Posted on 2004-04-13
16
339 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
what is a "java.lang.System Property"   ? 20 67
servlet requestdispatcher include and forward differences 1 31
factorial example 4 40
jdbc error in jsp application 20 45
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

809 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