Solved

Boder layout model needed

Posted on 2004-04-13
16
337 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
 

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now