Using the original instance of a JFrame - java

I have two Java classes; mcveF1 and mcveF2. The code below disables when run opens a JFrame with a singular JButton on it. This button opens a second JFrame and disables the first. Similarly this frame has a singular JButton on it. This button should close the second frame and re-enable the first frame. However an exception is thrown, java.lang.NullPointerException. I believe this is because I am creating a new instance of mcveF1 instead of using the current one. I am unaware of how to fix this and would appreciate any help in fixing it.
mcveF1
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import net.miginfocom.swing.MigLayout;
public class mcveF1
{
public JFrame myMainWindow = new JFrame("Frame 1");
JPanel panel2 = new JPanel();
//Variables and Components
JButton openFrame = new JButton("Open new frame");
public void runGUI()
{
myMainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myMainWindow.setLayout(new GridLayout(1,1));
createSortTestPanel();
myMainWindow.getContentPane().add(panel2);
myMainWindow.setVisible(true);
myMainWindow.pack();
myMainWindow.setMinimumSize(new Dimension(myMainWindow.getBounds().getSize()));
myMainWindow.setLocationRelativeTo(null);
}
public void createSortTestPanel()
{
MigLayout layout = new MigLayout("", "[grow]");
panel2.setLayout(layout);
openFrame.addActionListener(new buttonAction());
panel2.add(openFrame);
}
public static void main(String[] args)
{
mcveF1 f1 = new mcveF1();
f1.runGUI();
}
class buttonAction implements ActionListener
{
#Override
public void actionPerformed(ActionEvent e)
{
myMainWindow.setEnabled(false);
mcveF2 f2 = new mcveF2();
f2.runGUI();
}
}
}
mcveF2
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import net.miginfocom.swing.MigLayout;
public class mcveF2
{
JFrame myMainWindow = new JFrame("Frame 2");
JPanel panel2 = new JPanel();
//Variables and Components
JButton closeFrame = new JButton("Close");
mcveF1 f1;
public void runGUI()
{
myMainWindow.setLayout(new GridLayout(1,1));
createSortTestPanel();
myMainWindow.getContentPane().add(panel2);
myMainWindow.setVisible(true);
myMainWindow.pack();
myMainWindow.setMinimumSize(new Dimension(myMainWindow.getBounds().getSize()));
myMainWindow.setLocationRelativeTo(null);
}
public void createSortTestPanel()
{
MigLayout layout = new MigLayout("", "[grow]");
panel2.setLayout(layout);
closeFrame.addActionListener(new buttonAction());
panel2.add(closeFrame);
}
public static void main(String[] args)
{
mcveF2 f2 = new mcveF2();
f2.runGUI();
}
class buttonAction implements ActionListener
{
#Override
public void actionPerformed(ActionEvent e)
{
myMainWindow.dispose();
f1.myMainWindow.setEnabled(true);
}
}
}
The null pointer exception is shown in this picture as per PM77-1's request.

