Interaction between GUI class and Logic class? - java

In my Java course I have to create a GUI class that interacts with the user and a Logic class that handles the interaction. Since I find this very complicated and hard to understand, I'm looking for some help that can inspire me to continue.
Until now I have just used a text based Menu class with a Swich statement to handle simple input with Scanner and then handle all get and set methods. But I guess I don't need that anymore, and instead could create some Logic class to handle all get and set methods in objects depending on the input from the user. But to begin, how do I create a simple menu in a window and get input value from a GUI class to this Logic class and it's methods?
I add a simple GUI test class that I have done to start this task, but I'm afraid something is missing?
import javax.swing.*;
import java.awt.*;
class Guitest extends JFrame {
JTextField inputLine;
JLabel text;
Container contentPane;
// constructor
public Guitest() {
contentPane = getContentPane();
contentPane.setBackground(Color.WHITE);
setTitle("Test GUI");
setSize(400,200);
setLocation(400,400);
contentPane.setLayout(new FlowLayout(FlowLayout.LEFT));
text = new JLabel("Enter name of new customer");
contentPane.add(text);
inputLine = new JTextField();
inputLine.setColumns(10);
contentPane.add(inputLine);
setDefaultCloseOperation( EXIT_ON_CLOSE );
}
}
And I also add a simple class that make an instance of the window and make it visible. Perhaps this class could be the Logic class?
class Showgui {
// main
public static void main(String[] args) {
Guitest mywindow;
mywindow = new Guitest();
mywindow.setVisible(true);
}
}
All help is preciated! Thanks!

You should study event listeners for a start and then learn about about the MVC pattern, shown here.

You can add an ActionListener to the JTextField so whenever the users presses enter an event is triggered and a piece of code is executed.
For example:
inputLine.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String input = inputLine.getText();
}
});

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

How to return a value from actionPerformed to constructor

I am trying to display on a label (ecran) the file and path I just picked using FileDialog object.
But it is not working.
My code is returning a null value and I don't know why.
Can you please help me?
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class FerPrinc extends Frame implements ActionListener{
Label ecran;
public String path = null;
public FerPrinc(String titlu){
super(titlu);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
final Label ecran = new Label();
add(ecran, BorderLayout.SOUTH);
Button b = new Button("Choose file");
add(b, BorderLayout.NORTH);
b.addActionListener(this);
System.out.println (path);
ecran.setText(path);
pack();
}
public void actionPerformed(ActionEvent e){
FileDialog fd = new FileDialog(this, "Choose file", FileDialog.LOAD);
fd.setDirectory(".");
fd.show();
path = fd.getDirectory()+fd.getFile();
//System.out.println (path);
//ecran.setText(path);
}
}
public class TestFileDialog {
public static void main (String[] args) {
FerPrinc f = new FerPrinc("Test Dile Dialog");
f.show();
}
}
Actual result is NULL.
I am expecting to see the full path to teh file i selected.
You can't, this isn't how ActionListener or GUIs work. Swing, like most GUIs, is event driven, something happens, you react to, it's no linear. Registering a ActionListener to a button isn't going to stop the code execution, waiting for the user to press the button, what happens if they never do? Instead, you need to wait till the ActionListener is triggered and THEN perform the associated actions.
This is a basic concept of an observer pattern.
I think you need to speed some more time going through Creating a GUI With JFC/Swing, How to Use Buttons, Check Boxes, and Radio Buttons and How to Write an Action Listener as this is a basic concept of the API you should understand before embarking on writing your own applications

Swing - Dispose a frame [duplicate]

