Separate AcionListener from GUI - java

I'm new to java programming and currently enrolled in a 2week class. I'd like to ask is it possible to separate acionlistener from the gui class? I'd like to apply mvc while I'm still learning but have no idea on how to start with and how should I do it.
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.JOptionPane;
import javax.swing.JTextArea;
public class WriteFile extends JFrame implements ActionListener{
JTextArea textBox;
JButton convert;
WriteFile(){
//windows
setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
setVisible(true);
setSize(300, 300);
setLocationRelativeTo(null);
//others
textBox = new JTextArea("Type something here", 5, 15);
convert = new JButton("Display");
//layout
add(textBox, BorderLayout.CENTER);
add(convert, BorderLayout.LINE_END);
//actionlistener
convert.addActionListener(this);
}
#Override
public void actionPerformed(ActionEvent event) {
String output = "";
output = textBox.getText();
JOptionPane.showMessageDialog(null, output);
}
}
and here is my main:
import java.awt.BorderLayout;
public class main {
public static void main(String[] args) {
WriteFile wc = new WriteFile();
wc.pack();
}
}

I would take a look at the Action API.
It allows you to define a "Action" and it's properties independently of the UI.
It's a very powerful concept as it allows you to centralise commonly used actions in a independent way that allows them to be reused.
Have a look at How to use Actions for more information.

yes just make another class that implements ActionListener outside the gui class
public class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent event) {
JOptionPane.showMessageDialog(null, ((JTextArea) event.getSource()).getText() );
}

Related

Best way to access variables from another class?