Create a constructor in your class mcveF2
public class mcveF2() {
public mcveF2(mcveF1 f1) {
this.f1 = f1;
}
Then pass instance of mcveF1 to this constructor in button action listener.
class buttonAction implements ActionListener
{
#Override
public void actionPerformed(ActionEvent e)
{
myMainWindow.setEnabled(false);
mcveF2 f2 = new mcveF2(mcveF1.this);
f2.runGUI();
}
}

Related

Is there a way to reference setVisible() and dispose() from an ActionListener in a separate class file?

I'm working on a very complicated, multi-layered Swing GUI, and the main issue I'm running into right now involves having a JButton ActionListener perform setVisible() on a separate JFrame and immediately dispose() of the current JFrame. Because of the length of my code, it's important that main, both JFrames, and the ActionListener are all split into individual class files. I wrote a VERY simplified version of my problem, split into 4 tiny class files. Here they are:
File 1:
import javax.swing.*;
public class Test {
public static void main(String[] args) {
JFrame g1 = new GUI1();
g1.pack();
g1.setLocation(200,200);
g1.setVisible(true);
JFrame g2 = new GUI2();
g2.pack();
g2.setLocation(400,200);
g2.setVisible(false);
}
}
File 2:
import javax.swing.*;
public class GUI1 extends JFrame {
JPanel panel;
JButton button;
public GUI1() {
super("GUI1");
setDefaultCloseOperation(EXIT_ON_CLOSE);
panel = new JPanel();
button = new JButton("Create GUI2");
button.addActionListener(new Listener());
add(panel);
add(button);
}
}
File 3:
import javax.swing.*;
public class GUI2 extends JFrame {
JPanel panel;
JLabel label;
public GUI2() {
super("GUI2");
setDefaultCloseOperation(EXIT_ON_CLOSE);
panel = new JPanel();
label = new JLabel("I'm alive!");
add(panel);
add(label);
}
}
File 4:
import java.awt.event.*;
public class Listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
GUI2.setVisible(true);
GUI1.dispose();
}
}
As you can see, the only function of the ActionListener is to set GUI2 to visible and dispose of GUI1, but it runs the error "non-static method (setVisible(boolean) and dispose()) cannot be referenced from a static context". I figure this is because both methods are trying to reference objects that were created in main, which is static. My confusion is how to get around this, WITHOUT combining everything into one class.
Any suggestions? Thanks!
EDIT:
Here's the above code compiled into one file... although it returns the exact same error.
import javax.swing.*;
import java.awt.event.*;
public class Test {
public static void main(String[] args) {
JFrame g1 = new GUI1();
g1.pack();
g1.setLocation(200,200);
g1.setVisible(true);
JFrame g2 = new GUI2();
g2.pack();
g2.setLocation(400,200);
g2.setVisible(false);
}
}
class GUI1 extends JFrame {
JPanel panel;
JButton button;
public GUI1() {
super("GUI1");
setDefaultCloseOperation(EXIT_ON_CLOSE);
panel = new JPanel();
button = new JButton("Create GUI2");
button.addActionListener(new Listener());
add(panel);
add(button);
}
}
class GUI2 extends JFrame {
JPanel panel;
JLabel label;
public GUI2() {
super("GUI2");
setDefaultCloseOperation(EXIT_ON_CLOSE);
panel = new JPanel();
label = new JLabel("I'm alive!");
add(panel);
add(label);
}
}
class Listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
GUI2.setVisible(true);
GUI1.dispose();
}
}
You have to pass instances of frame1 and frame2 to your ActionListener.
import java.awt.event.*;
public class Listener implements ActionListener {
private JFrame frame1, frame2;
public Listener(JFrame frame1, JFrame frame2) {
this.frame1 = frame1;
this.frame2 = frame2;
}
public void actionPerformed(ActionEvent e) {
frame2.setVisible(true);
frame1.dispose();
}
}
This means you have to pass an instance of frame2 to your GUI1 class.
import javax.swing.*;
public class GUI1 extends JFrame {
JPanel panel;
JButton button;
public GUI1(JFrame frame2) {
super("GUI1");
setDefaultCloseOperation(EXIT_ON_CLOSE);
panel = new JPanel();
button = new JButton("Create GUI2");
button.addActionListener(new Listener(this, frame2));
add(panel);
add(button);
}
}
This means you have to create the frames in the reverse order.
import javax.swing.*;
public class Test {
public static void main(String[] args) {
JFrame g2 = new GUI2();
g2.pack();
g2.setLocation(400,200);
g2.setVisible(false);
JFrame g1 = new GUI1(g2);
g1.pack();
g1.setLocation(200,200);
g1.setVisible(true);
}
}

Can I communicate between two JFrames WITHOUT using static variables or methods?