This question already has answers here:
The Use of Multiple JFrames: Good or Bad Practice? [closed]
(9 answers)
Closed 7 years ago.
My aim is for an action listener to close a specific JFrame when the user hits the JButton to quit.
Overall, when the program starts a large JFrame opens then a small one in front....in my code the user enters some details in this small one and hits submit(for the sake of simplicity, ive omitted this code here and replaced submit with quit)
So when this quit buttons pressed. I expect this small JFrame to close. I can't seem to figure this out. The action listeners in a different class and ive tried making instances and had no luck. I've commented out the code I've tried below when attempting to solve this issue.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class test
{
public static void main(String Args[])
{
makeGUI m = new makeGUI();
}
}
class makeGUI
{
JButton close = new JButton("CLOSE ME");
makeGUI()
{
frame f1 = new frame();
JFrame smallframe = new JFrame(); //want to close this one
JPanel jp = new JPanel(new FlowLayout());
smallframe.setSize(300,300);
smallframe.setLocationRelativeTo(null);
smallframe.setDefaultCloseOperation(smallframe.DISPOSE_ON_CLOSE);
close.addActionListener(new action());
jp.add(close);
smallframe.add(jp);
smallframe.setVisible(true);
}
class action implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//makeGUI s1 = new makeGUI();
if (e.getSource () == close)
{
//s1.smallframe.dispose();
System.out.println("gotcha");
}
}
}
}
class frame extends JFrame
{
frame ()
{
setExtendedState(JFrame.MAXIMIZED_BOTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("big one");
setVisible(true);
}
}
First, it's not a good practice to name classes with a lowercase, so try renaming to something like MakeGUI instead of makeGUI.
The problem with your commented code is that it creates a new instance of makeGUI every time the button is clicked and the action listener is invoked. The result is that when you click on the close button, a new frame is created, then an inner one and this inner one gets immediately closed. The only thing you'd be doing is creating more and more frames. You should keep the instance as a state, for instance as a class member:
class MakeGUI {
JFrame smallframe;
JButton close = new JButton("CLOSE ME");
MakeGUI() {
frame f1 = new frame();
smallframe = new JFrame(); //want to close this one
JPanel jp = new JPanel(new FlowLayout());
smallframe.setSize(300, 300);
smallframe.setLocationRelativeTo(null);
smallframe.setDefaultCloseOperation(smallframe.DISPOSE_ON_CLOSE);
close.addActionListener(new action());
jp.add(close);
smallframe.add(jp);
smallframe.setVisible(true);
}
class action implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == close) {
// use this instead of dispose
smallframe.dispatchEvent(new WindowEvent(smallframe, WindowEvent.WINDOW_CLOSING));
System.out.println("gotcha");
}
}
}
}
If you want to simulate someone pressing the [X] button then you can use this code to programmatically trigger this event:
smallFrame.dispatchEvent(new WindowEvent(smallFrame, WindowEvent.WINDOW_CLOSING));
Aside from that, your code is not working because you are not closing your instance of the small window, instead you are creating another instance and disposing of it. Inside your close event you should be closing the smallFrame instance.
You can do this by either passing your JFrame to the constructor of your ActionListener or making smallFrame a class variable.
It appears you are using the small JFrame as a pop up to get information or display information. If so, you may want to look into the JOptionPane class which is made for "Dialogue Boxes".
Documentation:
http://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html

I created my GUI in a GUI class' constructor. How would I access its Frames, Panels etc. from another class?

First of all, this is a more specific question than it seems to be. To start off: I am currently doing a small application with a rather small GUI, so I decided to make a GUI class, and initialize my whole GUI in this constructor.
This would look like this:
public class GUI extends JFrame{
public GUI{
//Initialize GUI here, including its Frames, Panels, Buttons etc.
}
}
How can I now access the GUIs frame etc. from an external class? If I would create an object of the GUI class, I would simply duplicate my GUI window. I did not come across any other ideas than making the frame, panel and so on static.
I'm somewhat lost right now. Also I'm pretty sure that I am not thinking the right way into this case, but I need someone to point me to the right direction. If someone could help me out, I would be very thankful.
First of all, using static is the worst solution possible, even if your GUI class is a singleton (buf if it is, at least it will work fine).
Why don't you simply create getters and/or setters ? And finally, it is usually not normal that external classes need to access the components of another graphic class. You should wonder if your design is the most fitted for your needs.
Here's a simple GUI to change the background color of a JPanel with a JButton. Generally, this is how you construct a Swing GUI.
package com.ggl.testing;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class ChangeDemo implements Runnable {
private boolean isYellow;
private JFrame frame;
public static void main(String[] args) {
SwingUtilities.invokeLater(new ChangeDemo());
}
#Override
public void run() {
frame = new JFrame("Change Background Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
JPanel namePanel = new JPanel();
JLabel nameLabel = new JLabel(
"Click the button to change the background color");
nameLabel.setAlignmentX(JLabel.CENTER_ALIGNMENT);
namePanel.add(nameLabel);
mainPanel.add(namePanel);
final JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.setBackground(Color.YELLOW);
isYellow = true;
JButton changeButton = new JButton("Change Color");
changeButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
isYellow = !isYellow;
if (isYellow) buttonPanel.setBackground(Color.YELLOW);
else buttonPanel.setBackground(Color.RED);
}
});
buttonPanel.add(changeButton);
mainPanel.add(buttonPanel);
frame.add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
}
You don't access the Swing components of the GUI from other classes. You create other classes to hold the values of the GUI.
Generally, you use the model / view / controller pattern to construct a Swing GUI. That way, you can focus on one part of the GUI at a time.
Take a look at my article, Java Swing File Browser, to see how the MVC pattern works with a typical Swing GUI.
You don't need to make it static or to create a new JFrame object every time.
Have a look at this simple code :
class UseJFrame {
public static void main(String...args) {
Scanner sc = new Scanner(System.in);
JFrame frame = new GUI();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
System.out.println("Press E to exit");
String ip;
while(true) {
System.out.println("Show GUI (Y/N/E)? : ");
ip = sc.nextLine();
if(ip.equalsIgnoreCase("y") {
frame.setVisible(true);
} else if(ip.equalsIgnoreCase("n") {
frame.setVisible(false);
} else { // E or any other input
frame.dispose();
}
}
}
}
Note : Don't make GUI visible through constructor or it will show window at the very starting of creation of JFrame object.
If you want to use the same JFrame object at other places too then pool architecture would be better approach.

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