Image displaying with file directory, but not URL - java

Program works when I link to an image on my program (current code). when I replace that with a URL ("http://www.digitalphotoartistry.com/rose1.jpg"), the program will run without the image being displayed. I've tried a lot of variations with no luck. Can anyone see why it's not working?
public class ImageViewer extends JFrame {
public ImageViewer() {
//create panel of actions
JPanel actionPanel = new JPanel();
actionPanel.setLayout(new GridLayout(1, 4));
actionPanel.add(new JButton("Prev"));
actionPanel.add(new JButton("Add"));
actionPanel.add(new JButton("Del"));
actionPanel.add(new JButton("Next"));
//Create panel to hold pictures
JLabel label= new JLabel(new ImageIcon("C:/Users/Madison/Desktop/capture.png"), JLabel.CENTER);
JPanel imagePanel = new JPanel(new BorderLayout());
imagePanel.add(label, BorderLayout.CENTER );
//Add contents to frame
add(imagePanel, BorderLayout.NORTH);
add(actionPanel, BorderLayout.SOUTH);
}
public static void main (String args []){
ImageViewer frame = new ImageViewer();
frame.setTitle("Title");
frame.setSize(1000, 500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

Are you editing your code accordingly?
You can't just replace the path with the url.
Try this:
URL url = new URL("http://www.digitalphotoartistry.com/rose1.jpg"); //set url
ImageIcon image = new ImageIcon(ImageIO.read(url)); //read image and create ImageIcon
JLabel label = new JLabel(image, JLabel.CENTER);
Remember to check for IO and MalformedURL Exceptions.

Related

Java Swing GUI JLabel Not Showing

I am trying to write a Title for the main menu of my program, by using a JLabel, but it doesn't seem to appear on my screen.
import javax.swing.*;
public class GUI {
public GUI() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(30,30,10,30));
panel.setLayout(new GridLayout());
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Title");
frame.pack();
frame.setSize(854,560);
frame.setVisible(true);
JLabel title = new JLabel();
title.setText("Title");
//title.setSize();
title.setVisible(true);
}
public static void main(String[] args) {
new GUI();
}
}
What am I doing wrong and how could I change the position of the Text if I manage to make it visible?
And I also want to add a button to go to the next page so if you could tell me how to do that too that would be great.
I would quickly and untested say that you are adding the label after you set the frame visible.
Do it before. Else you would have to revalidate and repaint the frame
As I can see in your code you are not adding title in panel. As a quick solution put panel.add(title); after title.setVisible(true); line in your code, it will display the label.
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(30,30,10,30));
panel.setLayout(new GridLayout());
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Title");
frame.pack();
frame.setSize(854,560);
frame.setVisible(true);
JLabel title = new JLabel();
title.setText("Title");
//title.setSize();
title.setVisible(true);
panel.add(title); //<---- this one line will diaplay label in GUI

Adding a vertical scroll bar to a JTextArea

