I have the following problem: I have 2 classes in my game - CONFIGUREGAME (CG) and ROULETTETABLE (RT) - and the user is able to specify details about the game like his name or his money in the class CG. In the class RT I want the input from a JTextField from the class CG to be shown on a button in class RT.
Here's my code (I simplified it alot):
public class CONFIGUREGAME extends JFrame implements ActionListener
{
Jframe frame = new JFrame("...");
public JTextField playername1 = new JTextField();
public JButton startgame = new JButton();
public CONFIGUREGAME()
{
startgame.addActionListener(this);
}
public static void main(String (String[] args)
{
new CONFIGUREGAME();
}
public void actionPerformed(ActionEvent aEvt)
{
if(aEvt.getSource()==startgame)
{
frame.dispose();
new ROULETTETABLE();
}
}
now Class 2:
import ...;
public class ROULETTETABLE extends CONFIGUREGAME implements ActionListener
{
public player1 = new JButton();
public ROULETTETABLE()
{
String Strplayername1 = playername1.getText();
player1.setText(Strplayername1);
}
public static void main(String (String[] args)
{
new ROULETTETABLE();
}
}
I tried various ways that were supposed to help but they didn't. My UI is working totally fine so if there's a mistake in it it's because I made a mistake simplifying it.
I appreciate any from of help!
You need something like this.
public class CONFIGUREGAME extends JFrame implements ActionListener
{
Jframe frame = new JFrame("...");
public JTextField playername1 = new JTextField();
public JButton startgame = new JButton();
public CONFIGUREGAME()
{
startgame.addActionListener(this);
}
public static void main(String (String[] args)
{
new CONFIGUREGAME();
}
public void actionPerformed(ActionEvent aEvt)
{
if(aEvt.getSource()==startgame)
{
frame.dispose();
new ROULETTETABLE(playername1.getText());
}
}
}
public class ROULETTETABLE extends CONFIGUREGAME implements ActionListener
{
public JButton player1 = new JButton();
public ROULETTETABLE(String playerName)
{
player1.setText(playerName);
}
public static void main(String (String[] args)
{
new ROULETTETABLE();
}
}
P.S. Please learn the Java method and class notation. CapitalizedClassName, firstWordLowercaseMethodName, firstWordLowercaseVariableName, UPPER_CASE_CONSTANT_NAME
Option #1
Pass the result CONFIGUREGAME to ROULETTETABLE
public class CONFIGUREGAME extends JFrame implements ActionListener {
//Jframe frame = new JFrame("..."); WHY?
//...
public void actionPerformed(ActionEvent aEvt) {
if (aEvt.getSource() == startgame) {
frame.dispose();
new ROULETTETABLE(playername1.getText());
}
}
public class ROULETTETABLE extends JFrame /* CONFIGUREGAME why? */implements ActionListener
{
public JButton player1 = new JButton();
public ROULETTETABLE(String playerName)
{
player1.setText(playerName);
}
}
I'm not a fan of this because it couples of the CONFIGUREGAME class to the ROULETTETABLE
A Better Option...
Use a JDialog to collect the configuration information and then pass it to the ROULETTETABLE class
First, some reconfiguration of the classes. As a general rule, avoid extending from top level classes like JFrame, they couple the code to single access point and reduce it's flexibility and re-use
public class Roulettetable extends JPanel implements ActionListener {
private JButton player1 = new JButton();
public Roulettetable(String name) {
player1.setText(name);
}
}
public class ConfigureGame extends JPanel {
private JTextField playername1 = new JTextField();
public ConfigureGame() {
}
public String getPlayerName() {
return playername1.getText();
}
}
Then you wrap it altogether...
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
ConfigureGame configureGame = new ConfigureGame();
JOptionPane.showOptionDialog(null, configureGame, "Configure Game", JOptionPane.OK_OPTION, JOptionPane.PLAIN_MESSAGE, null, new Object[] {"Start"}, 0);
String name = configureGame.getPlayerName();
Roulettetable roulettetable = new Roulettetable(name);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(roulettetable);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
This example is pretty simple, it simply makes use of JOptionPane to display the dialog.
Have a look at How to Make Dialogs for more details
Related
I need get some data from "Board" Component but i dont know how. I tried Frame.Component.data but is doesn't work.
Code:
public class window extends JFrame {
public window() {
add(new Board());
setResizable(true);
pack();
setTitle("Game");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
JFrame ex = new window();
ex.setVisible(true);
ex.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
//ex.Board.data
System.exit(0);
}
});
});
}}
First a little tip to have a quick answer : reduced your code at the minimun to reproduced your bug its easier to understand especially in your case where your real purpose is in a comment ... and then make it compilable ...
To answer to your issue : personnaly i use dedicated fields to have a direct link to object i want to handle later there is two reason first a field is easy to use and don't use lot of memory . Second this solution will not depend on the way your frame is organised. an other way to get the same result is the second snippet the probelme is that if you change your frame organisation you will have to modify your listener
package so1;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Window extends JFrame {
private static final long serialVersionUID = 3000003489937872937L;
public class Data {
public void doSomethings() {
System.out.println("toto");
}
}
public class Board extends JLabel {
private static final long serialVersionUID = 7362684018638848838L;
private Data data = new Data();
}
private Board board;
public Window() {
board = new Board();
add(board);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
}
public static void main(String[] args) {
Window ex = new Window();
ex.setVisible(true);
ex.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
ex.board.data.doSomethings();
}
});
}
}
the bad solution :
public static void main(String[] args) {
Window ex = new Window();
ex.setVisible(true);
ex.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
((Board)((JPanel)((JLayeredPane)((JRootPane)ex.getComponents()[0]).getComponents()[1]).getComponents()[0]).getComponents()[0]).data.doSomethings();;
}
});
}
public class A2 {
public class B implements ActionListener{
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Fing");
}
}
public class C implements ActionListener{
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Fang");
}
}
public class D implements ActionListener{
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Foom");
}
}
public A2(){
JButton a = new JButton("Fing");
JButton b = new JButton("Fang");
JButton c = new JButton("Foom");
a.addActionListener(new B());
b.addActionListener(new C());
c.addActionListener(new D());
}
public static void main(String[] args) {
A2 a2 = new A2();
}
The problem I encountered is quite simple, but complex. I want it to shorten the code without retouching its functionality. For example, the code is showing to many actionlisteners and actionperformed, and I was trying to make it one class pulling out System.out.println(); and putting in String value on it. However, the coding does not work in this simple ways. Please help me out to curtail this code as simple and increase the readability. Thanks.
It's impossible to know what things you could do, I'm personally a fan of self documenting code, so sometimes, you need to be careful when trying to optimise solutions.
My first thought might be to start with the Action's API, which allows you to design a self contained unit of work
public class CommonAction extends AbstractAction {
public CommonAction(String name) {
putValue(NAME, name);
putValue(SHORT_DESCRIPTION, "This is a tool tip for " + name);
}
#Override
public void actionPerformed(ActionEvent e) {
System.out.println(getValue(NAME));
}
}
You could extend it further to provide more customisation if you needed, overriding the actionPerformed method, but, that's up to you.
Then you just need to apply to your buttons...
public class A2 {
public A2() {
JButton a = new JButton(new CommonAction("Fing"));
JButton b = new JButton(new CommonAction("Fang"));
JButton c = new JButton(new CommonAction("Foom"));
}
}
Or your menu's or your key bindings, Action is a rather flexible API supported by a number of other components
You can define single class MyActionListener which implements ActionListener as shown below:
public class MyActionListener implements ActionListener {
private String input;
public MyActionListener(String input) {
this.input = input;
}
#Override
public void actionPerformed(ActionEvent e) {
System.out.println(input);
}
}
public A2(){
String[] inputs = {"Fing","Fang","Foom"};//Array of JButton inputs
for(int i=0;i<inputs.length;i++) {
JButton jButton = new JButton(inputs[i]);//create JButton instance
jButton.addActionListener(new MyActionListener(inputs[i]));
}
}
This question already has answers here:
How can I implement ActionListener with a non abstract class? Java
(3 answers)
Closed 7 years ago.
I have this code that uses a textfield from which the program is to receive input from the user so I'm trying to add an ActionListener to my textfield input. However, when I compile I get this error:
Quiz.java:5: error: Quiz is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener
public class Quiz implements ActionListener {
Code:
public class Quiz implements ActionListener {
private static Label lblInput;
private static TextField tfInput;
private static String cityIn;
public void europe() {
JFrame frame = new JFrame();
frame.setLayout(null);
lblInput = new Label("Skriv in huvudstaden i : "); // Construct Label
lblInput.setBounds(40,30,300,40);
frame.add(lblInput);
tfInput = new TextField(10);
tfInput.setBounds(40,70,300,40);
frame.add(tfInput);
tfInput.addActionListener(this);
frame.setTitle("Europa");
frame.setSize(375, 150);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
You must override the actionPerformed(ActionEvent e) method :
public class Quiz implements ActionListener {
private static Label lblInput;
private static TextField tfInput;
private static String cityIn;
public void europe() {
....
}
#Override
public void actionPerformed(ActionEvent e) {
// Your code
}
}
EDIT : (Second way)
You can handle events on your textfield using a custom ActionListener:
public class Quiz implements ActionListener {
private static Label lblInput;
private static TextField tfInput;
private static String cityIn;
public void europe() {
....
tfInput.addActionListener(new CustomActionListener());
...
}
#Override
public void actionPerformed(ActionEvent e) {
// Your code
}
}
class CustomActionListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
// Your code
}
}
I'm trying to separate my Swing GUI from my actual code. In short, I want the user to kick off a process (based on the user's selections); in this case, the JFrame will no longer be needed.
What I couldn't figure out is how to share the user's selection from the GUI.class with the Main.class.
Do you have any advice for me?
Here's my code:
public class Main {
public static void main(String[] args) {
// Show GUI
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
GUI gui = new GUI(templates);
gui.setVisible(true);
}
});
// Kick off a process based on the user's selection
}
}
public class GUI extends JFrame {
private static final long serialVersionUID = 1L;
public GUI(Object[] objects) {
setTitle("GUI");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 350, 100);
setLocationRelativeTo(null);
JPanel cp = new JPanel();
cp.setBorder(new EmptyBorder(10, 10, 10, 10));
setContentPane(cp);
JLabel lbl = new JLabel("Selection:");
cp.add(lbl);
final JComboBox<String> comboBox = new JComboBox<String>(new String[] { "One", "Two", "Three" });
comboBox.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
dispose();
// Share the selected item with Main.class
}
});
cp.add(comboBox);
}
}
You could create an object to store the selection result and pass it in to the constructor of the GUI class. Set the selection result in that object before closing the UI and then your Main class could access the value:
public class SelectionResult {
private String selectionResult;
public void setSelectionResult(final String selectionResult) {
this.selectionResult = selectionResult;
}
public String getSelectionResult() {
return this.selectionResult;
}
}
Then, you could modify the GUI constructor like this:
private final SelectionResult selectionResult;
public GUI(Object[] objects, SelectionResult selectionResult) {
this.selectionResult = selectionResult;
...
Create a SelectionResult object in your Main class, and pass it to the constructor of the GUI class. In you GUI class ActionListener, you can then call the setSelectionResult() method with the selected value and that value will be available from the Main class.
You would need to add code to make your main method wait while you are waiting for the value to be set in the UI and then proceed with your logic based on the selection.
A Good way of doing this is use Callback mechanism.
Steps to follow:
create a callback interface
interface Callback {
void execute(Object result);
}
GUI class will implement Callback interface but without providing any implementation
Make GUI class abstract
abstract class GUI extends JFrame implements Callback
Now create an object of GUI class providing actual implementation of Callback interface
Here you can use Anonymous class
GUI gui = new GUI() {
#Override
public void execute(Object result) {
System.out.println("You have selected " + result);
}
};
You can pass any thing in execute() method of Callback.
comboBox.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
dispose();
// Share the selected item with Main.class
// Callback
execute(comboBox.getSelectedItem());
}
});
Here Main class is responsible for capturing the response of Callback that is directed by GUI class.
Here is the code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class Main {
public static void main(String[] args) {
// Show GUI
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
GUI gui = new GUI() {
#Override
public void execute(Object result) {
System.out.println("You have selected " + result);
}
};
gui.setVisible(true);
}
});
// Kick off a process based on the user's selection
}
}
interface Callback {
void execute(Object result);
}
abstract class GUI extends JFrame implements Callback {
private static final long serialVersionUID = 1L;
public GUI() {
setTitle("GUI");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 350, 100);
setLocationRelativeTo(null);
JPanel cp = new JPanel();
cp.setBorder(new EmptyBorder(10, 10, 10, 10));
setContentPane(cp);
JLabel lbl = new JLabel("Selection:");
cp.add(lbl);
final JComboBox comboBox = new JComboBox(new String[] { "One", "Two", "Three" });
comboBox.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
dispose();
// Share the selected item with Main.class
execute(comboBox.getSelectedItem());
}
});
cp.add(comboBox);
}
}
I need to have a reference to the Client because I need to invoke a setWinTitle to change the title of current window. How to fix it?
public class Client {
public static void main(String[] args){
JPanel gui= startGUI();
...
}
private static JPanel startGUI(){
f = new JFrame();
JPanel gui = new JPanel(this); // error
}
public void setWinTitle(String tite){
f.setTitle(tite);
}
}
public class JPanel extends javax.swing.JPanel {
Client client;
public JPanel(Client cl) {
client= cl;
initComponents();
}
...
}
You need to create an instance of Client:
JPanel gui = new JPanel(new Client());