I need to access variables from another class and I have done it using 2 different approaches described below.
My question is which of the two is preferable and why since both work quite nicely -or is there another better way to do it?. I have also done it using internal classes but this is inconvenient when the number of code lines gets growing ever larger.
In the following test code the commented asterisks repesent different files:
import java.awt.EventQueue;
import javax.swing.JFrame;
public class Test {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
MainFrame f = new MainFrame("Testing",50,50);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
});
}
}
//**********************************************************************
import java.awt.BorderLayout;
import javax.swing.JFrame;
public class MainFrame extends JFrame {
public MainFrame(String title,int x,int y) {
setTitle(title);
this.setLocation(x, y);
UpperPanel pUp=new UpperPanel();
add(pUp, BorderLayout.NORTH);
LowerPanel pLow=new LowerPanel();
add(pLow, BorderLayout.SOUTH);
pack();
}
}
Now as you can see below UpperPanel must access JButtons from LowerPanel and LowerPanel must access the menu from UpperPanel. For this reason I could pass pUp as a parameter to the LowerPanel constructor but I can't pass pLow as parameter to UpperPanel as it hasn't been created yet.
Therefore I have used 2 methods, one declaring instances of the relevant classes and the other using static variables. The previous 2 classes above are the same in each approach.
Below is the code of the panels in the first case:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JMenu;
import javax.swing.JPanel;
public class LowerPanel extends JPanel implements ActionListener {
static JButton butEnableMenu;
static JButton butEnableBut1;
public LowerPanel() {
setLayout(new FlowLayout(FlowLayout.LEFT));
butEnableMenu=new JButton("Enable menu");
butEnableMenu.setEnabled(true);
butEnableMenu.addActionListener(this);
add(butEnableMenu);
butEnableBut1=new JButton("Enable first button");
butEnableBut1.setEnabled(false);
butEnableBut1.addActionListener(this);
add(butEnableBut1);
}
public void actionPerformed(ActionEvent e) {
UpperPanel up = null;
Object clicked=e.getSource();
JMenu mnu=up.myMenuBar.getMenu(0);
if(clicked.equals(butEnableMenu)) {
mnu.setEnabled(true);
butEnableMenu.setEnabled(false);
}
else if(clicked.equals(butEnableBut1)) {
butEnableMenu.setEnabled(true);
butEnableBut1.setEnabled(false);
}
}
}
//**********************************************************************
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
public class UpperPanel extends JPanel {
static JMenuBar myMenuBar;
public UpperPanel() {
setLayout(new FlowLayout(FlowLayout.LEFT));
myMenuBar=new JMenuBar();
JMenu but2=new JMenu("2nd button");
JMenuItem enableBut2=new JMenuItem("Enable");
but2.setEnabled(false);
enableBut2.addActionListener(new menuActionListener());
myMenuBar.add(but2);
but2.add(enableBut2);
add(myMenuBar);
}
}
class menuActionListener implements ActionListener {
static String clickedMenuItem=null;
LowerPanel lp;
public void actionPerformed(ActionEvent e) {
clickedMenuItem=e.getActionCommand();
JMenuItem mnuItm=(JMenuItem)e.getSource();
JPopupMenu pmen = (JPopupMenu)mnuItm.getParent();
JMenu pmnu =(JMenu)pmen.getInvoker();
if(clickedMenuItem.equals("Enable")) {
pmnu.setEnabled(false);
lp.butEnableBut1.setEnabled(true);
}
}
}
And these are the panels in the second case:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
public class UpperPanel extends JPanel {
static JMenuBar myMenuBar;
public UpperPanel() {
setLayout(new FlowLayout(FlowLayout.LEFT));
myMenuBar=new JMenuBar();
JMenu but2=new JMenu("2nd button");
JMenuItem enableBut2=new JMenuItem("Enable");
but2.setEnabled(false);
enableBut2.addActionListener(new menuActionListener());
myMenuBar.add(but2);
but2.add(enableBut2);
add(myMenuBar);
}
}
class menuActionListener implements ActionListener {
static String clickedMenuItem=null;
public void actionPerformed(ActionEvent e) {
clickedMenuItem=e.getActionCommand();
JMenuItem mnuItm=(JMenuItem)e.getSource();
JPopupMenu jpm = (JPopupMenu)mnuItm.getParent();
JMenu pmnu =(JMenu)jpm.getInvoker();
if(clickedMenuItem.equals("Enable")) {
pmnu.setEnabled(false);
LowerPanel.butEnableBut1.setEnabled(true);
}
}
}
//**********************************************************************
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JMenu;
import javax.swing.JPanel;
public class LowerPanel extends JPanel implements ActionListener {
static JButton butEnableMenu;
static JButton butEnableBut1;
public LowerPanel() {
setLayout(new FlowLayout(FlowLayout.LEFT));
butEnableMenu=new JButton("Enable menu");
butEnableMenu.setEnabled(true);
butEnableMenu.addActionListener(this);
add(butEnableMenu);
butEnableBut1=new JButton("Enable first button");
butEnableBut1.setEnabled(false);
butEnableBut1.addActionListener(this);
add(butEnableBut1);
}
public void actionPerformed(ActionEvent e) {
Object clicked=e.getSource();
JMenu mnu=UpperPanel.myMenuBar.getMenu(0);
if(clicked.equals(butEnableMenu)) {
mnu.setEnabled(true);
butEnableMenu.setEnabled(false);
}
else if(clicked.equals(butEnableBut1)) {
butEnableMenu.setEnabled(true);
butEnableBut1.setEnabled(false);
}
}
}
In general there are 2 ways to access a variable from another class:
You create an object of that class. Then this object has all the variables from the scope of that class assigned to it. For example:
Test t = new Test();
t.name = "test";
You can also create a static variable. Then the variable is assigned to the class not the object of that class. This way you will not need to create an object, but all instances of the class will share the same variable.
//In the scope of the class
static String name;
-------------------------
//when classing the class
Test.name = "The Name of the Test";
If you do not want to create a new instance of a class every time, and always use the same instance, you can create a singleton object. You write a getter method that gets you the object. It looks like this:
public class Test {
Test t;
public static void main(String[] args) {
t = new Test();
}
public Test getTest() {
if (t != null) {
return t;
} else {
t = new Test();
return t;
}
}
}
I see you work with a JFrame. Then you will probably want to make it a singleton. Else you will open a new instance of the JFrame every time you call upon it, which is not recommended.
Does this answer your question?

Error message says I didn't override abstract method, although I did

