I'm trying to create a simple map that will create a circle on a certain coordinate. I want to use an image with it's fixed size as the map and add it to a JScrollPane. My problem here is that I dont know how to add a scrollable image.
Well you need a way to display that image. Is the image embedded in a component?
For example you draw the image in a JPanel
The JPanel can then be added to JScrollPane. When you open your window you open it to the size you want and the scrollbars will be there.
Add your image to a JLabel, then add that JLabel to your JScrollPane. If the size of the image is greater than the size of your JScrollPane, the scrollbars will appear. For example:
final Image image = ...
final JLabel imageLabel = new JLabel(new ImageIcon(image));
final JScrollPane scroll = new JScrollPane(imageLabel);
container.add(scroll, ...);
Related
I'd like to add JLabels dynamically in a JPanel vertically like the image that I've attached. After loading all images, I need to select an image, then selected the image should appear in another JPanel. I am reading Images from an ArrayList which contains the paths.
I used Jpanel with GridLayout in a JScrollPane, but the result is not the same that I want.
This is the code that I've used to add Jlabels:
for(String file: files) {
JLabel JLabelPicture = new JLabel(new ImageIcon(file));
panel_images.add(JLabelPicture);
}
I used Jpanel with GridLayout in a JScrollPane, but the result is not the same that I want.
A GridLayout will allow you to display the components vertically. When you create your panel you just use:
JPanel imagePanel = new JPanel( new GridLayout(0, 1) );
This will resize all the images to the same size.
Another option is to use a vertical BoxLayout. In this case you can use:
Box imagePanel = Box.createVerticalBox();
In this case the images will retain their preferred size.
In both case you add to the panel to a scroll pane which is added to your frame:
frame.add( new JScrollPane( imagePanel ) );
Read the Swing tutorial on Layout Managers for more information and working examples of each layout.
Edit:
after listing the images I need to select one of them,
Well where was that requirement in your original question. The complete requirement should be defined in the question so all the information is in one place for everybody to see.
So I would suggest you should be using a JList to display an Icon. Read the section from the Swing tutorial on How to Use Lists for more information.
I uploaded an image of my java application below.
Problem:
I'm currently trying to make a login screen with a premade image as the background. I highlighted the actual size the background image which should also be the size of the application window. You can see that the login field is also out of place because its corresponding to the application window.
This is probably because I'm using
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
to set the size of the window.
My question is, how do I make the size of my application border the image?
How to set the size of my application the same as an image
Add your image to a JLabel using an ImageIcon. Then add the label to the frame and pack() the frame.
You can then set the layout manager of the JLabel and add other components to the label.
If your panel is not yet visible:
Image im = ...; // your image
JPanel jp = ...;// panel where you draw your image
jp.setPreferredSize(new Dimension(im.getWidth(null), im.getHeight(null)));
I have this code to add an image
JFrame note=new new JFrame();
JLabel label5=new JLabel();
label5.setIcon(new ImageIcon(searchresult.class.getResource("/images/expired.png")));
label5.setBounds(200,500,450,100);
note.add(label5);
The result I get is this
I tried changing the bounds to other values but there is no change in the image position.The image remains at that same position.
what am I doing wrong?
You haven't set a layout to frame.
note.setLayout(new FlowLayout());
This will help you to drag your image (or whatever the component you want to add) in center.
If you want to position the component via setBounds(), you need to set the layout of the container to null. See http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html
But I would recommend to use a proper layout manager with constraints.
I designed one simple Form.
In which i put one panel as contentPane one JLabel as lblPanel.
now i am trying to set image in JLabel.
I am having WindowBuilder so i can directly set image using setting the icon property of JLabel.
But when i try this it shows the Image only as its original size which is natural we must have to manually set image size to fed the JLabel whole.
SO, here is the Code that is generated by the WindowBuilder when i set the Image using the icon properties of JLabel.
lblPanel.setIcon(new ImageIcon(Admin_Form.class.getResource("/Icons/MouthSmile.jpg")));
Now My Problem is i want to set the size of image as the size of JLabel so there is any direct way to do it using WindowBuilder or just Modifying the above line?
I also tried the following way in which i taken two Imageicon and one Image.
Here is the code for that that i have been tried.
private Image img;
private ImageIcon imgicon;
private ImageIcon newimgicon;
imgicon = new ImageIcon("/Icons/MouthSmile.jpg");
img = imgicon.getImage();
newimgicon = new ImageIcon(img.getScaledInstance(lblPanel.getWidth(),lblPanel.getHeight(), Image.SCALE_SMOOTH));
lblPanel.setIcon(newimgicon);
But when i remove the code that is generated by the Window Builder and use only mine the image is not displayed.
I also seen the way to do it using BufferedImage but i think there will be no difference wheather i will use BufferedImage to resize or ImageIcon and Image.
If you are trying to get the width and height of lblPanel before it is added to your Container and before your JFrame is visible, the width and height will both be 0.
You can resize the image you are choosing to use which will make the JLabel the size you desire. That is the simplest way of going about doing it.
I have a array of images stored in a BufferedImage array. I want to display these images in a JFrame in a table like layout. How can I do so. Note: the number of images in the buffered image array is dynamic
1) use JList there you can put Icon (from BufferedImage), Renderer returns JLabel/JComponent by default
2) all changes (remove, reordering, add) must be done on EDT, then better would be manage all these event from SwingWorker or Runnable#Thread
3) put these Objects to the DefaultListModel for JList
There's both topics to address in your question : draw some BufferedImage (which is done with Graphics) and layout images.
You can do your own layout, but this can be tricky.
To let Swing do this for you, use a already defined layout (for instance GridLayout or FlowLayout) and add as many JPanel as you have BufferedImage. Make sure your JPanel has a fixed dimension (cf. setPreferredSize()).
Custom drawing is done with Graphics/Graphics2D API.
On every JComponent, you can get the underlying Graphics instance to customize the rendering. Draw each BufferedImage in the graphics2D instance of every JPanel.
The last trick is to normalize images dimensions and don't forget respect ratios when calling Graphics.drawImage or crop images if you want to respect aspect ratio (which is what the users expect most of the time).
Create JPanel and set it's layout manager to a GridLayout with the number of rows and columns you want in the grid.
Then for each BufferedImage create a JLabel and set its icon to a new IconImage that contains the BufferedImage.
Finally add all the JLabels to the JPanel in the order you want and add the JPanel to the JFrame.
Here's an example from the top of my head; some pseudocode since I don't have images to work with:
JFrame frame = new JFrame("Title");
JPanel gridPanel = new JPanel();
//Layout as a grid with 4 rows and 3 columns
gridPanel.setLayout(new GridLayout(4,3));
//Pseudocode.
for(each BufferedImage in BufferedImageArray as img) {
gridPanel.add(new JLabel(new ImageIcon(img));
}
frame.add(gridPanel);
//Other frame stuff you want here
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600,600);
frame.setVisible(true);