Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Adding fixed size picture and button to panel

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    23
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Adding fixed size picture and button to panel

    I need to make a JPanel that contains picture and JButton. JButton should contain an actionListener and when user clicks on it JFileChooser opens and user can select picture and change that one (already being there in JPanel). I can do this one easily (adding picture to JLabel ...) but picture size MUST be fixed( in JLabel it is painted in its originial size) How can I achieve this ? Any advice ?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Adding fixed size picture and button to panel

    Rather than use a JLabel, you can draw the image directly to a JPanel (in the paintComponent method, using the Graphics object drawImage).First load the image using ImageIO, and you can then resize the image using the Image method getScaledInstance. If much of this doesn't make much sense I'd encourage you to read the following: Trail: 2D Graphics (The Java™ Tutorials)

  3. #3
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Adding fixed size picture and button to panel

    Load the image as a BufferedImage using ImageIO#read(...) and construct an ImageIcon with a scaled instance of the image.

    db

  4. #4
    Junior Member
    Join Date
    Aug 2010
    Posts
    23
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Adding fixed size picture and button to panel

    Thanks for replies.

    copeg How can I then add Button to the panel. When I add it,for example

    panel.add(new JButton("ok", BorderLayout.SOUTH)

    it is displayed over image?

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Adding fixed size picture and button to panel

    Leave it up to me to make things more complex than they should be...db's suggestion would be MUCH easier. However, if you wish to draw the images on a JPanel, you can to create a few panels and then add them to a third: eg you would have 3 panels: 1) draws the image 2) holds the button 3) both 1 and 2 are added to this panel and this panel is then added to the JFrame content pane. To try and anticipate another question: if at that point you wish to alter the way the components are displayed, take a look at this: A Visual Guide to Layout Managers
    Last edited by copeg; August 23rd, 2010 at 04:32 PM.

  6. #6
    Junior Member
    Join Date
    Aug 2010
    Posts
    23
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Adding fixed size picture and button to panel

    That is exactly what I did copeg but it does not work properly. Here is code (just change file address to point to some real picture in your documents).
    iimport javax.imageio.ImageIO;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
     
     
    class PicturePanel extends JPanel{
     
    	private BufferedImage b ;
     
    	public PicturePanel() throws IOException{
    		 b=ImageIO.read(new File("C:\\Pictures\\Flag.jpg"));
    		 this.setMinimumSize(new Dimension(300,300));
     
    	}
     
    	protected void paintComponent(Graphics g){
    		super.paintComponents(g);
    		Graphics2D g2=(Graphics2D)g;
    		g2.drawImage(b, 0, 0, 200, 200, null);
    	}
    }
     
    public class Scales{
     
    	public static void main(String[] args) throws IOException{
     
     
    		JFrame f=new JFrame();
     
    		JPanel mainPanel=new JPanel();
    		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
     
    		PicturePanel p=new PicturePanel();
    		mainPanel.add(p);
     
    		JPanel buttonPanel=new JPanel();
    		buttonPanel.add(new JButton("Change Picture"));
    		mainPanel.add(buttonPanel);
     
    		f.setVisible(true);
    		f.add(mainPanel);
    		f.pack();
     
    	}
    }
    Last edited by copeg; August 23rd, 2010 at 05:14 PM. Reason: Please use the code tags

  7. #7
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Adding fixed size picture and button to panel

    Try surrounding your code in [highlight=java] code here [/highlight] tags

  8. The Following User Says Thank You to Brt93yoda For This Useful Post:

    Javabeginner (August 23rd, 2010)

  9. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Adding fixed size picture and button to panel

    What do you mean 'doesn't work properly'? (btw could be just a copy/paste error but the top import will give a compile time error)

  10. #9
    Junior Member
    Join Date
    Aug 2010
    Posts
    23
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Adding fixed size picture and button to panel

    I mean, there is no syntax error It does not show picture completely Just part of it is showed and immediatly below JButton (try to run it with some real "big Size" image ( not small icon or similar)).

  11. #10
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Adding fixed size picture and button to panel

    Try calling setPreferredSize() on the image JPanel.

  12. The Following User Says Thank You to copeg For This Useful Post:

    Javabeginner (August 23rd, 2010)

  13. #11
    Junior Member
    Join Date
    Aug 2010
    Posts
    23
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Adding fixed size picture and button to panel

    That works . Thanks a lot for help.

Similar Threads

  1. Adding panels to a central panel.
    By Johannes in forum AWT / Java Swing
    Replies: 3
    Last Post: July 4th, 2010, 05:31 PM
  2. repaint panel without clearing it
    By enflation in forum Java Theory & Questions
    Replies: 5
    Last Post: June 27th, 2010, 04:00 PM
  3. Limit File Size or Request Size
    By tarek.mostafa in forum Java Servlet
    Replies: 3
    Last Post: June 12th, 2010, 04:28 PM
  4. Limit File Size or Request Size
    By tarek.mostafa in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: June 11th, 2010, 07:21 AM
  5. suggestionbox inside modal panel
    By smackdown90 in forum Web Frameworks
    Replies: 1
    Last Post: April 8th, 2010, 01:16 PM