JLabel picture display issue - java

I am currently trying to simply display an image within a JFrame. I have tried using the below code but it doesn't seem to work. I can't work out where I am going wrong - any ideas?
This is the error message displayed in the console:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at Day1.PictureTester.<init>(PictureTester.java:22)
at Day1.PictureTester.main(PictureTester.java:14)
Here is the code:
import java.awt.GridLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class PictureTester extends JFrame{
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
new PictureTester().setVisible(true);
}
public PictureTester(){
super();
setSize(600, 600);
setLayout(new GridLayout());
java.net.URL imgUrl = PictureTester.class.getResource("C:\\Users\\Harry\\Desktop\\LearnJava\\pug-image3.jpg");
ImageIcon image = new ImageIcon(imgUrl);
JLabel display = new JLabel(image);
add (display);
}
}

Of course that won't work. Read the javadoc of Class.getResource(). It uses the ClassLoader to load a resource from the classpath, using a slash separated path.
Put your image in the same package as the PictureTester class, and just use
PictureTester.class.getResource("pug-image3.jpg");
Or put it in some random package of your source folder (like com.foo.bar), and load it using
PictureTester.class.getResource("/com/foo/bar/pug-image3.jpg");

This code solved it, it helped my thinking to create it within a new project:
package NewAppIdea;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class PictureSplash extends JFrame {
private static final long serialVersionUID = 1L;
JLabel l1;
PictureSplash(){
setTitle("Pic");
setSize(400,400);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setLayout(new BorderLayout());
setContentPane(new JLabel(new ImageIcon("C:\\Users\\Harry\\Desktop\\LearnJava\\pug-image3.jpg")));
setLayout(new FlowLayout());
setSize(399,399);
setSize(400,400);
}
public static void main(String args[]) {
new PictureSplash();
}
}

You can do it like the following example, by using just an image and a label. also you can remove all the extra code if you intend to use this image only once.
import java.awt.GridLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class PictureTester extends JFrame{
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
new PictureTester().setVisible(true);
}
public PictureTester(){
super();
setSize(600, 600);
setLayout(new GridLayout());
add(new JLabel(new ImageIcon("Path/To/Your/Image.png")));
}
}

Icon i=new ImageIcon("src//pics//scope.jpg");
JLabel l1=new JLabel(i);
Use simply like this use JLabel or ImageIcon

Related

How to call this method into my other file?

