Hi
I am not able to set the position of Jpanel, having a tough time.

Code is attached.
1. How can I reduce the gap between
To and textbox
Cc and textbox
etc
2. How can I reduce the vertical space between Send/Contact/Spell/.... and the row containing To with textbox
I read tutorial like
http://www.leepoint.net/notes-java/GUI/layouts/30gridlayout.html
but no avail
Someone please help me
Thanks

Attachments
import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 
import java.util.*; 

public class EmailWindow2 extends JFrame implements ActionListener 
{ 

       private JTextField jtfto, jtfcc, jtfbcc, jtfsub; 
       private JTextArea textArea; 
       
       public EmailWindow2() { 
       
       this.setSize(new Dimension(540,380)); 
       this.setVisible(true); 
       
       //create new panel p1.nest it with grdlayout.
       this.getContentPane().setLayout(new BorderLayout()); 
       JPanel p1 = new JPanel(new GridLayout(1, 6)); 
       
       
       //custom panel j, created.
       JPanel j = new JPanel(new BorderLayout());
       
       p1.add(new JButton("Send"));
       
       
       p1.add(new JButton("Contacts"));
       p1.add(new JButton("Spell"));
       p1.add(new JButton("Attach"));
       p1.add(new JButton("Security"));
       p1.add(new JButton("Save"));
	   
	   j.add(p1,BorderLayout.NORTH);
	   
	   
       JPanel p2 = new JPanel(); 
       p2.setLayout(new GridLayout(5,2)); 
       //p2.setLayout(new GridLayout(5,116)); 
              
       p2.add(new JLabel("To:")); 
       p2.add(jtfto = new JTextField(20)); 
       p2.add(new JLabel("Cc:")); 
       p2.add(jtfcc = new JTextField(20)); 
       p2.add(new JLabel("Bcc:")); 
       p2.add(jtfbcc = new JTextField(20)); 
       p2.add(new JLabel("Subject:")); 
       p2.add(jtfsub = new JTextField(20));
       
       this.getContentPane().setLayout(new GridLayout(3,1)); 
       this.getContentPane().add(j); 
       this.getContentPane().add(p2); 
       this.setVisible(true);
 
       Container contentPane = this.getContentPane ();
       textArea = new JTextArea ();
       JScrollPane s = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
       s.setSize(new Dimension(100,100));
       contentPane.add(s, BorderLayout.SOUTH);
             
                               
       
       JMenuBar x = new JMenuBar(); 
       this.setJMenuBar(x); 
       
       //Creation of Various Menu's. 
       JMenu file = new JMenu("File"); 
       x.add(file);
       file.setMnemonic(KeyEvent.VK_F);
       JMenu edit = new JMenu("Edit"); 
       x.add(edit);
       edit.setMnemonic(KeyEvent.VK_E); 
       JMenu view = new JMenu("View"); 
       x.add(view);
       view.setMnemonic(KeyEvent.VK_V);
       JMenu insert = new JMenu("Insert"); 
       x.add(insert);
       insert.setMnemonic(KeyEvent.VK_I);
       JMenu format = new JMenu("Format"); 
       x.add(format);
       format.setMnemonic(KeyEvent.VK_O);                        
       JMenu tools = new JMenu("Tools"); 
       x.add(tools);                         
       tools.setMnemonic(KeyEvent.VK_T);
       JMenu help = new JMenu("Help"); 
       x.add(help); 
       help.setMnemonic(KeyEvent.VK_H);
         
       //File Menu: Sub Menu(s). 
       JMenu create = new JMenu("New"); 
       file.add(create);
       create.setMnemonic(KeyEvent.VK_N); 
         
       //File Menu: Menu Items. 
       JMenuItem msg = new JMenuItem("Message"); 
       create.add(msg);
       msg.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_N, ActionEvent.CTRL_MASK));
       JMenuItem addbk = new JMenuItem("Address Book Card"); 
       create.add(addbk);

       file.addSeparator();
        
       JMenuItem save = new JMenuItem("Save"); 
       file.add(save);
       save.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_S, ActionEvent.CTRL_MASK));
        
       JMenuItem saveas = new JMenuItem("Save As..."); 
       file.add(saveas);
       
       file.addSeparator(); 
       JMenuItem sendnow = new JMenuItem("Send Now"); 
       file.add(sendnow); 
       JMenuItem sendlatr = new JMenuItem("Send Later"); 
       file.add(sendlatr);
       
       file.addSeparator(); 
       JMenuItem exit = new JMenuItem("Exit"); 
       file.add(exit); 
       
       //Edit Menu: Menu Items. 
       JMenuItem undo = new JMenuItem("Undo"); 
       edit.add(undo); 
       undo.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_Z, ActionEvent.CTRL_MASK));
       JMenuItem redo = new JMenuItem("Redo"); 
       edit.add(redo);
       redo.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_Y, ActionEvent.CTRL_MASK)); 
       
       edit.addSeparator();
       JMenuItem cut = new JMenuItem("Cut"); 
       edit.add(cut);
       cut.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_X, ActionEvent.CTRL_MASK));
       JMenuItem copy = new JMenuItem("Copy"); 
       edit.add(copy);
       copy.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_C, ActionEvent.CTRL_MASK));
       JMenuItem paste = new JMenuItem("Paste"); 
       edit.add(paste);
       paste.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_V, ActionEvent.CTRL_MASK));
	   
	   edit.addSeparator();
       JMenuItem sel = new JMenuItem("Select All"); 
       edit.add(sel); 
       sel.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_A, ActionEvent.CTRL_MASK));
       JMenuItem find = new JMenuItem("Find"); 
       edit.add(find);
       find.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_F, ActionEvent.CTRL_MASK));
       
        //View Menu: Menu Items. 
       JMenu tbar = new JMenu("Toolbars"); 
       view.add(tbar);
       tbar.setMnemonic(KeyEvent.VK_T);  
       JMenuItem tbars = new JMenuItem("Mail Toolbar"); 
       tbar.add(tbars); 
       JMenuItem statbar = new JMenuItem("Status Bar"); 
       tbar.add(statbar); 
       
       //Insert Menu: Menu Items. 
       JMenuItem pic = new JMenuItem("Image"); 
       insert.add(pic); 
       JMenuItem url = new JMenuItem("Link"); 
       insert.add(url); 
       JMenuItem sym = new JMenuItem("Characters and Symbols..."); 
       insert.add(sym); 
    
       //Format Menu: Sub-Menu's. 
       JMenu font = new JMenu("Font"); 
       format.add(font);
       font.setMnemonic(KeyEvent.VK_F);  
       JMenu size = new JMenu("Text Size"); 
       format.add(size); 
       size.setMnemonic(KeyEvent.VK_T);
       JMenu colr = new JMenu("Text Colour"); 
       format.add(colr);
       colr.setMnemonic(KeyEvent.VK_C);  
       JMenu align = new JMenu("Align Text"); 
       format.add(align); 
	   align.setMnemonic(KeyEvent.VK_A); 
	   
       //Format Menu: Sub-Menu Font Items. 
       JMenuItem a = new JMenuItem("Arial"); 
       font.add(a); 
       JMenuItem ab = new JMenuItem("Arial Black"); 
       font.add(ab); 
       JMenuItem cs = new JMenuItem("Comic Sans"); 
       font.add(cs); 
       JMenuItem cn = new JMenuItem("Courier New"); 
       font.add(cn); 
       JMenuItem tnr = new JMenuItem("Times New Roman"); 
       font.add(tnr); 
       JMenuItem v = new JMenuItem("Verdana"); 
       font.add(v); 
      
       //Format Menu: Sub-Menu Size Items. 
       JMenuItem small = new JMenuItem("Small"); 
       size.add(small); 
       JMenuItem med = new JMenuItem("Medium"); 
       size.add(med); 
       JMenuItem large = new JMenuItem("Large"); 
       size.add(large); 
   
       //Format Menu: Sub-Menu Colour Items. 
       JMenuItem red = new JMenuItem("Red"); 
       colr.add(red); 
       JMenuItem grn = new JMenuItem("Green"); 
       colr.add(grn); 
       JMenuItem blu = new JMenuItem("Blue"); 
       colr.add(blu); 
       JMenuItem org = new JMenuItem("Orange"); 
       colr.add(org); 
       
       //Format Menu: Sub-Menu Align Items. 
       JMenuItem lft = new JMenuItem("Left"); 
       align.add(lft); 
       JMenuItem rgt = new JMenuItem("Right"); 
       align.add(rgt); 
       JMenuItem cen = new JMenuItem("Center"); 
       align.add(cen); 
       
       //Tools Menu: Menu Items. 
       JMenuItem spell = new JMenuItem("Check Spelling"); 
       tools.add(spell);
       tools.addSeparator(); 
       
       JMenuItem mailnews = new JMenuItem("Mail & Newsgroups"); 
       tools.add(mailnews); 
       JMenuItem addrbk = new JMenuItem("Address Book"); 
       tools.add(addrbk);
       
       tools.addSeparator(); 
       JMenuItem accset = new JMenuItem("Account Settings..."); 
       tools.add(accset); 
       JMenuItem opt = new JMenuItem("Options..."); 
       tools.add(opt); 
       
       //Help Menu: Menu Items. 
       JMenuItem hlp = new JMenuItem("Help"); 
       help.add(hlp); 
       JMenuItem abt = new JMenuItem("About"); 
       help.add(abt); 

       msg.addActionListener(this); 
       addbk.addActionListener(this); 
       save.addActionListener(this); 
       saveas.addActionListener(this); 
       sendnow.addActionListener(this); 
       sendlatr.addActionListener(this); 
       exit.addActionListener(this); 
       
        
       } 
          
      public void actionPerformed(ActionEvent a) {
      	 
       if(a.getActionCommand().equals("Exit")) { 
           System.exit(0); 
       } 
       else if(a.getActionCommand().equals("Message")) { 
           System.out.println("New Message Window."); 
       } 
       else if(a.getActionCommand().equals("Address Book Card")) { 
           System.out.println("New Address Book entry Created."); 
       } 
       else if(a.getActionCommand().equals("Save")) { 
           System.out.println("Message Saved."); 
       } 
       else if(a.getActionCommand().equals("Save As")) { 
           System.out.println("Message Saved To Specified Path."); 
       } 
       else if(a.getActionCommand().equals("Send Now")) { 
           System.out.println("Message Sent Successfully!"); 
       } 
       else if(a.getActionCommand().equals("Send Later")) { 
           System.out.println("Message Saved To Outbox Folder."); 
           System.exit(0); 
       } 
       else if(a.getActionCommand().equals("Save As")) { 
           System.out.println("Message Saved In Specified Path."); 
       } // CHANGE ACTION COMMANDS BELOW!!!!!!!!!!!!!!!!!!! 
       else {

You mean to say I have no control over the display
Then how do I do this?

You mean to say I have no control over the display
Then how do I do this?

Jwenting just said "It's pretty much out of your hands."
:confused:


I don't see what the problem is, how it looks is just pointless fluff. If it works it works. What more do you want.

you have absolute control over the way the components are displayed in relation to each other.
You have very little control over how (what size and visual style) they're displayed at.

This is deliberate to enable the JVM to make it look decent on different operating systems that have different ways of doing things.
If you go and set that an input control is X pixels by Y pixels with a border of Z pixels that may look great on your Windows machine but completely crappy on a Mac or X terminal.
It may even be impossible to render there at all at that size.

Greetings:
You could try with GridBagLayout, it allows you to control a little more the appereance by using weights for the spaces that the components use, something like this:

GridBagLayout gridbag = new GridBagLayout();
this.getContentPane().setLayout(gridbag);
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
//to add components you just asign a grid position and a weight
g.gridx=0;c.gridy=0;c.weightx=2;c.weighty=0;this.getContentPane().add(component,c);

Here is the description of GridbagLayout from java.sun, http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbag.html

You could always set the layout to null and set the bounds, but I don't know if that's a good idea or not...... That is, if you are talking about positioning components.

NullLayout is never recommended except on platforms where there is no other layout manager available (maybe some mobile platforms).

This article has been dead for over six months. Start a new discussion instead.