I'm writing a simple game that is based on two JFrames. The user is only displayed on JFrame at a time. To get to the other JFrame, the user clicks a button. The button in the new JFrame will have an overriden method that should navigate the user back to the old JFrame.
I've found success doing this however the .dispose() method doesn't seem to close the new frame when the user tries to navigate back to the old frame.
Here's a snippet of my code (original JFrame class):
public class TicTacToe extends JFrame implements ActionListener{
....
public class gameModeListener implements ActionListener
{
#Override public void actionPerformed(ActionEvent e)
{
TicTacToeSingle singlePlayer = new TicTacToeSingle();
singlePlayer.setVisible(true);
dispose();
}
}
}
And from the other JFrame class:
public class TicTacToeSingle extends TicTacToe{
private int i;
private int j;
JButton boardArray[][];
Random random_generator = new Random();
int randomI;
int randomJ;
public TicTacToeSingle()
{
super("Single Player");
gameMode.setText("Two Player"); //gameMode is the button that has it's actionlistener method overriden. It navigates the user to and back from JFrame to JFrame
gameMode.addActionListener(new gameModeListener());
....
}
....
public class gameModeListener implements ActionListener
{
#Override public void actionPerformed(ActionEvent e)
{
TicTacToe twoPlayer = new TicTacToe("Two Player");
twoPlayer.setVisible(true);
dispose();
}
}
Your help is greatly appreciated :)
Well when u have used Object of the class and u are calling just Dispose(); then how compiler came to know that for this particular class object should be dispose on that call.?
so for giving reference u need to use this try this:
TicTacToe twoPlayer = new TicTacToe("Two Player");
twoPlayer.setVisible(true);
twoPlayer.dispose();
if still there is prob let me know.
Related
I am trying to create a program where when the user clicks a JMenuItem (basic) in the frame in class2, it opens the frame in class1. However, when I try to run the code, I keep getting errors about how it can't find the "setVisible" method. I was wondering whether something could give me some pointers. Also, both classes are stored in different java files (but in the same folder).
CLASS 1:
public class CalculatorFrame {
private JFrame frame;
public String calc = "";
public CalculatorFrame() {
frame = new JFrame("Basic Calculator");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
CLASS 2:
public class CalculatorFrameA extends CalculatorFrame{
private JFrame frameA;
public CalculatorFrameA() {
frameA = new JFrame("Advanced Calculator");
frameA.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ActionListener basicListener = new ActionListener(){ //basic is a JMenuItem
#Override
public void actionPerformed(ActionEvent e){
CalculatorFrame frame = new CalculatorFrame();
//frame.show();
frameA.dispose();
frame.setVisible(true);
}
};
EDIT : I found my problem but still don't have a clue for why this happen, I'm still not finished Online Lectures from Professor Mehran
Sahami (Stanford), maybe I'll find an answer if I push on on the
lecture videos.
The problem is I remove my other components methods before my button
method for efficient posting space, so I should put my JToggleButton
method after my main JFrame method for it to work, but what if my
other components inherit other class too? Which method should I put first to make all of components works? That I'll found out with
practicing java more.
Thank you #Dan and #SebVb for answers and suggestions, sorry if this
just a beginners mistake :)
I am learning java for a month now and already had simple project for learning but now I have problems with JToggleButton, ItemEvent, and actionPerformed included in If-statement.
I've searching for a week for examples on using actionPerformed within if-statement that have ItemEvent from another class but i can't find a same problem to produce a working result.
I'm trying to make a window scanner that will scan only if toggle button is selected then paint JPanel using buffered image (repaint every 100 millisecond) and disposed it if toggle button is deselected, but I think my approach to do it is wrong. I have one main class and two sub-classes like these:
Main class:
public class WindowScanner {
public static void main(String[] args) {
new Window().setVisible(true);
}
}
Window class:
class Window extends JFrame {
static JToggleButton captureButton = new JToggleButton("CAPTURE");
#SuppressWarnings("Convert2Lambda")
public Window() {
// JFrame looks codes
/** EDIT: these components method should be written after button method
* JPanel looks codes
* JLabel looks codes
* END EDIT
*/
add(captureButton);
// capture button default looks code
ItemListener captureListener = new ItemListener(){
#Override
public void itemStateChanged(ItemEvent captureButtonEvent) {
int captureState = captureButtonEvent.getStateChange();
if(captureState == ItemEvent.SELECTED){
// capture button SELECTED looks code
System.out.println("capture button is selected");
} else if(captureState == ItemEvent.DESELECTED){
// capture button DESELECTED looks code
System.out.println("capture button is deselected");
}
}
}; captureButton.addItemListener(captureListener);
}
}
Scanner class:
public class Scanner extends Window {
private static BufferedImage boardCaptured;
static int delay = 100;
protected BufferedImage boardScanned(){
return boardCaptured;
}
#SuppressWarnings("Convert2Lambda")
public static void Scan() {
if (captureButton.isSelected()) {
ActionListener taskPerformer = new ActionListener() {
#Override
public void actionPerformed(ActionEvent captureEvent) {
try {
// capturing method
} catch (AWTException error) {
// AWTException error method
}
// is this the right place to put JPanel code?
JPanel panel = new JPanel();
boardCaptured = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphic = boardCaptured.createGraphics();
panel.setSize(500,500);
panel.paint(graphic);
panel.revalidate();
panel.repaint();
}
}; new Timer(delay, taskPerformer).start();
} else {
// this suppose to end capturing if capture button isSelected() == false
}
}
}
So here is my questions:
Do I really have to make Main class separated from Window class?
What the reason?
How to make my if statement in Scan method recognize state of my
JToggleButton from Window class? Is it impossible and I had a wrong
approach to do it?
In Scanner class, i can't make a get/set for my actionPerformed
(Netbeans always checked it as an error), but why I can make one for
BufferdImage?
If I can't get question number 3 happen, how can I make If statement
to stop capturing using Timer.stop()? Or am I in wrong approach again?
Do my JPanel in Scanner class would be produced and make a viewer
for my buffered image?
P.S. I'm sorry it cramped with questions, I tried not to make multiple post, so I make single post with multiple questions. Please notice me if there's answer before, I'm honestly can't find it or had search it with wrong tags.
Here is a simple version of what I think you want to do. This can be edited to include your variables, such as boardCaptured. This code mainly portrays how to get a component from a different class.
Main.java (Contains all the classes in one java file)
import javax.swing.JLabel;
import javax.swing.JToggleButton;
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.swing.Timer;
class WindowScanner extends JFrame {
private JLabel label;
private JToggleButton captureButton = new JToggleButton("CAPTURE");
WindowScanner() {
super("Fist Window");
setSize(150, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
add(captureButton);
setVisible(true);
new Scanner(this);
}
public JToggleButton getCaptureButton() {
return captureButton;
}
}
class Scanner extends JFrame {
private WindowScanner wS;
private int delay = 1000;
private Timer t = new Timer(delay, new taskPerformer());
Scanner(WindowScanner wS) {
super("Second Window");
this.wS = wS;
setBounds(200,0,500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
wS.getCaptureButton().addActionListener(new taskPerformer());
}
private Color randomColor() {
Random rand = new Random();
float r = rand.nextFloat() / 2f ;
float g = rand.nextFloat() / 2f;
float b = rand.nextFloat() / 2f;
Color randomColor = new Color(r, g, b);
return randomColor;
}
private class taskPerformer implements ActionListener {
#Override
public void actionPerformed(ActionEvent captureEvent) {
if(captureEvent.getSource() == wS.getCaptureButton()) {
if (wS.getCaptureButton().isSelected()) {
t.start();
} else {
t.stop();
}
}
if(captureEvent.getSource() == t) {
getContentPane().setBackground(randomColor());
revalidate();
repaint();
}
}
}
}
public class Main {
public static void main (String[] args) {
new WindowScanner();
}
}
This particular piece of code changes the color of the background in the second JFrame to a random color every second using a timer from javax.swing.timer. This code portrays how to get a component, or a variable if you change it, from a different class.
It is mainly these code fragments which allow it.
1
public JToggleButton getCaptureButton() {
return captureButton;
}
This allows other classes to get the component.
2
private WindowScanner wS;
Scanner(WindowScanner wS) {
...
this.wS = wS;
...
}
This makes the current instance of WindowScanner and the instance of WindowScanner declared in Scanner the same instance.
Note: Look into using public getters and setters.
As for your 5 listed questions.
1) Do I really have to make Main class separated from Window class? What the reason?
In most cases yes you do. As SebVb said it is good practice. However you can do something like this if you wish to have them in the same class.
import javax.swing.JToggleButton;
import javax.swing.JFrame;
import java.awt.FlowLayout;
public class Test extends JFrame {
private JToggleButton captureButton = new JToggleButton("CAPTURE");
Test() {
super("Fist Window");
setSize(150, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
add(captureButton);
setVisible(true);
}
public JToggleButton getCaptureButton() {
return captureButton;
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
Test frame = new Test();
}
});
}
}
2) How to make my if statement in Scan method recognize state of my JToggleButton from Window class? Is it impossible and I had a wrong approach to do it?
You were using the wrong approach to do this. See the code and code fragments I have put above for how to do it correctly. (Using public getters.)
3) In Scanner class, i can't make a get/set for my actionPerformed (Netbeans always checked it as an error), but why I can make one for BufferdImage?
I can't entirely say I'm sure what you are asking but see my code above to see if that helps. If it doesn't leave a comment trying to fully explain what you mean.
4) If I can't get question number 3 happen, how can I make If statement to stop capturing using Timer.stop()? Or am I in wrong approach again?
In my code I show you how this can be related to the JToggleButton. See code fragment below
private class taskPerformer implements ActionListener {
#Override
public void actionPerformed(ActionEvent captureEvent) {
if(captureEvent.getSource() == wS.getCaptureButton()) {
if (wS.getCaptureButton().isSelected()) {
t.start();
} else {
t.stop();
}
}
if(captureEvent.getSource() == t) {
getContentPane().setBackground(randomColor());
revalidate();
repaint();
}
}
}
This code says when the JToggleButton fires an ActionEvent if it is selected then start the timer, t.start(), or if it is not selected stop the timer, t.stop().
5) Do my JPanel in Scanner class would be produced and make a viewer for my buffered image?
Again I'm not entirely sure what you are asking but here is my best guess. You have two options.
1
Put boardCaptured directly on the frame.
paint(graphic);
repaint();
revaildate();
2
Create a JPanel like you did but outside the ActionListener
JPanel panel = new JPanel()
boardCaptured = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphic = boardCaptured.createGraphics();
panel.setSize(500,500);
panel.paint(graphic);
add(panel);
private class taskPerformer implements ActionListener {
if(captureEvent.getSource() == t) {
panel.paint(graphic);
panel.revalidate();
panel.repaint();
}
}
I think there's an easier way do do what do you want. Taking your questions by order
Having the main class separated from your Window class allow you to re-use your Windows class everywhere you want. It's a good pratice to only init your GUI objects on your main class
Why don't you have your JToggleButton private and a mehtod wwhich will access to his status ? also, with a static field, all your instaces of Windows will share the same JToggleButton.
It's an anonymous class that contains your actionPerformed method. If you want to see it, you have to create an inner class.
I think your approch is wrong. Using a thread, which will launch your repaint with a specific delay is better. If you create a class which extends Runnable, you can check the state of your button and then do the appropriate action
Your JPanel is inside an ActionListener, i've never seen that and i don't think that it can works.
In a shorter version
Put in your Window class your JPanel, BufferedImage and JToggleButton
Create a specific thread to do your repainting when the JToggleButton is selected
First off this is a homework assignment so explanations and pointers are preferred over flat solutions. We are learning swing and are practicing separate class ActionListeners (bonus question, why would you use a separate class over an inner class, it seems like inner class is simpler and less error prone without losing any real abilities). The problems I have been running into are passing the Frame as a parameter so that the separate class can access the tools it needs, and then using the separate class to actually change the display. The project is supposed to work something like a slideshow, having a timer as a default switch, but also implementing buttons to move manually.
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.event.*;
import java.io.File;
public class SliderFrame extends JFrame{
public SliderFrame(){
File file1 = new File("images"); //change as necessary
File file = new File("images\\CMU");
File[] paths;
paths = file.listFiles();
//file1
ImageIcon left = new ImageIcon("backward.png");
ImageIcon right = new ImageIcon("forward.png");
JButton btnLeft = new JButton(left);
btnLeft.addActionListener(new MyActionListener(this));
JButton btnRight = new JButton(right);
btnRight.addActionListener(new MyActionListener(this));
JTextField jtfTitle = new JTextField("Welcome to CMU!");
JLabel jlbMain = new JLabel();
new Timer(2000, new MyActionListener(this)).start();
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add("PAGE_START", jtfTitle);
panel.add("Center", jlbMain);
panel.add("LINE_START", btnLeft);
panel.add("LINE_END", btnRight);
add(panel);
setTitle("CPS240 SlideShow");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public static void main(String[] args) {
JFrame frame = new SliderFrame();
btnRight.addActionListener(new MyActionListener(frame));
}
}
And then my ActionListener Class
import java.awt.event.*;
import javax.swing.*;
//does it need to extend SliderFrame? Originally I thought it would help with some of my errors
public class MyActionListener extends SliderFrame implements ActionListener {
JFrame frame;
public MyActionListener(JFrame frame) {
this.frame = frame;
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() instanceof Timer){
//here's where I need to be able to change the 'main' label in the frame
} else if(e.getSource() == btnRight){
//trying to figure out if the left or right button was pushed
} else{
}
}
}
I'm not sure if the source of my errors lie in how I set up the format to start with or if I'm just not getting something. Any help or opinions would be greatly appreciated.
bonus question, why would you use a separate class over an inner class, it seems like inner class is simpler and less error prone without losing any real abilities
Initially, you had no choice, as you couldn't have inner classes, but, there may be occasions where the functionality is common and can easily be repeated, for example, an "Open File" action, which is manged by a toolbar button, menu item and keyboard short cut...
First your ActionListener does not need to extend from SliderFrame, but instead, probably wants a reference to an instance of SliderFrame...
This
public class MyActionListener extends SliderFrame implements ActionListener {
Should probably be more like
public class MyActionListener implements ActionListener {
Instead of passing a reference of JFrame, you will want to pass a reference of SliderFrame. Having said that, I have no idea where btnRight is, but I'm pretty sure it shouldn't be maintained within the main method, but within the SliderFrame itself...
public class SliderFrame extends JFrame{
public SliderFrame(){
//...
btnRight.addActionListener(new MyActionListener(this));
You ActionListener should also expect an instance of SliderFrame
public class MyActionListener extends SliderFrame implements ActionListener {
private SliderFrame frame;
public MyActionListener(SliderFrame frame) {
This allows your ActionListener to make use of functionality that is defined by the SliderFrame, which won't be available from an instance of JFrame
Next, you want to provide functionality in your SliderFrame which can be used to update the state of the slide show...
public class SliderFrame extends JFrame{
//...
public void nextSlide() {
//...
}
public void previousSlide() {
//...
}
Then when your ActionListener is triggered, you just call the appropriate methods on SliderFrame
public class NextSlideActionListener extends SliderFrame implements ActionListener {
//...
#Override
public void actionPerformed(ActionEvent e) {
frame.nextSlide();
}
}
(ps- the above example can be used by the Timer and "next" button, because the functionality is the same for both)
Say, you have a subclass of JFrame, and use it to create your own custom JFrame. In this class (we'll call it mainFrame), we create a reference to another custom JFrame class (we'll call this one sidePanel).
In sidePanel, you have different buttons, radio buttons,..
My question is, is there a way to notify mainFrame the user presses on a button?
I've created a (untested) example of what I mean:
class mainFrame extends JFrame {
public mainFrame() {
super("main frame");
//...........
sidePanel panel = new sidePanel();
//...........
}
public static void main(String[] args) {
mainFrame mainF = new mainFrame();
//.........
}
}
And the sidePanel class:
class sidePanel extends JFrame {
public sidePanel() {
super("sidePanel frame");
//...........
JButton button1 = new JButton();
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Notify mainFrame somehow button is pressed
}});
//...........
}
}
To notify mainFrame of an event, the SidePanel instance (really bad name for a Frame) must have a reference to mainFrame. Pass mainFrame as an argument of the SidePanel constructor, and callback mainFrame from the actionPerformed method in SidePanel:
SidePanel panel = new SidePanel(this);
and in SidePanel:
public void actionPerformed(ActionEvent e) {
mainFrame.buttonHasBeenClicked();
...
}
This tightly couples both classes though. A way to decouple them is to make the SidePanel object accept listeners for custom events, and to fire such an event when the button is clicked. The mainFrame would construct the SidePanel instance, and add itself (or an inner anonymous class instance) as a listener to the sidePanel.
See this page for an example.
I have simple Swing GUI with main window JFrame and its main panel derive from JPanel. The panel has some buttons that can be clicked and generate events.
I want these events affect data stored in JFrame because it is my main application - it has some queues for thread, open streams and so on.
So how do I make my button in panel invoke callbacks in its parent frame? What is best practice of this for Java/Swing?
To invoke methods in the parent frame you need a reference to the parent frame. So your JPanel's constructor can be declared like this:
public MyPanel(MyFrame frame){
super();
this.frame = frame;
//the rest of your code
}
And in the JFrame you invoke this constructor like this:
panel = new MyPanel(this);//this refers to your JFrame
In the event handlers attached to your buttons you now have access to the frame and can invoke the various methods as needed.
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//do some stuff
frame.someMethod();//invoke method on frame
//do more stuff
}
});
Have a look on this tutorial for using SwingWorker.
Use addActionListener method on desired buttons specifying the class implementing ActionListener.
ActionListenerClass actionListenerObject = new actionListenerClass();
JButton b = new JButton("Button");
b.addActionListener(actionListenerObject);
public class ActionListenerClass implements ActionListener(){
//or better : actionListenerClass extends AbstractAction
public void actionPerformed(ActionEvent e) {
}
}
EDIT:
Yes, I know this. But the action
listener I want to be in parent JFrame
class - this is the problem
then extends JFrame class making the new derived class implementing the desired interface.
You can implement the ActionListener in your class that has the JFrame (or extends it):
class MyPanelClass {
public MyPanelClass(ActionListener al)
{
//...
JButton myButton = new JButton("Button");
myButton.addActionListener(al);
//...
}
}
class MainClass extends JFrame implements ActionListener {
public void someMethod() {
MyPanelClass mpc = new MyPanelClass(this);
}
#Override
public void ActionPerformed(ActionEvent ev) {
// your implementation
}
}