Results 1 to 3 of 3
  1. #1
    Zamioculcas is offline Member
    Join Date
    Mar 2011
    Posts
    20
    Rep Power
    0

    Default How can I put components with different size below each other?

    Dear Readers,


    I have a JPanel object which contains three components next to each other in the same line. (JLabel, JTextField, JBUtton).
    Then I created a bigger JTextPane.

    I want to add both the previous JPanel and the JTextPane into a new JPanel. And I would like to arrange them below each other in one column. I place the components with the GridLayout.

    My problem is the the size of the first panel (with the label,field, button), it is too unwieldy and elephantine:) I want to reduce the size of the first panel.
    I try to call the preferredSize(Dimension d) method but it doesn't works in my code.

    How can I achieve the smaller panel ?

    I enclosed my code below.

    Thanks in advance for your response.


    Java Code:
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.JScrollPane;
    import javax.swing.JTextPane;
    
    import java.awt.GridLayout;
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    
    
    public class RegexpFrame{
    
    JFrame frame;
    JPanel firstPanel;
    JPanel textPanel;
    public RegexpFrame()
    	{
    	   frame = new JFrame("regexp Text");
    	   initFirstPanel();	    	
    	   initTextPanel();
    	   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	   frame.setSize(400,500);
    	   frame.setVisible(true);	
    	   	   
    	}
    
    public void initFirstPanel()
    	{
    	   firstPanel = new JPanel();
    	   JLabel fajlname = new JLabel("name: ");
    	   JTextField text = new JTextField(12);
    	   JButton jenson = new JButton("OK");
    	   firstPanel.setLayout(new GridLayout(1,3,2,2));	
    	   firstPanel.add(fajlname);
    	   firstPanel.add(text);
    	   firstPanel.add(jenson);
    	}
    
    public void initTextPanel()
    	{
    	  textPanel = new JPanel();
    	  textPanel.setLayout(new GridLayout(2,1,2,2));
    	  JTextPane textPane = new JTextPane();
    	  JScrollPane scroller = new JScrollPane(textPane);	
    	  textPane.setPreferredSize(new Dimension(400,200));
    	  	   firstPanel.setPreferredSize(new Dimension(400,30));
    	  textPanel.add(firstPanel);
    	  textPanel.add(scroller);	  
    	  frame.getContentPane().add(textPanel,BorderLayout.NORTH);
    	  
    	  		   	
    	}

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    One solution: have a look at using a BoxLayout. Also, remember that you can nest layouts by nesting JPanels, each using a unique layout manager.

  3. #3
    Zamioculcas is offline Member
    Join Date
    Mar 2011
    Posts
    20
    Rep Power
    0

    Default

    Thank for your quick response. I replace GridLayout with BoxLayout at the given panel. (I created a BOxLayout for the panel) And I choice the Y_AXIS parameter of that layout manager. Y_AXIS places my swing components vertically from top to bottom.

    here is the portion of the code after correction:

    textPanel = new JPanel();
    textPanel.setLayout(new BoxLayout(textPanel,BoxLayout.Y_AXIS));
    ...
    textPanel.add(firstLabel);
    ...
    frame.getContentPane().add(textPanel,BorderLayout. NORTH);

Similar Threads

  1. Replies: 4
    Last Post: 08-05-2010, 08:08 AM
  2. change text size in all GUI components
    By itaipee in forum AWT / Swing
    Replies: 2
    Last Post: 08-07-2009, 04:59 AM
  3. JFrame 's components size and location problem
    By petrosgraf in forum Threads and Synchronization
    Replies: 5
    Last Post: 04-18-2009, 02:24 AM
  4. Replies: 1
    Last Post: 06-06-2008, 07:55 AM
  5. gridbaglayout: increase/decrease size of components.
    By newtojava7 in forum New To Java
    Replies: 2
    Last Post: 01-28-2008, 07:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •