Passing JButton info from one class to the Main class - java

I have asked a similar question a while ago, so I apologize for asking this if I am abusing the website by doing this, but this question is a bit different. I have a Main class, in which I have created a JFrame, and a JPanel. To avoid clutter, I created another class called "Buttons" to hold all my JButtons. I want to add my JButton to the mainPanel (my JPanel), but I am having trouble inheriting access to the mainPanel from the Button's class.
Here is my Main Class:
package main;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JPanel {
public Main() {
Main m = new Main();
//The main window
JFrame Main = new JFrame("Don't Blink");
Main.setSize(500,500);
Main.setResizable(false);
Main.setVisible(true);
Main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//The main window's panel
JPanel mainPanel = new JPanel(); //I want to add buttons to this panel from the Buttons class
mainPanel.setSize(500, 500);
mainPanel.setVisible(true);
Main.add(mainPanel);
}
public static void main(String[] args) {
Main m = new Main();
}
}
The Buttons class:
package main;
import javax.swing.JButton;
public class Buttons extends Main {
public Buttons(Main m) {
//The button to get a job
JButton workButton = new JButton("Get a job");
mainPanel.add(workButton);//The mainPanel here gives me
//the error "mainPanel can
//not be resolved". It
//doesn't seem to recoginze
//mainPanel
}
}

