My JPanel doesn't doesn't show up consistently - java

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);
}
}

Related

Java gui - JLabel Location

I am new here and i want to ask, i just wanted to set new JLabel and i want it to put it to specific place. But it doesnt work. Im sorry if this is dumm question but i am begginer
:
package ChangingImageAtTheTop;
import java.awt.Color;
import java.awt.Font;
import javax.swing.*;
public class Gui {
public static void main(String[] args) {
guii f = new guii();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(500, 500);
f.setVisible(true);
}
}
package ChangingImageAtTheTop;
import javax.swing.*;
import java.awt.*;
public class guii extends JFrame {
public guii() {
super("c");
setLayout(new FlowLayout());
JLabel label = new JLabel();
label.setText("User");
label.setBounds(10,25,30,85);
add(label);
}
}

How to show different pages from the center element of JFrame (having set to BorderLayout)

import java.awt.BorderLayout;
import java.awt.Component;
import javax.swing.JFrame;
import javax.swing.border.Border;
public class GuiController extends JFrame {
private CentreFrameController centreFrameController;
private CustomerPage customerPage;
private LoginPage loginPage;
public GuiController(){
centreFrameController=new CentreFrameController(this);
setLayout(new BorderLayout());
add(centreFrameController,BorderLayout.CENTER);
setTitle("Courier System-Login Page");
setVisible(true);
setSize(550,650);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
CentreFrameController
import java.awt.BorderLayout;
import java.awt.Graphics;
import javax.swing.JPanel;
public class CentreFrameController extends JPanel {
private GuiController guiController;
private CustomerPage customerPage;
private LoginPage loginPage;
public CentreFrameController(GuiController guiController){
this.guiController=guiController;
loginPage=new LoginPage(this);
setLayout(new BorderLayout());
add(loginPage,BorderLayout.CENTER);
loginPage.addLoginPageListener(new LoginPageListener(){
public void getLoginPageReply(int reply) {
switch(reply){
case 0:System.out.println("Customer login sucess");
setCustomerHomePage();
break;
case 1:System.out.println("Admin login success");
}
}
});
}
public void setCustomerHomePage(){
customerPage=new CustomerPage(this,loginPage);
add(customerPage,BorderLayout.CENTER);///This is the part which is not working
}
}
The aim of this part of the code is:
I want to make the GuiController frame have some common style for my application on all pages (irrespective of any activity)
The GuiController frame gives the center element styling of the BorderLayout to the CentreFrameController panel
Now the CentreFrameController should switch pages after hearing from the LoginListener. But here,when it hears from LoginListener, it's not adding the CustomerPage to its layout (please note that here, the CustomerPage class is made to extend JPanel and I have not uploaded that because its not really necessary).
Here is a mcve demonstrating how you could use CardLayout as suggested by Andrew Thompson:
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GuiController extends JFrame {
private boolean isRedShowing;
public GuiController(){
setTitle("CardLayout Demo");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
CentreFrameController centreFrameController = new CentreFrameController();
centreFrameController.showRedPane();
isRedShowing = true;
setLayout(new BorderLayout());
add(centreFrameController,BorderLayout.CENTER);
JButton toggle = new JButton("Toggle");
toggle.addActionListener(e ->{
if(! isRedShowing) {
centreFrameController.showRedPane();
} else {
centreFrameController.showYelloPane();
}
isRedShowing = ! isRedShowing;
});
add(toggle,BorderLayout.PAGE_END);
pack();
setVisible(true);
}
public static void main(String[] args) {
new GuiController();
}
}
class CentreFrameController extends JPanel {
public final String YELLOW_PAGE = "yellow page";
public final String RED_PAGE = "red page";
private final CardLayout cLayout;
public CentreFrameController(){
cLayout = new CardLayout();
setLayout(cLayout);
setPreferredSize(new Dimension(200, 150));
add(YELLOW_PAGE, new YellowPane());
add(RED_PAGE, new RedPane());
}
//two convenience methods that encapsulate CardLayout#show(Container, String)
void showRedPane() {
cLayout.show(this, RED_PAGE);
}
void showYelloPane() {
cLayout.show(this, YELLOW_PAGE);
}
}
class RedPane extends JPanel{
RedPane(){
setBackground(Color.RED);
}
}
class YellowPane extends JPanel{
YellowPane(){
setBackground(Color.YELLOW);
}
}
You control which card shows by using CarrdLayout show.

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);

JLabel picture display issue

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

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