Results 1 to 9 of 9
  1. #1
    razmyasdfg is offline Member
    Join Date
    Jul 2008
    Location
    London (Kingsbury)
    Posts
    41
    Rep Power
    0

    Default Adding JButton to JPanel

    Hi guys

    i have extended a class to JPanel to create a image panel witch is loading a image as a background code is follows

    Java Code:
    
    public class ImagePanel extends JPanel {
    	
    	private Image img;
    	
    	public ImagePanel (Image img){
    		this.img = img;
    		Dimension size = new Dimension(img.getWidth(null),img.getHeight(null));
    		setSize(size);
    		setPreferredSize(size);
    		setMaximumSize(size);
    		setMinimumSize(size);
    		setLayout(null);
    	}
    	public void paintComponent(Graphics g){
    		g.drawImage(img,0,0,null);
    	}
    and then i use this panel to my JFrame
    i wanted to add a JLabel and JButton to this panel. i have added both of them. there is no error in the code but when i run the program it doesn't show the Jbutton and JLabel

    Code..

    Java Code:
    private static final long serialVersionUID = 1L;
    	static ImagePanel panel = new ImagePanel(new ImageIcon("res/back10.png").getImage());
    	static JLabel username = new JLabel("User Name");
    	static JButton ok = new JButton("ok");
    	
    	public JWinTest(){
    		
    		super("Enter Username | Password");
    		getContentPane().add(panel);
    		
    		panel.add(username);
    		panel.add(ok);
    		
    		setVisible(true);
    		
    	}
    	
    	public static void main(String[] args){
    		
    		JWinTest t = new JWinTest();
    		t.getContentPane().add(panel);
    		t.pack();
    		t.setVisible(true);
    	}
    if any one know the solution please post
    it would be great full
    thank you guys for your time

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You are adding all sorts of things to the frame's content pane in this order:

    panel (with the image)
    username
    ok
    panel (again)

    Notice that t has a BorderLayout (that's the default for a frame's content pane) and the effect of add() is to place the component being added into the middle of the pane replacing whatever was there. So adding four things the way you do will mean that only the last remains added.

    ---------

    Adding panel twice betrays a little desparation. You might find it useful to have a read of the Swing section of Oracle's tutorial or some other textbook to see how layouts work and what options are available.

  3. #3
    razmyasdfg is offline Member
    Join Date
    Jul 2008
    Location
    London (Kingsbury)
    Posts
    41
    Rep Power
    0

    Default

    is there anyway to add username and ok button to top of that panel

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

    Default

    You can nest JPanels, each one of them having its own layout. So create JPanel, add your username and ok button on it, and then add the JPanel to another JPanel (or the contentPane) that uses BorderLayout in the BorderLayout.NORTH position. But most important -- please read the Oracle Swing tutorial on use of layout managers.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default

    Other points:
    -- Why do you construct an ImageIcon only to retrieve its image? Load the image using ImageIO#read(...) instead.
    -- As Fubarable said, learn to use layout managers. Don't set a null layout to ImagePanel.

    db

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    There is no need to create a custom class for this. The only time you need to do custom painting is when you want to scale the image or manipulate it in some other way.

    To display an image at its actual size just use a JLabel and add an Icon to it. YOu can then set the layout manager of the label and add other components to it the same way you add components to a panel.

  7. #7
    razmyasdfg is offline Member
    Join Date
    Jul 2008
    Location
    London (Kingsbury)
    Posts
    41
    Rep Power
    0

    Default

    thank you guys for your posts. but stile i didnt sort out my problem
    i dont want to use the layouts. i wanted to set the layout to null and i want to place the component (JButtons, JLabels) where i wants

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

    Default

    Quote Originally Posted by razmyasdfg View Post
    thank you guys for your posts. but stile i didnt sort out my problem
    i dont want to use the layouts. i wanted to set the layout to null and i want to place the component (JButtons, JLabels) where i wants
    Well good luck with that then. But I do have to wonder why you are asking for our help if you choose to ignore it. You want to add components to the top of a container, and the best way, the easiest way to do this is by using appropriate layout managers.

  9. #9
    razmyasdfg is offline Member
    Join Date
    Jul 2008
    Location
    London (Kingsbury)
    Posts
    41
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Well good luck with that then. But I do have to wonder why you are asking for our help if you choose to ignore it. You want to add components to the top of a container, and the best way, the easiest way to do this is by using appropriate layout managers.
    i am sorry. i am not intend to ignore any one of your help. well i red layout manager from swing website . i tried spring layout. it did work well. thank you so much guys.

Similar Threads

  1. Adding a jpanel to a customized Jpanel Class
    By trishtren in forum AWT / Swing
    Replies: 7
    Last Post: 04-05-2011, 06:52 PM
  2. Adding Jpanel ontop of another Jpanel
    By Manfizy in forum AWT / Swing
    Replies: 4
    Last Post: 03-05-2011, 10:34 PM
  3. [SWING]Adding JButton(class1) to JPanel(class2)
    By equal in forum New To Java
    Replies: 8
    Last Post: 02-20-2011, 01:09 AM
  4. Adding JButton to a JTable
    By ting.at.net@hotmail.com in forum AWT / Swing
    Replies: 6
    Last Post: 05-26-2009, 03:37 AM
  5. Problem on adding JButton on JPanel NEED HELP
    By boisk in forum AWT / Swing
    Replies: 15
    Last Post: 03-15-2009, 02:27 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
  •