Basically, I'm trying to get the JButton in Frame1 to edit the JLabel in Frame2. I know it can work if I set the JLabel and getLabel() method in Frame2 to static, and have the ActionListener reference Frame1 directly, but I want to know if there's a way to do it without using static variables or methods.
Here's the code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test {
public static void main(String[] args) {
Frame2 f2 = new Frame2();
f2.pack();
f2.setLocation(700, 400);
f2.setVisible(true);
Frame1 f1 = new Frame1(f2);
f1.pack();
f1.setLocation(400, 400);
f1.setVisible(true);
}
}
class Frame1 extends JFrame {
JButton button;
public Frame1(JFrame f) {
super("Frame 1");
setDefaultCloseOperation(EXIT_ON_CLOSE);
button = new JButton("Button");
add(button);
button.addActionListener(new Listener(f.getLabel()));
}
}
class Frame2 extends JFrame {
JLabel label;
public Frame2() {
super("Frame 2");
setDefaultCloseOperation(EXIT_ON_CLOSE);
label = new JLabel("hello");
add(label);
}
public JLabel getLabel() {
return label;
}
}
class Listener implements ActionListener {
private JLabel lab;
public Listener(JLabel lab) {
this.lab = lab;
}
public void actionPerformed(ActionEvent e) {
lab.setText("nice");
}
}
Any suggestions? Thanks!
EDIT: Here's a compilable version of the code -- label and getLabel() are static, and the ActionListener references JFrame1 directly when it's called. My goal is to have no static variables or methods (outside of main).
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test {
public static void main(String[] args) {
Frame2 f2 = new Frame2();
f2.pack();
f2.setLocation(700, 400);
f2.setVisible(true);
Frame1 f1 = new Frame1(f2);
f1.pack();
f1.setLocation(400, 400);
f1.setVisible(true);
}
}
class Frame1 extends JFrame {
JButton button;
public Frame1(JFrame f) {
super("Frame 1");
setDefaultCloseOperation(EXIT_ON_CLOSE);
button = new JButton("Button");
add(button);
button.addActionListener(new Listener(Frame2.getLabel()));
}
}
class Frame2 extends JFrame {
static JLabel label;
public Frame2() {
super("Frame 2");
setDefaultCloseOperation(EXIT_ON_CLOSE);
label = new JLabel("hello");
add(label);
}
public static JLabel getLabel() {
return label;
}
}
class Listener implements ActionListener {
private JLabel lab;
public Listener(JLabel lab) {
this.lab = lab;
}
public void actionPerformed(ActionEvent e) {
lab.setText("nice");
}
}
Oracle has a really nifty Swing tutorial. I think your studying the tutorial would be a really good idea.
I had to make a bunch of changes to your code to get it to execute.
The main change I made was to keep the reference to the JLabel in the Frame2 class. I passed an instance of Frame2 to the Listener class. Just like I did yesterday with your previous question.
Here's the code. Take the time to study what I did before you ask another question tomorrow.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class DoubleJFrameTest {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
Frame2 f2 = new Frame2();
f2.pack();
f2.setLocation(700, 400);
f2.setVisible(true);
Frame1 f1 = new Frame1(f2);
f1.pack();
f1.setLocation(400, 400);
f1.setVisible(true);
}
});
}
}
class Frame1 extends JFrame {
private static final long serialVersionUID = 1L;
private JButton button;
public Frame1(Frame2 f) {
super("Frame 1");
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel(new BorderLayout());
button = new JButton("Button");
button.addActionListener(new Listener(f));
panel.add(button, BorderLayout.CENTER);
add(panel);
}
}
class Frame2 extends JFrame {
private static final long serialVersionUID = 1L;
private JLabel label;
public Frame2() {
super("Frame 2");
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel(new BorderLayout());
label = new JLabel("hello");
panel.add(label, BorderLayout.CENTER);
add(panel);
}
public void setLabelText(String text) {
label.setText(text);;
}
}
class Listener implements ActionListener {
private Frame2 frame;
public Listener(Frame2 frame) {
this.frame = frame;
}
#Override
public void actionPerformed(ActionEvent e) {
frame.setLabelText("nice");
}
}

How to show and hide an existing JFrame