My question is about an error message I don't understand. Well.. I do understand what it says, but not why it says so or how to fix it. I just started the learning chapter about Swing, and this is one of the examples in my course. I copy/pasted every word into Netbeans but for some reason, it doesn't work.
I have a class called MijnVenster (meaning "MyWindow"), where a JPanel is created with some JButtons with an ActionListener. In the class, there are 2 inner classes describing the performed action.
There's an error message next to both class headers,, saying:
MijnVenster.HoofdLetterListener is not abstract and does not override abstract method actionPerformed (ActionEvent in ActionList)
The other error message next to both #Override statements says:
method does not override or implement a method from a supertype
Isn't that exactly what I did?
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import javafx.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
class MijnVenster extends JFrame {
private static final long serialVersionUID = 1L;
private final static String ZIN = "Hier staat een zin";
private final JTextField textField = new JTextField(ZIN);
public MijnVenster() {
super("Letters");
add(textField);
JPanel panelSouth = new JPanel(new FlowLayout(FlowLayout.LEFT));
JButton buttonHoofdLetters = new JButton("Hoofdletters");
panelSouth.add(buttonHoofdLetters);
JButton buttonKleineLetters = new JButton("Kleine letters");
panelSouth.add(buttonKleineLetters);
add(panelSouth, BorderLayout.SOUTH);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buttonHoofdLetters.addActionListener(new HoofdLetterListener());
buttonKleineLetters.addActionListener(new KleineLettersListener());
}
// an inner class for upper case
private class HoofdLetterListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent event) {
textField.setText(ZIN.toUpperCase());
}
}
// an inner class for lower case
private class KleineLettersListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent event) {
textField.setText(ZIN.toLowerCase());
}
}
}

holding key breaks java swing form

I am working on a basic Java Swing UI application, which seems to be working correctly, except for an issue I've noticed when using the form I created.
The problem is when I am typing in the text boxes, holding down a key seems to break the form--ie, after taking this action, the text boxes no longer seem to accept any input. I find it hard to believe this is a released issue with Java Swing components, but I also can't see how it would be related to my code. Has anyone seen a similar issue before? I am using OS x with Intellij IDEA if that is relevant.
My form is defined thusly:
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JTextField;
#SuppressWarnings("serial")
public class LoginDialogMcve extends JFrame {
protected JTextField stringEntry, dateEntry;
public LoginDialogMcve() {
super("Create Textbox");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
this.stringEntry = new JTextField(5);
add(this.stringEntry);
this.dateEntry = new JTextField(5);
add(this.dateEntry);
}
public static void main(String... args) {
LoginDialogMcve me = new LoginDialogMcve();
me.pack();
me.setLocationByPlatform(true);
me.setVisible(true);
}
}
Edit: Thanks for the feedback, this is quite possibly not a Java problem. Could have something to do with OSx... Uploaded simpler example with mcve
My MCVE that works fine. Test it yourself to see.
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
// import net.miginfocom.swing.MigLayout;
#SuppressWarnings("serial")
public class LoginDialogMcve extends JFrame {
protected JTextField stringEntry, dateEntry;
protected JLabel stringEntryLabel, dateEntryLabel;
protected JButton print;
protected Action validateAction;
public LoginDialogMcve() {
super("Create Textbox");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
// setLayout(new MigLayout("ins 10, gap 5",
// "[][grow]",
// "[][][]"));
this.stringEntryLabel = new JLabel("Name:");
add(this.stringEntryLabel);
this.stringEntry = new JTextField(5);
add(this.stringEntry);
this.dateEntryLabel = new JLabel("Date:");
add(this.dateEntryLabel);
this.dateEntry = new JTextField(5);
add(this.dateEntry);
this.validateAction = new MyAction();
this.print = new JButton(this.validateAction);
add(this.print);
getRootPane().getActionMap().put("validate", this.validateAction);
}
public static void main(String... args) {
LoginDialogMcve me = new LoginDialogMcve();
me.pack();
me.setLocationByPlatform(true);
me.setVisible(true);
}
public class MyAction extends AbstractAction {
public MyAction() {
super("Validate");
}
#Override
public void actionPerformed(ActionEvent e) {
// lots of irrelevant code
}
}
}

Java - a simple PlayerAction counter to practice object-oriented programming

