Results 1 to 5 of 5
  1. #1
    aneesahamedaa is offline Member
    Join Date
    Jul 2008
    Posts
    26
    Rep Power
    0

    Default How to add more components to main panel

    Hi all,
    I am posting an SSCE. Please find the image(used in code) attached with this thread.
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    
    import javax.swing.AbstractAction;
    import javax.swing.AbstractButton;
    import javax.swing.Action;
    import javax.swing.BorderFactory;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
     
     
    public class CreateWindow {
        public CreateWindow(String module, String id){
        	if(module.equals("mail")){
        		JPanel mail = new JPanel(null);
        		mail.setPreferredSize(new Dimension(500, 350));
        		//Color colr = new Color(222, 236, 255);
        		mail.setBackground(Color.WHITE);
        		JLabel file = new JLabel("File Name");
        		//file.setBounds(18,25,75,50);
        		
        		// Retrieve the icon
        		Icon icon = new ImageIcon(ClassLoader.getSystemResource("image.gif"));
        	    
        	    // Create an action with an icon
        	    Action action = new AbstractAction("", icon) {
        	        // This method is called when the button is pressed
        	        public void actionPerformed(ActionEvent evt) {
        	            // Perform action
        	        }
        	    };
        	    
        	    // Create the button; the icon will appear to the left of the label
        	    JButton button = new JButton(action);
        	    //button.setBounds(18,25,75,50);
        	    JPanel panel = new JPanel(new BorderLayout());
        	    panel.setBackground(Color.WHITE);
        	    panel.add(button, BorderLayout.NORTH);
        	    panel.add(file, BorderLayout.SOUTH);
        	    panel.setBounds(18,25,75,75);
        		mail.add(panel);
        		buildGUI(mail,500, 350);
            }
        }
        public void buildGUI(JPanel panel, int width, int height)
          {
            JFrame.setDefaultLookAndFeelDecorated(true);
            UIManager.put("activeCaption", new javax.swing.plaf.ColorUIResource(Color.LIGHT_GRAY));
            JFrame f = new JFrame("Propriétés:");
            changeButtonColor(f.getComponents());
            f.getContentPane().setBackground(Color.WHITE);
            f.getContentPane().add(panel);
            f.getRootPane().setBorder(BorderFactory.createLineBorder(Color.PINK,2));
            f.setSize(width,height);
            f.setLocationRelativeTo(null);
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            f.setVisible(true);
            }
          public void changeButtonColor(Component[] comps)
          {
            for(int x = 0, y = comps.length; x < y; x++)
            {
              if(comps[x] instanceof AbstractButton)
              {
                ((AbstractButton)comps[x]).setBackground(Color.LIGHT_GRAY);
                ((AbstractButton)comps[x]).setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
              }
              else if (comps[x] instanceof Container)
              {
                changeButtonColor(((Container)comps[x]).getComponents());
              }
            }
          }
          public static void main(String args[]){
      		CreateWindow cw = new CreateWindow("mail","Test.doc");
      	}
    }
    When I use the code "mail.add(panel);" I have a panel added to the main panel . Similarly, how can I add many panels to this main panel, such that each of them come side by side and have a reasonable spacing between them and when one row finishes, it should go to the next row.Please use the same "panel" which I used in "mail.add(panel);" to demonstrate, how to add panels side by side in the main panel.
    Please run the above given SSCE , using the image attached so that, my question will be crystal clear.
    Anees
    Attached Thumbnails Attached Thumbnails How to add more components to main panel-ei0021-48.gif  
    Last edited by aneesahamedaa; 08-12-2008 at 09:06 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Take a look at some of the Layout manager classes. For example the GridLayout many do what you want.

  3. #3
    aneesahamedaa is offline Member
    Join Date
    Jul 2008
    Posts
    26
    Rep Power
    0

    Default

    I will describe my question in short.

    1)I need to have a main Jpanel with 'n' child Jpanels added to it, where n is a variable.
    2)Inside the child Jpanels, there will be jbuttons.
    3)There should be uniform spacing between each child Jpanel.
    4)I need to add main Jpanel in a frame.
    5)When there are more child Jpanels to the right(horizontal) or to the bottom(vertical),there has to be horizontal and vertical scroll bars .

    Please give a sample code.
    Anees

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    At least search some examples on the web and try to workout something. You can easily found lots of such examples on the web.

  5. #5
    Join Date
    Aug 2008
    Posts
    1
    Rep Power
    0

    Default

    Hi anees,

    i am getting your point.It's simple you want to use two panels for your flexibility...that is for main panel with some child Jpanels for that you can use...Flowlayout.....It will add the n components side by side as well as if row completes it will goes to the next.so your entire jframe u can use flowlayout...and for that one specific Jpanel...with n now of child Jpanels...you can use BorderLayout it will caotains all the methods for constant gap betwwen the components....

    for example,
    for horizantal gap setHgap(int);
    for vertical gap setVgap(int);
    for gaps you can use this two functions....

    am implementing the same kind 6 months before....so i will hope it works fine...

    ---Ashokkumar.

Similar Threads

  1. Replies: 5
    Last Post: 08-02-2008, 08:05 PM
  2. add image on panel
    By samiksha.goel in forum AWT / Swing
    Replies: 4
    Last Post: 08-02-2008, 07:38 PM
  3. Help with drag from panel
    By fernando in forum AWT / Swing
    Replies: 2
    Last Post: 08-07-2007, 10:19 PM
  4. Replies: 1
    Last Post: 08-07-2007, 07:10 AM
  5. Replies: 1
    Last Post: 08-06-2007, 09:11 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
  •