I want to be able to call the Introduction.Intro() method into my main file code, but it tells me I am unable to call a non-static method intro from a static context. Since I am still fairly new to coding I'm not entirely sure what the problem is. I've added my codes down below. I've tried countless online methods but sadly none have seemed to work.
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Start extends JFrame implements ActionListener
{
private JFrame Main;
private JPanel PanelA, PanelB, PanelC;
private JLabel Text, ImageL;
private JButton Button;
private ImageIcon Image;
public Start ()
{
//Button
Button = new JButton("Start");
Button.addActionListener(new ButtonListener());
//Text
Text = new JLabel("Welcome To The Game"); //ADD NAME OF THE GAME
//Image
Image = new ImageIcon(getClass().getResource("download.jfif")); //ADD THE IMAGE FOR WELCOME
ImageL = new JLabel(Image);
//Top Panel (PanelA) - Image
PanelA = new JPanel();
PanelA.setBorder(BorderFactory.createEmptyBorder(0,200,150,200));
PanelA.setLayout(new FlowLayout(FlowLayout.CENTER));
PanelA.add(ImageL);
//Middle Panel (PanelB) - Text
PanelB = new JPanel();
PanelB.setBorder(BorderFactory.createEmptyBorder(50,200,10,200));
PanelB.setLayout(new FlowLayout(FlowLayout.CENTER));
PanelB.add(Text);
//Bottom Panel (PanelC) - Buttons
PanelC = new JPanel();
PanelC.setBorder(BorderFactory.createEmptyBorder(0,200,20,200));
PanelC.setLayout(new FlowLayout(FlowLayout.CENTER));
PanelC.add(Button);
//Main Frame
Main = new JFrame ();
Main.add(PanelA, BorderLayout.NORTH);
Main.add(PanelB, BorderLayout.CENTER);
Main.add(PanelC, BorderLayout.SOUTH);
Main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Main.setTitle("GAME TITLE"); //ADD THIS LATER
Main.pack();
Main.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent ae)
{
}
public class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == Button)
{
Introduction.Intro1(); //THESE LINE RIGHT HERE
return null; //THESE LINE RIGHT HERE
}
}
}
public static void main(String[] args)
{
new Start();
}
}
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Introduction
{
private JFrame Main;
private JPanel PanelD;
private JLabel Text, ImageL;
private JButton Button;
private ImageIcon Image;
public void Intro()
{
Image = new ImageIcon(getClass().getResource("guy.jfif"));
ImageL = new JLabel(Image);
PanelD = new JPanel();
PanelD.setBorder(BorderFactory.createEmptyBorder(0,100,10,100));
PanelD.setLayout(new FlowLayout(FlowLayout.CENTER));
PanelD.add(ImageL);
PanelD.setVisible(true);
Main.add(PanelD, BorderLayout.NORTH);
}
}
EDIT: So I made another method in the Introduction class where I added this line of code, it managed to fix the error, however, the panel isn't being saved and my JFrame is outputting blank.
public static JFrame Intro1()
{
Introduction M = new Introduction();
return M;
}
If you are looking to initialize the Introduction class in main method of Start class, You can add belo code in main method after Start()
Introduction M = new Introduction();
You main method becomes :
public static void main(String[] args)
{
new Start();
Introduction M = new Introduction();
m.Intro
}
Looking at this set of code, It looks like there is incompatible issue, as you have declare JFrame as return type, while you are returning instance of Introduction.
public static JFrame Intro1()
{
Introduction M = new Introduction();
return M;
}

I do not understand why this code won't work(new to JRadioButtons)

I am learning JRadioButtons and I do not know why it is working in the tutorial I am watching and not in my code. Could someone please take a look at it?
Main Class:
import java.awt.*;
import javax.swing.*;
public class Calculator extends JPanel{
private static final long serialVersionUID = 1L;
public static void main(String[] args){
Screen screen = new Screen();
screen.setVisible(true);
}
}
Here is the Screen Class:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
public class Screen extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
JRadioButton b1, b2;
ButtonGroup group;
JTextArea tb;
public Screen(){
super("First GUI");
setSize(600,600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JPanel p1 = new JPanel();
JTextArea tb = new JTextArea("Text Area");
group = new ButtonGroup();
group.add(b1);
group.add(b2);
b1 = new JRadioButton("Hello");
b1.setActionCommand("HELLO!");
b1.addActionListener(this);
b2 = new JRadioButton("Goodbye");
b2.setActionCommand("Goodbye! =)");
b2.addActionListener(this);
p1.add(b1);
p1.add(b2);
p1.add(tb);
add(p1);
}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
tb.setText(e.getActionCommand());
}
}
In my new Java head this should work perfectly. I initialize the buttons, initialize the group. I am getting an error after I click one of the buttons: AWT-EventQueue-0. I do not know what that means so I do not know how to fix this issue.
You have declared twice the same variable. If you declare the same variable (JTextArea tb) in the global and local scope, it will be a separate object. Remove the declaration in the local scope from the Screen() constructor so it will work.
Try this
tb = new JTextArea("Text Area");
instead of
JTextArea tb = new JTextArea("Text Area");
Because of your current code, tb is still not initialized in the global scope.

Java GUI shows up blank [duplicate]

