Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 344
  • Last Modified:

Boder layout model needed

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
clearme1
Asked:
clearme1
  • 8
  • 8
1 Solution
 
zzynxSoftware engineerCommented:
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
 
zzynxSoftware engineerCommented:
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
 
clearme1Author Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
clearme1Author Commented:
Hi zzynx,
  Are u their?
0
 
zzynxSoftware engineerCommented:
I'm back. Sorry.
Looking...
0
 
zzynxSoftware engineerCommented:
add

               pack();

before

               show();
0
 
clearme1Author Commented:
No problem!happy that u r back.
0
 
clearme1Author Commented:
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
 
clearme1Author Commented:
Hi zzynx,
     r u their!!!!!!!!!!!
0
 
zzynxSoftware engineerCommented:
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
 
clearme1Author Commented:
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
 
zzynxSoftware engineerCommented:
>> My friends told me that reponses are quick
Very often they are. Sometimes they aren't.
0
 
zzynxSoftware engineerCommented:
>> 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
 
clearme1Author Commented:
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
 
zzynxSoftware engineerCommented:
Thanks for accepting.
:°)
That makes us answering your future questions too.
0
 
clearme1Author Commented:
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

  • 8
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now