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

    Default Components getting Scattered when attached to Panel-SSCCE attached

    What can be the reason for, the components getting scattered when added to JPanel?
    I am giving my SSCCE here.
    Java Code:
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Container;
    import java.awt.Dimension;
     
    import javax.swing.AbstractButton;
    import javax.swing.BorderFactory;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
     
     
    public class CreateWindow {
    	public CreateWindow(String module, String fileId){
    			JPanel propertiesPanel = new JPanel(null);
    			propertiesPanel.setPreferredSize(new Dimension(300, 210));
    			JPanel panFile = new JPanel(null);
    			panFile.setBounds(9,25,300,50);
    			JLabel useName = new JLabel(fileId.toUpperCase());
    			useName.setFont(useName.getFont().deriveFont(15f));
    			useName.setBounds(0, 3, 200, 20);
    			panFile.add(useName);
    	       
    	        JPanel createdOn = new JPanel(null);
    	        createdOn.setBounds(9,50,135,50);
    	        JLabel date = new JLabel("X dossier(s) Y fichier(s)");
    	        date.setBounds(0, 3, 200, 20);
    	        createdOn.add(date);
    	       
    	        JPanel filePath = new JPanel(null);
    	        filePath.setBounds(9,80,100,50);
    	        JLabel path = new JLabel("Espace global:");
    	        path.setBounds(0, 3, 200, 20);
    	        filePath.add(path);
    	       
    	        JPanel fileSize = new JPanel(null);
    	        fileSize.setBounds(9,105,35,50);
    	        JLabel size = new JLabel("Espace utilisé:");
    	        size.setBounds(0, 3, 200, 20);
    	        fileSize.add(size);
    	       
    	        JPanel modifiedDate = new JPanel(null);
    	        modifiedDate.setBounds(9,130,130,50);
    	        JLabel date1 = new JLabel("Espace libre:");
    	        date1.setBounds(0, 3, 200, 20);
    	        modifiedDate.add(date1);
    	       
    	        propertiesPanel.add(panFile);
    	        propertiesPanel.add(createdOn);
    	        propertiesPanel.add(filePath);
    	        propertiesPanel.add(fileSize);
    	        propertiesPanel.add(modifiedDate);
    	        buildGUI(propertiesPanel);
    		}
    	public void buildGUI(JPanel panel)
    	  {
    	    JFrame.setDefaultLookAndFeelDecorated(true);
    	    UIManager.put("activeCaption", new javax.swing.plaf.ColorUIResource(Color.LIGHT_GRAY));
    	    JFrame f = new JFrame("BoooDrive");
    	    f.setIconImage(new ImageIcon("save.gif").getImage());//need an image file with black background
    	    changeButtonColor(f.getComponents());
    	    f.getContentPane().setBackground(Color.WHITE);
    	    f.getContentPane().add( panel);
    	    f.getRootPane().setBorder(BorderFactory.createLineBorder(Color.PINK,2));
    	    f.setSize(400,300);
    	    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());
    	      }
    	    }
    	  }
    }
    I call the constructor of the above class from.
    Java Code:
    public class Test {
    	public static void main(String args[]){
    		CreateWindow cw = new CreateWindow("myAccount","demo13");
    	}
     
    }
    Please help.
    Anees

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default

    Can you describe what it is you are trying to do in your code?

    For example why do you use the setBounds() method?
    What layout manager are you using?

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

    Default

    I need to have a frame as shown in the attached image. I have written the code for the same.
    Please help.
    Attached Thumbnails Attached Thumbnails Components getting Scattered when attached to Panel-SSCCE attached-requirement.jpg  

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default

    Did you read my questions?

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

    Default

    I did not use any layout manager, for the reason that, I cannot position the components in the frame at the desired positions.
    Moreover I have no expertise using them.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default

    Here are some ideas to help you debug your code:
    Add a mouse listener to show where displayed components are located by mouse clicks:
    Java Code:
          f.addMouseListener(new MouseAdapter() {
             public void mouseClicked(MouseEvent me) {
                System.out.println("clicked " + me.getPoint());
             }
           });
    Make the labels opaque and given them unique colors:
    Java Code:
            size.setOpaque(true);
            size.setBackground(Color.blue);
    Comment out some of the adds to see if they are interfering with each other:
    // propertiesPanel.add(modifiedDate);


    Learn how to use Layout managers will be the shortest path.
    Last edited by Norm; 08-02-2008 at 10:48 PM.

Similar Threads

  1. add image on panel
    By samiksha.goel in forum AWT / Swing
    Replies: 4
    Last Post: 08-02-2008, 08:38 PM
  2. Undoable Drawing Panel
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 08:52 PM
  3. Problem in getting table on panel
    By adeeb in forum AWT / Swing
    Replies: 0
    Last Post: 06-09-2008, 09:23 PM
  4. Why the panel text changed?
    By ottawalyli in forum AWT / Swing
    Replies: 1
    Last Post: 12-17-2007, 06:56 AM
  5. Help with drag from panel
    By fernando in forum AWT / Swing
    Replies: 2
    Last Post: 08-07-2007, 11:19 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
  •