This question already has answers here:
Elements not showing in GridBagLayout
(2 answers)
Closed 7 years ago.
So I'm trying to create my first GUI in Java but it's coming up blank. As far as I can see I've done everything correct, but of course since it's just blank I'm obviously doing something wrong.
I've got a mainclass and a class for the JFrame and all its contents.
I'm not getting any errors except for the public class MainFrame giving me "The serializable class MainFrame does not declare a static final serialVersionUID field of type long".
package main;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import gui.MainFrame;
public class Main {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run()
MainFrame frame = new MainFrame();
frame.setTitle("GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 400);
frame.setVisible(true);
}
});
}
}
.
package gui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainFrame extends JFrame {
private JButton button;
private Container cont;
public MainFrame(){
Container cont = new Container();
setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JButton button = new JButton("Change Color");
panel.add(button, BorderLayout.CENTER);
cont.add(panel, BorderLayout.CENTER);
cont.setBackground(Color.GREEN);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cont.setBackground(Color.CYAN);
}
});
}
}
You didn't add Container cont to the frame.
Forgot about adding component to frame:
panel.add(cont);

My JPanel doesn't doesn't show up consistently

I'm a relatively young programmer and I'm having a problem. When ever I try to run my program on the custom JPanel I made doesn't show up most of the time and when it does show up it and I try to run it again with the exact same code it doesn't work.
Can someone please help me?
package ca.Software.PocketUniverse;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class MercuryMain extends JFrame {
ImageIcon windowicon;
public MercuryMain(){
windowsetup();
}
public void windowsetup(){
JFrame j=new JFrame();
windowicon= new ImageIcon(getClass().getResource("mercury.jpg"));
this.setTitle("MercuryMessaging");
this.setVisible(true);
this.setResizable(false);
this.pack();
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setIconImage(windowicon.getImage());
ContentPanel p=new ContentPanel();
JButton b=new JButton("code");
p.add(b);
add(p);
}
public void errormethod(){
}
public static void main(String[] args) {
new MercuryMain();
}
}
ContentPanel
package ca.Software.PocketUniverse;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.CardLayout;
public class ContentPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
JButton contacts;
public ContentPanel(){
setLayout(new BorderLayout());
contacts=new JButton("contacts");
this.add(contacts,BorderLayout.NORTH);
}
}

AWT button not clickable in VLCJ

I have created a simple VLCJ project that consists of a simple embedded player and a button to exit.
The code is as follows:
package test;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;
import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
public class Demo {
private final JFrame frame;
private final EmbeddedMediaPlayerComponent mediaPlayerComponent;
private JPanel videoPane;
private JPanel buttonPane;
private Button exitButton;
private ActionListener a;
private static String vlc_location = "C:\\Program Files\\VideoLAN\\VLC";
public static void main(String[] args) {
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), vlc_location);
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Demo().run();
}
});
}
public Demo() {
mediaPlayerComponent = new EmbeddedMediaPlayerComponent();
a = new MyActionListener();
exitButton = new Button("Exit");
exitButton.setActionCommand("Exit app");
exitButton.addActionListener(a);
buttonPane = new JPanel();
buttonPane.setLayout(new BorderLayout());
buttonPane.setBackground(Color.black);
buttonPane.add(exitButton, BorderLayout.CENTER);
videoPane = new JPanel();
videoPane.setLayout(new BorderLayout());
videoPane.setBackground(Color.black);
videoPane.add(mediaPlayerComponent, BorderLayout.CENTER);
videoPane.add(buttonPane, BorderLayout.PAGE_END);
frame = new JFrame("vlcj demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(100, 100);
frame.setSize(1200, 800);
frame.setContentPane(videoPane);
frame.setVisible(true);
}
public void run() {
mediaPlayerComponent.getMediaPlayer().playMedia(video_file);
}
class MyActionListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent arg0) {
String s = arg0.getActionCommand();
if (s.equals("Exit")) {
System.exit(0);
}
}
}
}
The problem is that the button does show up but it cannot be clicked. When i removed the videoPane, it was back to clickable! Any ideas if I'm missing something?
I am using the version 2.1.0 for vlcj.
Thanks!
Thanks MadProgrammer for your advise. I went on to think about it and tried commenting away the line of code in run(). The JButton came back!
However, when i un-commented the code in run(), the JButton disappeared. I was thinking maybe the Swing runnable was causing issue with the creation of the JButton.
Hence, what i did was to comment away the whole Swing runnable and just use:
final Demo demo = new Demo();
demo.run();
The demo can now play video and display the Exit button, thanks!

Categories

Resources