Few issues here... first of all this is an awful OOP design, but let's see...
You should add as a class variable your mainPanel
public class Main extends JPanel {
JPanel mainPanel;
Now to start you don't declare the type, since you want to have it in a class scope:
this.mainPanel = new JPanel(); // I want to add buttons to this panel from the Buttons class
You are trying to call it in Buttons but you only declared it locally in your Main constructor.
You are calling new Main inside new Main, I believe this will end in an infinite recursion.
remove Main m = new Main(); from your Main constructor.
I don't think that inherit Main on Buttons is good, but if you really need that, Main does not know about Buttons, anyway you could use the main method in Buttons instead of Main. I'd remove the main method from Main class and add it to Buttons class instead since it is extending Main anyway.
public static void main(String[] args) {
Buttons m = new Buttons();
}
Now, buttons claims to need a Main as argument, that is weird because you want to call the Main, not another Main, I'd remove the argument Main from Buttons constructor. You may think that then you will not have mainPanel but Buttons is already a Main class so it has mainPanel too.

From an OOP point of view, this is quite bit strange, and does not seem right.
In my opinion, you should not create a separate class for this, just a method, inside the Main class, and pass the reference of the panel to it:
public class Main extends JPanel{
public Main(){
Main m = new Main();
//The main window
JFrame Main = new JFrame("Don't Blink");
Main.setSize(500,500);
Main.setResizable(false);
Main.setVisible(true);
Main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//The main window's panel
JPanel mainPanel = new JPanel(); //I want to add buttons to this panel from the Buttons class
mainPanel.setSize(500, 500);
mainPanel.setVisible(true);
Main.add(mainPanel);
addTheButtons(mainPanel); //this does the heavy lifting
}
private addTheButtons(JPanel mainPanel){
//The button to get a job
JButton workButton = new JButton("Get a job");
mainPanel.add(workButton);
}
}
This would work as long as you don't want to reuse the logic creating the buttons in other classes - but as far as my experience goes with this, this granularity of UI code doen't get reused much...

First of all, it is bad practice to name a variable name with a capital letter, like Main. Use camelCase.
Please don't use inheritance for this. I recommend you define a method as follows:
private Container getButtonContainer() {
final JPanel buttonPanel = new JPanel(...);
...
buttonPanel.add(button1);
buttonPanel.add(button2);
return buttonPanel;
}
Then add this panel to the main panel.

Related

How to close a Java JFrame based on action event from a JPanel

How would I go about closing a JFrame based on an ActionEvent from a button click within a JPanel?
I have a total of three classes:
Application: contains the main method and runs the program by creating a FrameStartUp object.
FrameStartUp: extends the JFrame class and displays the contents within StartUpPanel.
StartUpPanel: extends the JPanel class and has all the components and ActionEvents.
Within the StartUpPanel class, I have a button with an ActionEventListener waiting for the button to be clicked.
When the button is clicked I want the application to shut down completely. I know of a method called .dispose() for the JFrame class, but I can't use it because creating an object of FrameStartUp would just run another GUI (run by the constructor).
As I am new to programming and swing, I do not know any other way to fix this, other than getting rid of the StartUpPanel and just creating a JPanel within the FrameStartUp class.
Are there any methods provided by Swing that can access the current JFrame that the panel is on, so the program can close when the ActionEvent is triggered?
I know of a method called .dispose() for the JFrame class
This will work if you explicitly set setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Are there any methods provided by Swing that can access the current JFrame that the panel is on
Yes... SwingUtilities provides one called getWindowAncestor().
button.addActionListener(e -> {
SwingUtilities.getWindowAncestor((Component)e.getSource()).dispose();
});
... or more commonly, you can chose to reference a final variable to achieve the same effect...
final JFrame swingStuff = this; // or expose via a getter/setter
button.addActionListener(e -> {
swingStuff.dispose();
});
... however the final variable placement and setter/getter would need a small reproducible code example.
And finally, as others have mentioned, System.exit(0) works quite fantastically well too, so as long as it doesn't break the lifecycle of any of your other components.
My test class:
import javax.swing.*;
import java.awt.*;
public class SwingStuff extends JFrame {
// Our main JFrame
public SwingStuff() {
super();
// The button
JButton button = new JButton("Close");
button.addActionListener(e -> {
SwingUtilities.getWindowAncestor((Component)e.getSource()).dispose();
});
// The JPanel and nested components
JPanel startupPanel = new JPanel();
startupPanel.add(button);
add(startupPanel);
pack();
// Make sure the app exits when closed
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// Just our entry point
public static void main(String ... args) {
SwingUtilities.invokeLater(() -> {
new SwingStuff().setVisible(true);
});
}
}

Unable to implement mouse listener

I am trying to implement mouse listener however I can not seem to get it to work. My code doesnt have any errors, but when I click on the frame I I can not get a message to print out. I have tried extending the class HandleClassOne to viewOne, but that also wouldn't work. Any thoughts?
The main class creates a frame and then creates an instance of viewOne on the frame.
public class main{
protected static JFrame window;
public static void main(String args[]){
window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(400,400);
window.setVisible(true);
new viewOne(window);
}
}
The viewOne class adds a panel and a label to a frame. It also adds a mouse listener to the panel.
public class viewOne {
private static JPanel panel1;
private static JLabel label1;
public viewOne(JFrame frame) {
panel1 = new JPanel();
label1 = new JLabel("View One");
panel1.add(label1);
frame.add(panel1);
panel1.setBackground(Color.red);
frame.validate();
}
public static void mouseAdd() {
HandleClassOne handle = new HandleClassOne();
panel1.addMouseListener(handle);
panel1.addMouseMotionListener(handle);
}
public static void main(String[] args) {
mouseAdd();
}
}
The HandleClassOne class should print out a message when the panel created in viewOne is clicked.
public class HandleClassOne extends main implements MouseListener, MouseMotionListener {
public void mouseClicked(MouseEvent e) {
System.out.println("mouse clicked");
}
}
While you have defined the function mouseAdd(...) I don't see you calling it.
Try (within the constructor)
public viewOne(JFrame frame) {
...
mouseAdd();
...
}
naturally, you'll need to do this after the panel1 is set.
Note that there are other issues, too
You don't invoke presenting the JFrame properly within your main function in your main class. Look up a basic tutorial on Java Swing, where it talks about the event dispatch thread and the requirements to not present within your program's main thread of execution.
You have an additional main function in your viewOne class, which is not how these things are wired up.
You added the mouseAdd() method (which is responsible for registering the mouse listener) inside the main method of viewOne class.
Please keep in mind that main method only gets called whenever you are running it as entry point class for your application. Here you have main class to act as an entry point.
You kept main method in viewOne class as well and it will get called only when you are running it as an individual piece (not along with main class).
To fix the issue here, keep your mouseAdd() method call inside viewOne() constructor as constructor gets called every time whenever object is getting created.
public viewOne(JFrame frame) {
panel1 = new JPanel();
label1 = new JLabel("View One");
panel1.add(label1);
frame.add(panel1);
panel1.setBackground(Color.red);
mouseAdd();
frame.validate();
}

Linking GUI to Main in Java

I'm very new to Java and OOP in general and I want to call my GUI class from my Main class which will be the start point for my program.
This should be pretty simple but any help will be appreciated
GUIForm1 Code:
import javax.swing.*;
import java.awt.*;
public class GUIForm1 {
private JPanel MainPanel;
private JPanel MenuPanel;
Private JPanel AnimationPanel;
private JButton greenTrail;
private JButton purpleTrail;
private JSeparator animationMenuDivider;
private JSlider rangeSlider;
//more components
public GUIForm1() {
JFrame frame = new JFrame("GUIForm1");
frame.setContentPane(new GUIForm1().MainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
frame.pack();
frame.setVisible(true);
}
}
Main Class Code:
public class ProjectileSim {
public static void main(String[] args){
GUIForm1 gui = new GUIForm1();
}
}
Your code is good. Your main class instantiation of the GUIForm class is also correct. The reason why you are having an issue is here:
frame.setContentPane(new GUIForm1().MainPanel);
That is your issue in the code. You are trying to call the constructor of your class within your class to set a MainPanel that is non-existent.
If you are using NetBeans (Or any IDE) it is simple enough for you to just drag a JPanel onto your GUI and then you can set your frames content frame to that (for example):
frame.setContentPane(myNewJPanel);
Give it a go. Comment out the line where you are setting your contentframe and see what I mean.
(this is how you comment out by the way :
//frame.setContentPane(new GUIForm1().MainPanel);
You just insert 2 forward slashes in front of the line that you do not want to be executed.
All the best. Let me know of the outcome.
add text Jfield or JButton or change the colour of the panel you feel somthing new.
And the main method is must in java which should exist in public classs.
Your problem is very general. I would advise you to go through https://docs.oracle.com/javase/tutorial/uiswing/start/index.html at the beginning.
But, what you should do in order to get any visible result at all would be:
public GUIForm1() {
JFrame frame = new JFrame("GUIForm1");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
frame.setVisible(true);
}
Normally you should initialize the GUI objects (like JButtons, JFrame etc.) and then add them to the Content Pane, and after that set everything to visible. But go through the tutorials, since they cover all the basics.

Unable to set size and location of button in java

I was just writing an application and i was not able to set location and size of button to desired values. whenever i open code the button comes at same location and with same size
Here is my code
public class main_class {
public static void main(String args[]){
Main_page mp = new Main_page();
mp.start();
}
}
Second file is:
import javax.swing.*;
import java.awt.*;
public class Main_page extends JPanel {
private static final long serialVersionUID = 1L;
public void start(){
JPanel panel1 = new JPanel();
panel1.setBackground(Color.pink);
JButton Button1 = new JButton("Programmer");
Button1.setSize(10, 100);
Button1.setLocation(200,500);
panel1.add(Button1);
JFrame frame1 = new JFrame("Main Window");
frame1.setSize(700,500);
frame1.setContentPane(panel1);
frame1.setResizable(false);
frame1.setVisible(true);
}
}
what is the problem.
What is the problem?
I'm so glad you asked that question. It's not just a matter of getting a Swing application to work. You must get the Swing application to work correctly so that you can add more functionality to your Swing application without your Swing application breaking.
I added a call to SwingUtilities invokeLater in the main method. This call ensures that the Swing components are defined and used on the Event Dispatch thread (EDT).
I changed the name of your class to MainPage to conform to Java standards for naming classes.
I implemented Runnable to make the invokeLater method parameter easier to define.
I removed the extends of JPanel. You use Swing components. The only reason to extend a Swing component is when you want to override one of the component methods. The only reason you extend any Java class is when you want to override one of the class methods.
I changed the name of your MainPage method to run.
I set a layout (FlowLayout) for your JPanel. You must always use a layout manager for your Swing components.
I made button1 lowercase. Java field names are lowercase, so you can easily differentiate them from class names.
I added a call to the JFrame setDefaultCloseOperation to your run method. Without this call, your Swing application will not stop executing when you close the window. After a while, you'll have dozens of copies of your Swing application running, with no easy way to stop them.
I added a call to the JFrame pack to let the JFrame expand or contract to fit the components, rather than be a fixed size.
Here's the revised code. I put the main method in the MainPage class to make it easier to paste the code.
package com.ggl.testing;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class MainPage implements Runnable {
#Override
public void run() {
JPanel panel1 = new JPanel();
panel1.setBackground(Color.pink);
panel1.setLayout(new FlowLayout());
JButton button1 = new JButton("Programmer");
panel1.add(button1);
JFrame frame1 = new JFrame("Main Window");
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.add(panel1);
frame1.pack();
frame1.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new MainPage());
}
}
if you want to set the size and location manually then you can use panel1.setLayout(null); as people have said in the comments this isn't recommended.

Difficulty removing all components from a Jpanel

G'day all,
I am coding a main menu for a project. The menu displays properly. I have also set up ActionListeners for the three buttons on the menu.
What I wish to do is reuse the JPanel for a new set of radio buttons when the user chooses "Start a New Game".
However, coding ActionPerformed to remove the existing components from the JPanel has me stumped. I know removeAll is somehow important, but unfortunately NetBeans informs me I cannot call it on my mainMenu JPanel object within ActionPerformed. So i have commented it out in my code below, but left it in so you can see what I am trying to do.
Your thoughts or hints are appreciated.
Here is my main code:
public class Main {
public static void main(String[] args) {
MainMenu menu = new MainMenu();
menu.pack();
menu.setVisible(true);
}
}
Here is my mainMenu code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainMenu extends JFrame implements ActionListener {
JButton startNewGame = new JButton("Start a New Game");
JButton loadOldGame = new JButton("Load an Old Game");
JButton seeInstructions = new JButton("Instructions");
public MainMenu() {
super("RPG Main Menu");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainMenu = new JPanel();
mainMenu.setLayout(new FlowLayout());
startNewGame.setMnemonic('n');
loadOldGame.setMnemonic('l');
seeInstructions.setMnemonic('i');
startNewGame.addActionListener(this);
loadOldGame.addActionListener(this);
seeInstructions.addActionListener(this);
mainMenu.add(startNewGame);
mainMenu.add(loadOldGame);
mainMenu.add(seeInstructions);
setContentPane(mainMenu);
}
public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
if (source == startNewGame) {
// StartNewGame code goes here
// mainMenu.removeAll();
}
if (source == loadOldGame) {
// LoadOldGame code goes here
}
if (source == seeInstructions) {
// Quit code goes here
}
}
}
Consider using a CardLayout instead, which manages two or more components (usually JPanel instances) that share the same display space. That way you don't have to fiddle with adding and removing components at runtime.
You need mainMenu to be a member variable:
public class MainMenu extends JFrame implements ActionListener {
JButton startNewGame = new JButton("Start a New Game");
JButton loadOldGame = new JButton("Load an Old Game");
JButton seeInstructions = new JButton("Instructions");
JPanel mainMenu = new JPanel();
Why do you feel the need to re-use this object?
You don't have a reference to mainMenu actionPerformed use. If you declare mainMenu with the buttons. It would work.
The problem is that the actionPerformed method is trying to call the JPanel mainMenu which is out of scope, i.e. the mainMenu variable is not visible from the actionPerformed method.
One way to get around this is to have the JPanel mainMenu declaration in the class itself and make it an instance field which is accessible to all instance methods of the class.
For example:
public class MainMenu extends JFrame implements ActionListener
{
...
JPanel mainMenu;
public MainMenu()
{
...
mainMenu = new JPanel();
...
}
public void actionPerformed(ActionEvent e)
{
...
mainMenu.removeAll();
}
}
Avoid attempting to "reuse" stuff. Computers are quite capable of tidying up. Concentrate on making you code clear.
So instead of attempting to tidy up the panel, simply replace it with a new one.
Generally a better way to write listeners is as anonymous inner classes. Code within these will have access to final variables in the enclosing scope and to members of the enclosing class. So, if you make mainMenu final and you ActionListeners anonymous inner classes, your code should at least compile.
Also don't attempt to "reuse" classes. Try to make each class do one sensible thing, and avoid inheritance (of implementation). There is almost never any need to extend JFrame, so don't do that. Create an ActionListener for each action, rather than attempting to determine the event source.
Also note, you should always use Swing components on the AWT Event Dispatch Thread. Change the main method to add boilerplate something like:
public static void main(final String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() { public void run() {
runEDT();
}});
}

Categories

Resources