I am doing some basic tests to understand how Java Swing works.
I have a test application which consist of three fully independent windows (JFrames):
Main Menu
Asset Window 1
Asset Window 2
The Main Menu has a JButton which will show/hide Asset Window 1 (a1).
This is the main class to launch all windows:
package test1;
import test1.AssetList.AssetList;
import test1.MainMenu.MainMenu;
import javax.swing.*;
public class Test1 {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
MainMenu m = new MainMenu();
AssetList a1 = new AssetList();
AssetList a2 = new AssetList();
}
});
}
}
This is the class with the Asset Window JFrame:
package test1.AssetList;
import javax.swing.*;
public class AssetList extends JFrame {
public AssetList() {
JLabel label = new JLabel("Asset list");
this.getContentPane().add(label);
this.pack();
this.setVisible(false);
}
}
This is the class for the MainMenu JFrame:
package test1.MainMenu;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
public class MainMenu extends JFrame {
JLabel label = new JLabel("Main Menu");
JButton button = new JButton("Asset");
public MainMenu() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().add(label);
this.getContentPane().add(button);
button.addActionListener(new ButtonAssetListener());
this.pack();
this.setVisible(true);
}
}
This is the class for the Asset Window Button JButton listener:
package test1.MainMenu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ButtonAssetListener implements ActionListener{
#Override
public void actionPerformed(ActionEvent evt) {
System.out.println("CLICK!");
/* PSEUDOCODE
if(a1 from Test1.isVisible()==true) {
a1 from Test1.setVisible(false);
} else {
a1 from Test1.setVisible(true);
}
*/
}
}
How can I retrieve the a1 instance from ButtonAssetListener in order to toggle its visibility?
Is there a better alternative to structure this kind of multiple windows application in Java Swing?
You can just pass the instance you want to hide to the button listener.
public class Test1 {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
AssetList a1 = new AssetList();
AssetList a2 = new AssetList();
MainMenu m = new MainMenu(a1);
}
});
}
}
Make your main menu take in a component which it will show and hide.
public class MainMenu extends JFrame {
JLabel label = new JLabel("Main Menu");
JButton button = new JButton("Asset");
public MainMenu(JComponent assetList) {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().add(label);
this.getContentPane().add(button);
button.addActionListener(new ButtonAssetListener(assetList));
this.pack();
this.setVisible(true);
}
}
Then modify your your button asset listener to take in a component which it will then show or hide.
public class ButtonAssetListener implements ActionListener{
private JComponent component;
public ButtonAssetListener(JComponent component) {
this.component = component;
}
#Override
public void actionPerformed(ActionEvent evt) {
if(component.isVisible()) {
component.setVisible(false);
} else {
component.setVisible(true);
}
}
}

How do I make java game options?