I am learning the basics of Java and decided to practice working on objects, by making a simple program which I called a 'PlayerAction Counter'. It does nothing more that counting an 'action' which is fired by clicking '+1' button and additionally reset the counter, when the 'Reset' button is clicked. For this case I've made a Application.java which holds the main method and launches the program, Interface.java for GUI, EventHandling.java for events and Person.java which holds interface components and methods to change their state. It looks like this:
My idea was to create the window in Interface class and set it's layout, then create two Person objects called PlayerOne and Player Two. The Person class creates two labels and two buttons and set a label of one by a constructor parameter.
I've managed to add these two objects' elements to the GUI by getters and it looks just fine. I added ActionListener to both buttons in Person class and it also works just fine.
I am stuck on getting back to Person fields and methods from EventHandling class. I've created a setValueLabel() which increases by one a value of 'action' and sets it on the label, but I have to idea how to launch the method from another class. I can not make another object inside (throws StackOverFlow error), but should it not somehow refer to those two made in Interface class?
Here is the code:
Application.java
import java.awt.EventQueue;
public class Application {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new Interface().setVisible(true);
}
});
}
}
Interface.java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;
public class Interface extends JFrame {
private JLabel titleLabel;
private JPanel mainPanel;
private JPanel leftPanel, rightPanel;
private JSeparator separator;
Person PlayerOne = new Person("PlayerOne");
Person PlayerTwo = new Person("PlayerTwo");
public Interface() {
//Basic init
setTitle("PlayerAction Counter");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
//Layout
mainPanel = new JPanel();
add(mainPanel);
mainPanel.setLayout(new BorderLayout());
mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
leftPanel = new JPanel();
rightPanel = new JPanel();
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
leftPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
mainPanel.add(leftPanel, BorderLayout.WEST);
separator = new JSeparator(SwingConstants.VERTICAL);
mainPanel.add(separator, BorderLayout.CENTER);
titleLabel = new JLabel("PlayerAction Counter");
mainPanel.add(titleLabel, BorderLayout.PAGE_START);
//Components init
PlayerTwo.getPersonLabel().setAlignmentX(Component.CENTER_ALIGNMENT);
leftPanel.add(PlayerTwo.getPersonLabel());
PlayerTwo.getValueLabel().setAlignmentX(Component.CENTER_ALIGNMENT);
leftPanel.add(PlayerTwo.getValueLabel());
PlayerTwo.getValueUpButton().setAlignmentX(Component.CENTER_ALIGNMENT);
leftPanel.add(PlayerTwo.getValueUpButton());
PlayerTwo.getValueResetButton().setAlignmentX(Component.CENTER_ALIGNMENT);
leftPanel.add(PlayerTwo.getValueResetButton());
rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
rightPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
mainPanel.add(rightPanel, BorderLayout.EAST);
PlayerOne.getPersonLabel().setAlignmentX(Component.CENTER_ALIGNMENT);
rightPanel.add(PlayerOne.getPersonLabel());
PlayerOne.getValueLabel().setAlignmentX(Component.CENTER_ALIGNMENT);
rightPanel.add(PlayerOne.getValueLabel());
PlayerOne.getValueUpButton().setAlignmentX(Component.CENTER_ALIGNMENT);
rightPanel.add(PlayerOne.getValueUpButton());
PlayerOne.getValueResetButton().setAlignmentX(Component.CENTER_ALIGNMENT);
rightPanel.add(PlayerOne.getValueResetButton());
pack();
}
}
Person.java
import java.awt.Component;
import javax.swing.JButton;
import javax.swing.JLabel;
public class Person {
private JLabel personLabel;
private JLabel valueLabel;
private JButton valueUpButton;
private JButton valueResetButton;
private int actionValue = 0;
public Person(String s) {
personLabel = new JLabel(s);
setComponents();
}
private void setComponents() {
valueUpButton = new JButton("+1");
valueResetButton = new JButton("Reset");
valueLabel = new JLabel(""+actionValue);
EventHandling eventHand = new EventHandling(valueUpButton, valueResetButton);
valueResetButton.addActionListener(eventHand);
valueUpButton.addActionListener(eventHand);
}
public void setValueLabel() {
actionValue++;
valueLabel.setText("" +actionValue);
}
public JLabel getPersonLabel() {
return personLabel;
}
public JLabel getValueLabel() {
return valueLabel;
}
public JButton getValueUpButton() {
return valueUpButton;
}
public JButton getValueResetButton() {
return valueResetButton;
}
}
EventHandling.java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
public class EventHandling implements ActionListener {
private JButton valueUpButton;
private JButton valueResetButton;
public EventHandling(JButton valueUpButton, JButton valueResetButton) {
this.valueUpButton = valueUpButton;
this.valueResetButton = valueResetButton;
}
#Override
public void actionPerformed(ActionEvent event) {
if (event.getSource() == valueUpButton) {
//no idea how to launch Person.setValueLabel();
//creating new Person object throws StackOverFlow error and I doubt that's the way
}
else if (event.getSource() == valueResetButton) {
}
}
}
I do hope my issue is understandable. The question is: how should it be done?
Please keep in mind that I am a complete rookie and try to learn OOP, but am very confused by it. Please point my mistakes in thinking and the code.
Any feedback will be greatly appraciated.
Thank you.

