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 ?
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)
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
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?
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
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).
Code java:
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();
}
}
Re: Adding fixed size picture and button to panel
Try surrounding your code in [highlight=java] code here [/highlight] tags
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)
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)).
Re: Adding fixed size picture and button to panel
Try calling setPreferredSize() on the image JPanel.
Re: Adding fixed size picture and button to panel
That works . Thanks a lot for help.