I m trying to make a main menu with START button, CONTROLS button and HELP button.
I made a backboard for it but I need help with making options.
I tried to make it, but the error says that local variable gameFrame is accessed from within inner class; needs to be declared final
import java.util.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import static java.lang.System.*;
public class GameFrame {
public static void main(String[] args) {
JFrame gameFrame = new JFrame("PoopMan");
gameFrame.setSize(900, 800);
gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameFrame.setResizable(false);
gameFrame.setVisible(true);
gameFrame.getContentPane().setBackground(Color.yellow);
JPanel panel = new JPanel();
JButton button1 = new JButton();
gameFrame.add(panel);
panel.add(button1);
gameFrame.setVisible(true);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(gameFrame.getComponent(0), "START");
}
});
}
}
I believe you would like to initialize your Game in a non-static context. Do the following.
public class Main {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new GameFrame();
}
});
}
}
And
import java.util.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import static java.lang.System.*;
public class GameFrame extends JFrame {
private void init() {
this.setSize(900, 800);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setResizable(false);
this.setVisible(true);
this.getContentPane().setBackground(Color.yellow);
JPanel panel = new JPanel();
JButton button1 = new JButton("START");
this.add(panel);
panel.add(button1);
this.setLayout(new FlowLayout());
this.setMinimumSize(new Dimension(300, 300));
this.pack();
this.setVisible(true);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(getComponent(0), "START");
}
});
}
public GameFrame() {
super("PoopMan");
init();
}
}
This way, you start the GameFrame on another thread, and you can continue your work within the GameFrame class.
That is quite simple. Please note that this is a place to inquire about issues regarding your code, and not a place to look for answers.
What you should be researching is known as JMenuBar (refer to This Tutorial for Assitance)
Seeing as you are new, I will assist you this once. Below is the code that will accomplish what you ask. (Tried and Tested)
import java.util.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import static java.lang.System.*;
public class GameFrame
{
static private JMenuBar menuBar;
static private JMenu startMenu, controlsMenu, helpMenu;
static private JMenuItem startBtn;
static private JMenuItem controls;
static private JMenuItem hlpBtn;
public static void main(String[] args)
{
JFrame gameFrame = new JFrame("PoopMan");
gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameFrame.getContentPane().setBackground(Color.yellow);
menuBar = new JMenuBar();
startMenu = new JMenu("File");
controlsMenu = new JMenu("Controls");
helpMenu = new JMenu("Help");
startBtn = new JMenuItem("Start");
controls = new JMenuItem("Controls");
hlpBtn = new JMenuItem("Help");
gameFrame.add(menuBar, BorderLayout.PAGE_START);
menuBar.add(startMenu);
menuBar.add(controlsMenu);
menuBar.add(helpMenu);
startMenu.add(startBtn);
controlsMenu.add(controls);
helpMenu.add(hlpBtn);
gameFrame.pack();
gameFrame.setResizable(false);
gameFrame.setVisible(true);
gameFrame.setSize(900,800);
}
}
I am not certain as to why you are receiving a Final error. It works fine.
From later edits to the question:
JButton button1 = new JButton("Button");
JPanel panel = new JPanel();
panel.setBackground(Color.yellow);
gameFrame.add(panel);
panel.add(button1);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(gameFrame.getComponent(0), "START");
}
});

How could I make the colour BLACK be transparent on top of the uploaded image?

Right now all I have is created the black background and uploaded my image I wanted. I want to make it so the image is behind a transparent background. I initially had this all in separate class files but put them all in one main class
package Flashlight;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Flashlight3 extends JFrame {
public class FlashLabelChange extends JPanel {
Flashlight.FlashDisp flashDisp;
public FlashLabelChange(Flashlight.FlashDisp _flashDisp) {
flashDisp = _flashDisp;
JButton btn1 = new JButton("Start");
add(btn1);
class Button implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (event.getActionCommand().equals("Start")) {
flashDisp.UpdateLabel("Start");
}
}
}
ActionListener button = new Button();
btn1.addActionListener(button);
}
}
public class FlashDisp extends JPanel {
private JLabel lblName;
private String sLabel;
private JLabel lblImage;
public FlashDisp() {
lblImage = new JLabel();
add(lblImage);
lblName = new JLabel("Start?");
add(lblName); //add it to the Frame
}
void UpdateLabel(String _sNew) {
sLabel = _sNew;
lblName.setText(sLabel);
}
void UpdateBackground(String _sNew) {
sLabel = _sNew;
if (sLabel == ("Black")) {
setBackground(Color.black);
lblImage.setIcon(new ImageIcon("Hallway.png"));
}
}
}
public class FlashColour extends JPanel {
FlashDisp flashDisp;
public FlashColour(FlashDisp _flashDisp) {
flashDisp = _flashDisp;
setLayout(new GridLayout(3, 1));
JButton btnDark = new JButton("Dark");
add(btnDark);
class ColourChangeListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (event.getActionCommand().equals("Dark")) {
flashDisp.UpdateBackground("Black");
}
}
}
ActionListener colourChangeListener = new ColourChangeListener();
btnDark.addActionListener(colourChangeListener);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
FlashMain flashMain = new FlashMain();
frame.setSize(400, 400);
frame.setTitle("FLAAAASH LIGHT");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(flashMain);
frame.setVisible(true);
}
}
I think that you need to use the opaque method.
Have a look here
setOpaque(true/false); Java for using opaque.
Hope that helps.

Categories