Probably a stupid question. My apologies if so. I couldn't find what I needed with google searches, but probably because I'm not really sure how to word what I need.
I have this line of code:
JScrollPane scrlPane = new JScrollPane(new JLabel(imgIcon));
This does exactly what I need it to do. However, I'm not sure how to change the image in the imgIcon after it's created with this method.
Again, I apologize if this is a stupid question. I've tried to create a separate JLabel outside of this method and add it to the JScrollPane, but for some reason, it draws a gray box over the image. I know it's drawing the image, because I can see 1 pixel of the image loaded around the edge of the gray box.
Thanks for your time!
The easiest thing to do would be to keep a reference to your JLabel object, and to to use its setIcon(Icon icon) method to change it to your new ImageIcon object.
Or you can do somehting like this.
JViewport viewport = scrollPane.getViewport();
JLabel label = (JLabel)viewport.getView();
if (label != null) {
label.setIcon(newImgIcon);
}
Related
I'm still pretty new to Java, and in our programming class we are working in a group. My part of the assignment is to insert an image at the top part of the GUI.
this is the code I have so far..
ImageIcon image = new ImageIcon(getClass().getResource("EXTS.png"));
JPanel.add(image, BorderLayout.NORTH);
but right under the .add part of the Jpanel.add is that red squiggly line telling me that I should change my Image to a component and when I do that it tells me to switch it back to an Image?? this is what I'm getting confused on why would it tell me to change it back if it won't use it the way it is now?
So I guess my question is what should I do to fix this problem?
Also exactly how would I position it, I know it goes into the layout spot in the North but will that be dead center? or does it start from the 0,0 top left and then pixel in?
Thank you in advanced!
(P.s. this is the path to the image file if it should be different please tell me otherwise it's fine -- Project 3/Images/EXTS.png)
An Icon is not a component. You need to add the Icon to a component like a JLabel:
ImageIcon image = new ImageIcon(getClass().getResource("EXTS.png"));
//JPanel.add(image, BorderLayout.NORTH);
JPanel.add(new JLabel(image), BorderLayout.NORTH);
Assuming that the path to the image is correct, you should use a JLabel to show the image. See How to Use Labels for more details.
I'd also consider using ImageIO to read the image instead of ImageIcon as ImageIO will throw an IOException if the image can't be loaded for some reason. See Reading/Loading an Image for more deatils
ImageIcon image = new ImageIcon(
ImageIO.read(getClass().getResource("/EXTS.png")));
JPanel.add(new JLabel(image), BorderLayout.NORTH);
I have an application that whenever I load in any image or update the JTextArea it always places the new object / text in the top left of the frame instead of simply updating whatever object it is supposed to be updating..
I am completely dumbfounded to why it is updating the screen in this way, does anyone have any hints or tips as to how to change this?
Below is the code for adding a jLabel into a jTabbedFrame
JLabel tempJLabel = new javax.swing.JLabel();
//tempJLabel.setLocation(1200,1200);
BufferedImage img = scaleImage(getStoredProductImage(photoDir[i]), 190); //scale down found image to whatever is needed
String filename = photoDir[i].getName();
Image tempImage = new Image(img,photoDir[i].getName(),photoDir[i],figureSaveDir(gtinTextBox.getText(), uidTextBox.getText()),tFrame,tempJLabel); //create ImageObj for later use
if(filename.length()>20){
tFrame.addTab(photoDir[i].getName().substring(15,19), tempJLabel);
tempJLabel.setIcon(new ImageIcon(tempImage.getImg()));
}
Unsure if I should also append information of the GUI construction...
i assume you just use it in any wrong way.
The ScreenShot of the Tab-Layout looks really strange to me.
But maybe i am wrong (i do not know what a JTabbedFrame is?)
When you mean a JTabbedPane, maybe you need to read this:
http://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html
before using it.
I'm a somewhat novice programmer and I'm have some trouble adding an image to my frame. While I know how to add images generally, this specific case it does not work.
public class Tutorial extends JFrame{
Tutorial(){
JFrame frame = new JFrame("ImageTutorial");
frame.setVisible(true);
frame.setSize(750,850);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
ImageIcon image = new ImageIcon(getClass().getResource("Green Block.png"));
JLabel imagelabel = new JLabel(image);
imagelabel.setBounds(10, 10, 75, 75);
imagelabel.setOpaque(true);
frame.add(imagelabel);
Now, I've located the problem but I don't understand 'why' its a problem. When I remove
frame.setSize(750,850);
the image shows, but when its there it doesn't. How can the frame's size impact the image showing and how can I get around it?
Just curious, logically, what makes you think a frame should be visible before you add any components? Logically speaking, wouldn't it seem right to add your components first, then make the frame visible. It's like displaying a painting in an art gallery even before the painter has painted anything on it. It just makes no sense. I highly doubt setting the size has anything to do with it. IF you don't set the size of the frame, then the frame appears as small as possible. When you resize the frame, it causes a repaint, then showing the label you add. But generally, you want to always set frame visible after all you components are added, to avoid this problem.
Side note: You should stay away from null layouts. You need to learn to use Layout Managers and let them do the dynamic sizing and locating for you.
I have an icon for a JLabel which I can see the change for only once. When blank, a new set image for the below code works as it should. But after that, the image is stuck. No new image can replace it. When I use repaint on panelPainting without revalidate(), I get no pictures at all. Thats also weird.
Here is the code, (panelMain houses panelPainting)
//get image from somewhere
JLabel imageLabel = new JLabel();
Icon imageIcon = new ImageIcon(image);
imageLabel.setIcon(imageIcon);
panelPainting.setAlignmentX(JLabel.CENTER);
panelPainting.add(imageLabel); // default center section
//my insanity starts here
panelPainting.revalidate();
panelMain.remove(panelPainting);
panelMain.revalidate();
EDIT: I double checked that the image does change every time.
use JLabel.setIcon() as standard way, then there no reason to remove, modify and add a new JComponents on runtime
in some cases there is issue with repainting Icon in the JLabel (from external sources, www sites, etc.), then you have to call,
myIcon.getImage().flush();
myLabel.setIcon(myIcon);
use CardLayout with a few views, then any action is only to switch betweens cards
otherwise
have to call container.revalidate() and container.repaint(), as last code lines, one time, after all changes are done
for better help sooner post an SSCCE, short, runnable, compilable, just about JFrame with JLabel contains ImageIcon / Icon created on fly
I add some images to a JPanel. Therefore, I add a single image to a JLabel as an ImageIcon and add this to the main JPanel. Although I set the bounds (setBounds) to the image-size, there is a margin of a few pixel on top of the image shown below.
image http://w752749.open.ge.tt/1/files/64dsvTG/0/blob/x675
I also tried to add the images as DisplayJai(), without success (with DisplayJai, the images have also been croped in a strange way).
The important part of the code is
JPanel srcJPanel = new JPanel();
srcJPanel.setBounds(posW, posH, width, height);
srcJPanel.setBorder(new LineBorder(Color.GREEN.darker(), 2));
Image image = newImage.getScaledImg().getAsBufferedImage();
JLabel l = new JLabel(new ImageIcon(image));
l.setBorder(new LineBorder(Color.RED.darker(), 2));
srcJPanel.add(l, BorderLayout.CENTER);
MainPanel.add(srcJPanel);
this.validate();
Can anyone help me with this margin?
Thanks a lot.
setBounds method should be used only if you have "null" layout on your MainPanel, otherwise with each its validation your bounds will be reset to default layout bounds. To set "null" layout - just pass null into MainPanel's setLayout method.
Also using "null" layout is unnecessary in most cases. You can simply use existing layouts or write your own to avoid problems you might have using "null" layout.
Anyway the code you have provided is not enough to see the actual problem - better post an SSCCE.