Unable to bring down the size of a panel


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Unable to bring down the size of a panel

  1. #1
    Join Date
    Dec 2005
    Posts
    1

    Unable to bring down the size of a panel

    1. I am new to Swing and I am trying to put a JButton in to a JPanel. The JPanel is configured to work with BorderLayout. I want the panel to exactly fit the size of the button but I could not do it. The panel is large and there is ugly space sorrounding it.

    2. I am trying to set the rows and columns of the text area. After I set them using setRows and setColumns of JTextArea methods, it shows in the console that they are set when I see them using getRows and getColumns but I could not actually see the text area widening up. It also affects the sizes of the labels...

    Here is the Java code for the Interface.java file
    Code:
    import java.io.*;
    import javax.swing.*;
    import java.awt.*;
    
    
    class JavaFrame extends JFrame{
    
    	JPanel newPanel1 = new JPanel();
    	JPanel newPanel2 = new JPanel();
    	JPanel newPanel3 = new JPanel();
    
    	JTextArea requestTextArea = new JTextArea();
    	JTextArea responseTextArea = new JTextArea();
    
    	JLabel requestLabel = new JLabel("Send commands to the mail server");
    	JLabel responseLabel = new JLabel("Received response from the mail server");
    
    	JButton sendRequest = new JButton("Send to server");
    
    	public JavaFrame(String title){
    		super(title);
    		this.setDefaultLookAndFeelDecorated(true);
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    	}
    
    	void addLabels(){
    
    		//set sizes for labels
    		requestLabel.setPreferredSize(new Dimension(600, 50));
    		responseLabel.setPreferredSize(new Dimension(600, 50));
    
    		//set colors for labels
    		requestLabel.setOpaque(true);
    		responseLabel.setOpaque(true);
    		requestLabel.setBackground(Color.YELLOW);
    		responseLabel.setBackground(Color.YELLOW);
    
    		//set labels for textareas
    		requestLabel.setLabelFor(requestTextArea);
    		responseLabel.setLabelFor(responseTextArea);
    		
    		//add labels to panels
    		newPanel1.add(requestLabel);
    		newPanel3.add(responseLabel);
    		
    	}
    
    	void addButtons(){
    		newPanel2.add(sendRequest, BorderLayout.WEST);
    	}
    
    	void addTextAreas(){
    		//set colors for textareas
    		requestTextArea.setBackground(new Color(200, 100, 100));
    		responseTextArea.setBackground(new Color(200, 100, 100));		
    		requestTextArea.setForeground(Color.WHITE);
    		responseTextArea.setForeground(Color.WHITE);	
    
    		//set rows and columns for textareas
    		requestTextArea.setRows(15);
    		System.out.println(requestTextArea.getRows());
    		System.out.println(requestTextArea.getColumns());
    
    		//add textareas to panels
    		newPanel1.add(requestTextArea);
    		newPanel3.add(responseTextArea);
    	}
    
    	void addComponentsToFrame(){
    		//set layouts and sizes for panels
    		newPanel1.setLayout(new GridLayout(2,0));
    		//newPanel2.setLayout(new GridLayout(0,1));
    		newPanel3.setLayout(new GridLayout(2,0));
    		newPanel2.setPreferredSize(new Dimension(400, 0));
    		System.out.println(newPanel2.getSize().toString());
    
    		//Frame's layout and its components
    		
    		this.getContentPane().add(newPanel1);
    		this.getContentPane().add(newPanel2);
    		this.getContentPane().add(newPanel3);
    	}
    }
    
    
    class Interface{
    	public static void main(String args[]){
    		JavaFrame newFrame = new JavaFrame("Mail Client");
    		
    		//set frame's layout
    		newFrame.setLayout(new GridLayout(3,0));
    
    		//add all components to panels and panels to frame
    		newFrame.addLabels();
    		newFrame.addButtons();
    		newFrame.addTextAreas();
    		newFrame.addComponentsToFrame();
    	
    		//frame's visibility and size
    		newFrame.setVisible(true);
    		newFrame.pack();
    	}
    }
    Last edited by raghavan20; 12-20-2005 at 08:37 AM. Reason: Code edited to give more clarity

  2. #2
    Join Date
    May 2005
    Posts
    75
    Have you tried a null layout? You can set the exact size and location of nearly any swing object.

Similar Threads

  1. queues how to print it
    By xsouldeath in forum Java
    Replies: 0
    Last Post: 12-10-2005, 10:56 PM
  2. Replies: 2
    Last Post: 09-10-2005, 02:56 AM
  3. About the corr. paper size of the report
    By Edwardc in forum VB Classic
    Replies: 0
    Last Post: 05-20-2005, 12:07 AM
  4. Deleteing duplicates integers from a vector!!!
    By Paul Statham in forum Java
    Replies: 0
    Last Post: 11-04-2001, 06:23 AM
  5. Replies: 2
    Last Post: 03-07-2001, 08:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center