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.
Related
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?
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());
}
}
}
I'm learning Swing to make GUI in java. My goal is to have 1 mainGUI class to initialize everything and another class that controls all the button.
What I'm doing now is I have a mainGUI which has:
public class mainGUI(){
.... (main and initialize things here) ....
protected JButton btnLogin;
public void initialize(){
btnLogin = new JButton("Login");
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
_buttonLogin();
}
});
}
protected void _buttonLogin(){};
}
Then in my buttonControl I have:
public class buttonControl extends mainGUI{
#Override
protected void _buttonLogin(){
if (isLogin == true){
btnLogin.setEnabled(false);
} else {
// somthing else
}
}
}
The program actually works but not as i expected. When i click on the "login" button, the login button is not set to unclickable. If i don't have the _buttonLogin method in the mainGUI class then I cannot call it from buttonControl class.
I'm just wondering is my approach right in this situation? or any other neat way to have a separated listener class?
Thank you so much
For one you're misusing inheritance. You don't use inheritance to gain access to variables. For that you should use composition.
For example, ButtonControl (and note that the first letter of any class should start with an upper case letter), could have a MainGui field which is passed into it via its constructors. Then the control class can call Gui methods.
class ButtonControl extends AbstractAction {
MainGui gui;
public ButtonControl(MainGui gui, String name, int mnemonic) {
super(name);
putValue(MNEMONIC, mnemmonic);
this.gui = gui;
}
public void actionPerformed(ActionEvent e) {
// ....
}
}
And it could be used like so:
ButtonControl btnCtrl = new ButtonControl(this, "My Button", KeyEvent.VK_M);
JButton myButton = new JButton(btnCtrl);
If you need a Single class for control all the buttons you have you can create a ButtonControl class which can register and de-register buttons to it and handle its events inside the control class. A Simple example code is given
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.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class MainUI extends JFrame{
ButtonController buttonController;
public MainUI() {
super();
buttonController=new ButtonController(this);
initialize();
}
private void initialize() {
JTextField userName=new JTextField();
JPasswordField passwordField=new JPasswordField();
JButton loginButton=new JButton("Login");
loginButton.setActionCommand(ButtonController.LOGIN_COMMAND);
JButton cancelButton=new JButton("Cancel");
cancelButton.setActionCommand(ButtonController.CANCEL_COMMAND);
JPanel contentPane=new JPanel();
contentPane.setLayout(new GridLayout(3,2));
contentPane.add(new JLabel("Username : "));
contentPane.add(userName);
contentPane.add(new JLabel("Password : "));
contentPane.add(passwordField);
contentPane.add(loginButton);
contentPane.add(cancelButton);
buttonController.registerButton(loginButton);
buttonController.registerButton(cancelButton);
setContentPane(contentPane);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
}
/**
* #param args
*/
public static void main(String[] args) {
MainUI ui=new MainUI();
ui.setVisible(true);
}
}
class ButtonController implements ActionListener
{
private MainUI mainUI;
public static String LOGIN_COMMAND="Login";
public static String CANCEL_COMMAND="Cancel";
public ButtonController(MainUI mainUi ) {
this.mainUI=mainUi;
}
public void registerButton(JButton button)
{
button.addActionListener(this);
}
public void deRegisterButton(Button button)
{
button.removeActionListener(this);
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals(LOGIN_COMMAND))
{
((JButton)e.getSource()).setEnabled(false);
}
if(e.getActionCommand().equals(CANCEL_COMMAND))
{
mainUI.dispose();
}
}
}
It's been a long time I've used swing, but we can only call the child class implementation by only from the child class object type :
mainGUI gui = new buttonControl();
we should pass buttonControl object type instance instead of mainGui instance if we want to let the caller use the implementation in buttonControl.
This related to Polymorphism with overidding.
Hopes this help..
I minimized my program to include only the problem and I tried to code exactly as I understood from many examples. When I used the ActionListener, I get problem solved. But I wonder why using ItemListener, checkbox does not operate correctly.
If I run my program without ItemListener, it works correctly. With this ItemListener, checkBox doesn't change state.
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class omaJFrame extends JFrame{
private JCheckBox checkBox1;
public omaJFrame() {
super("Window Title");
TheHandler handler = new TheHandler();
setLayout(new FlowLayout());
checkBox1 = new JCheckBox("Checkbox 1");
add(checkBox1);
checkBox1.addItemListener(handler);
}
private class TheHandler implements ItemListener {
String output = "";
public void itemStateChanged(ItemEvent event) {
if (event.getItem()==checkBox1)
output = String.format("%s", checkBox1.isSelected());
JOptionPane.showMessageDialog(null, output);
}
}
}
import javax.swing.JFrame;
public class EventHandlerMain {
public static void main(String[] args) {
omaJFrame window = new omaJFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(350,200);
window.setVisible(true);
}
}
Works for me. Note also that Swing GUI objects should be constructed and manipulated only on the event dispatch thread.
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class OmaJFrame extends JFrame {
private JCheckBox checkBox1;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
OmaJFrame f = new OmaJFrame();
}
});
}
public OmaJFrame() {
super("Window Title");
setDefaultCloseOperation(EXIT_ON_CLOSE);
TheHandler handler = new TheHandler();
setLayout(new FlowLayout());
checkBox1 = new JCheckBox("Checkbox 1");
add(checkBox1);
checkBox1.addItemListener(handler);
pack();
setLocationByPlatform(true);
setVisible(true);
}
private class TheHandler implements ItemListener {
String output = "";
#Override
public void itemStateChanged(ItemEvent event) {
if (event.getItem() == checkBox1) {
output = String.format("%s", checkBox1.isSelected());
}
JOptionPane.showMessageDialog(null, output);
}
}
}
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() );
}