access vars from another JFrame Gui

Ok guys, I've made some changes in my code as told by you. I have 3 classes:
The second class (and first GUI): I have 4 JButtons - Simulare, CazParticular, Start and HandSelection, some JLabels and 3 JTextFields; When I press the HandSelection button another frame it's created with different content.
The 3rd class (and second GUI): I have 2 JButtons - Ok and Cancel and other stuff. When I press the Ok button I want to the access to the JTextField(QuesHandText) from the first Gui and use the method setText(). I can't figure this out, I'm thinking on this like 4-5 days and still can't get the answer. Please help me!
What code should I write in if statement to be able to modify the text in the JTextField from 2nd class (first GUI) ?
First class:
import javax.swing.JFrame;
public class Main {
public static void main(String[] args){
//other stuff
GuiMain gui = new GuiMain();
gui.frame1.setLocation(150,150);
gui.frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.frame1.setSize(400,250);
gui.frame1.setResizable(false);
gui.frame1.setVisible(true);
//other stuff
}
}
Second class:
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import javax.swing.*;
public class GuiMain {
public static GuiMain instance;
public static GuiMain getInstance(){
if(GuiMain.instance == null){GuiMain.instance = new GuiMain();}
return GuiMain.instance;
}
public JFrame frame1 = new JFrame();
public JTextField QuesHandText, FlopTurnRiverText, RezultatText;
public JButton Simulare, CazParticular, Start, HandSelection;
public int w1,h1;
public JLabel someText;
static int u=0;
public int j=0;
public GuiMain(){
frame1.setTitle("HoldemTool");
frame1.setLayout(null);
QuesHandText = new JTextField(4);
Simulare = new JButton("Simulare");
CazParticular = new JButton("Caz particular");
Start = new JButton("Start");
HandSelection = new JButton(new ImageIcon(getClass().getResource("GuiPic.png")));
Handler handler1 = new Handler();
CazParticular.addActionListener(handler1);
Simulare.addActionListener(handler1);
HandSelection.addActionListener(handler1);
Start.addActionListener(handler1);
QuesHandText.setEditable(false);
FlopTurnRiverText.setEditable(false);
RezultatText.setEditable(false);
frame1.add(Welcome1);
frame1.add(Welcome2);
frame1.add(QuesHand);
frame1.add(FlopTurnRiver);
frame1.add(Rezultat);
frame1.add(QuesHandText);
frame1.add(FlopTurnRiverText);
frame1.add(RezultatText);
frame1.add(Simulare);
frame1.add(CazParticular);
frame1.add(Start);
}
public JTextField getQuesHandText(){
return QuesHandText;
}
public class Handler implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource()==Simulare)
{
}
if(e.getSource()==CazParticular){
QuesHandText.setEditable(true);
FlopTurnRiverText.setEditable(true);
QuesHandText.setText("");
FlopTurnRiverText.setText("");
RezultatText.setText("");
frame1.setSize(470, 250);
Start.setBounds(3*FlopTurnRiverText.getX(), QuesHand.getY(), 65, h1);
HandSelection.setBounds(3*FlopTurnRiverText.getX(), FlopTurnRiverText.getY(), 65, h1);
frame1.add(HandSelection);
frame1.add(Start);
}
if(e.getSource()==Start){
QuesHandText.setText("Text");
}
if(e.getSource()==HandSelection){
GuiSelection gui2 = new GuiSelection();
gui2.frame2.setVisible(true);
}
}
}}
3rd class
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import javax.swing.*;
public class GuiSelection extends GuiMain {
JFrame frame2 = new JFrame();
GuiMain guiMain;
public JButton Ok,Cancel;
//other stuff
public GuiSelection(){
guiMain = new GuiMain();
frame2.setTitle("Hand selection");
frame2.setSize(1135,535);
frame2.setLayout(null);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setVisible(true);
frame2.setResizable(false);
//other stuff
Handler2 handler2 = new Handler2();
Ok.addActionListener(handler2);
Cancel.addActionListener(handler2);
frame2.add(Ok); frame2.add(Cancel);
}
public class Handler2 implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource()==Cancel){
frame2.hide();
}
if(e.getSource()==Ok)
{
GuiMain.getInstance().getQuesHandText().setText("From Ok");
//When I prees this button "Ok" I want to get access to the JTextField(QuesHandText) in the GuiMain class, and .setText();
//somothing like QuesHandtText.setText("someText");
}
}
}
}
Add to your first GUI the method public JTextField getQuesHandText() and a static method public static JFrame getInstance() which returns the instance of the first GUI. Now you can call SecondClass.getInstance().getQuesHandText() from anywhere to get the JTextField instance. Note that with this method you can only have a single instance of SecondClass at any time.
Your getInstance() method will look as follows:
public class SecondClass extends JFrame {
private static SecondClass instance;
public static SecondClass getInstance() {
if(SecondClass.instance == null)
SecondClass.instance = new SecondClass();
return SecondClass.instance
}
}
Note that you shouldn't create an instance of SecondClass manually.
Use Composition,
1. Create the instance of the class which contains the JFrame, whose JTextField you need to access.
2. Then on that instance call the setter or getter method of the JTextField.
Edited:
Make Sure you have implemented Singleton principle on the Main class, else you will get
a new instance , which you dont want......
In Second class.
public class GuiMain{
Main m = new Main();
m.getText();
m.setText();
// Other stuffs
}
Perhaps you do not really need two Windows. What you need is a Dialog which can be achieved by the class JOptionPane.
Here is a demo code.
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class YourGui implements ActionListener {
private JFrame frame;
private JTextField text;
private JButton takeInput;
public YourGui() {
frame = new JFrame();
frame.setLayout(new GridLayout(2, 1));
text = new JTextField();
takeInput = new JButton("Take Input!");
frame.add(text);
frame.add(takeInput);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 100);
takeInput.addActionListener(this);
}
public void show() {
frame.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e) {
int selection =
JOptionPane.showConfirmDialog(frame, "Select Hand", "Select",
JOptionPane.OK_CANCEL_OPTION);
if (selection == JOptionPane.OK_OPTION) {
text.setText("You selected ok");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
YourGui gui = new YourGui();
gui.show();
}
});
}
}
Use the instance of the initiated Class to get access to its public variables. So you should do something like:
GuiMain main=new GuiMain();
...
main.QuesHandtText.setText("someText");
Alternatively if your JTextField is private (though its not) use an instance method that has public access to change its contents - this is the preferred method:
Class A:
class A {
private JTextField tf;
public void setFieldText(String text) {
tf.setText(text);
}
}
Class B:
class B {
A a = new A();
a.setText("hello");
}
In the handler of GuiMain, pass itself (the main JFrame) as an argument to the constructor of GuiSelection:
GuiSelection gui2 = new GuiSelection(this);
And then change the constructor of GuiSelection from
public GuiSelection(){
guiMain = new GuiMain();
...
to
public GuiSelection(GuiMain guiMain){
this.guiMain = guiMain;
...
Also, it seems quite odd that GuiSelection is a subclass of GuiMain. Probably both of these should be direct subclasses of JFrame.
Also, you should wrap everything in the main method within SwingUtilities.invokeLater since everything related to Swing should run on the event-dispatch thread.
Also, you should never use public member variables: it is very un-Java.

Categories

Resources