I'm making a simple GUI that has a JTextArea that I can paste a string in. When the string gets too long the JTextArea fills up and I can see the words going below the set bounds I have for the text area. I would like to add a vertical scroll bar to this text area.
public class GUI implements ActionListener {
private static JTextArea fileContents;
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
frame.setSize(700, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
panel.setLayout(null);
fileContents = new JTextArea();
fileContents.setBounds(175, 75, 275, 275);
panel.add(fileContents);
JScrollPane sp = new JScrollPane();
sp.add(fileContents, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
panel.add(sp);
I get an error when I have this setup.
Exception in thread "main" java.lang.IllegalArgumentException: illegal component position
at java.desktop/java.awt.Container.addImpl(Container.java:1111)
at java.desktop/java.awt.Container.add(Container.java:489)
at harness.GUI.main(HarnessGUI.java:60)
This code works:
JFrame frame = new JFrame();
JPanel panel = new JPanel();
frame.setSize(700, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.setVisible(true);
panel.setLayout(null);
fileContents = new JTextArea();
JScrollPane sp = new JScrollPane(fileContents);
sp.setBounds(175, 75, 300, 300);
panel.add(sp);
Your had 2 problems:
If fileContents added to sp that added to panel, tou needn't add fileContents to panel.
When using JScrollPane, you should add Components only with the constractor JScrollPane(Component view) and not with the add() method.

ImageIcon will not display image

I cannot display an image. I have tried both ImageIO and ImageIcon.
This is my code:
public TestGUImain()
{
JFrame frame = new JFrame("Basic Window");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800,600);
frame.setVisible(true);
JPanel panel = new JPanel();
panel.setLayout(null);
frame.add(panel);
JLabel lblText = new JLabel("Hello World", JLabel.CENTER);
lblText.setBounds(10, 10, 100, 200);
panel.add(lblText);
ImageIcon image = new ImageIcon(getClass().getResource("150_leading_side_prep.jpg"));
JLabel lblImage = new JLabel(image, JLabel.CENTER);
lblImage.setBounds(0, 0, 800, 600);
panel.add(lblImage);
}
P.S. if the JLabel is changed to have text in it, it will display. The image is stored in a source folder called resource at the same level as the src folder.
You need to store the image in the src folder, not at the same level as it. You also need to do frame.setVisible(true) at the end of your constructor, or use revalidate(); and repaint(); to refresh your window.

I can't get my JLabels to show. I'm not sure if I placed them in the wrong position in the BorderLayout. Help! My code is below

This is the main class where JLabels and lists are initialized:
public dutchessHousing() {
super("Plan options");
contents = getContentPane();
contents.setLayout(new BorderLayout());
selections = new JLabel (suiteList[0]);
suitesScrollPane = new JScrollPane(suites);
meals = new JList<String>(mealList);
housingOptions = new JLabel();
mealOptions = new JLabel();
total = new JLabel();
contents.add(suitesScrollPane, BorderLayout.EAST);
contents.add(selections, BorderLayout.CENTER);
contents.add(meals, BorderLayout.SOUTH);
contents.add(total, BorderLayout.WEST);
setSize(750, 700);
addComponent();
showCalculations();
setVisible(true);
}
Method to show calculations in frame. housingOptions is supposed to show on the top of the frame, before the JList.
public static void showCalculations() {
housingOptions = new JLabel("Choose one of the following housing options: ", JLabel.CENTER);
mealOptions = new JLabel("Choose one of the following meal plan options: ", JLabel.CENTER);
total = new JLabel();
housingOptions.setLayout(new BorderLayout());
contents.add(housingOptions);
contents.add(mealOptions);
contents.add(total);
}
Main method:
public static void main(String[] args) {
dutchessHousing frame = new dutchessHousing();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(selections);
frame.add(suitesScrollPane);
frame.add(meals);
//frame.add(housingOptions);
frame.add(mealOptions);
frame.add(total);
showCalculations();
frame.setVisible(true);
}
}
contents.add(suitesScrollPane, BorderLayout.EAST);
contents.add(selections, BorderLayout.CENTER);
contents.add(meals, BorderLayout.SOUTH);
contents.add(total, BorderLayout.WEST);
setSize(750, 700);
addComponent();
showCalculations();
Above you add components to different areas of the BorderLayout which is reasonable.
But then when you invoke the showCalculations() method you use:
contents.add(housingOptions);
contents.add(mealOptions);
contents.add(total);
Which will replace all the components added in the "CENTER" with the "total" component since only the last component added to any region of the BorderLayout will be visible.
So you first need to add all the labels to a panel. Then add the panel to the "BorderLayout.CENTER"

Picture and text in same window

This program is supposed to open a window, add a picture, and then add the text "hello world" above the picture. The text appears when i do frame.add(label) and then try to add the picture (like the code shows), but even when I do the opposite and add the picture first I only get a gray schreen. Can anybody show me how I can get both the picture and the text?
public window(){
JFrame frame = new JFrame("name");
JLabel label = new JLabel ("hello world", JLabel.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setSize(600, 400);
frame.setVisible(true);
label.setAlignmentX(0);
label.setAlignmentY(0);
frame.add(label);
frame.add(new JLabel(new ImageIcon("file")));;
}
}
You should use overlay layout, but it is applicable on JPanel.
So add a JPanel to your frame then apply the layout, finally add the components.
Your code may be like that:
public window(){
JFrame frame = new JFrame("name");
JLabel label = new JLabel ("hello world", JLabel.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel() {
public boolean isOptimizedDrawingEnabled() {
return false;
}
};
LayoutManager overlay = new OverlayLayout(panel);
panel.setLayout(overlay);
frame.setResizable(false);
frame.setSize(600, 400);
frame.setVisible(true);
label.setAlignmentX(0);
label.setAlignmentY(0);
panel.add(label);
panel.add(new JLabel(new ImageIcon("file")));
frame.add(panel, BorderLayout.CENTER);
}
}
A label can have both text and icon, and the relative position can be customized.
JLabel label = new JLabel ("hello world", new ImageIcon("file"), JLabel.CENTER);
label.setVerticalTextPosition(SwingConstants.TOP);
frame.add(label);
//frame.add(new JLabel(new ImageIcon("file")));;
The default layout is BorderLayout, and add(label, BorderLayout.CENTER).

Categories