I have an interface with two buttons, one to save a word and other to store a letter.
To allow me to manage the word I'll enter with button 1, I have a class, word class.
In this class, There are getters and setters, and methods.
The method "table" allows me to retrieve the value that I'll get my button 1, then save it as a tab char []
.
I wish I could take the same char array []
(button1), with the same values in my 2nd button
In summary, I would like to use the word entered in the button 1, on the button 2.
but I don't know how do it?
//BUTTON 1
final JFrame popup = new JFrame();
//create new instance of JButton
final Mot monMot = new Mot();
newButton.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
String name = JOptionPane.showInputDialog(popup, "Enter one word", null);
monMot.setMot(name);
monMot.tableau();
try {
monMot.affichage();
} catch (Exception e) {
System.out.println(e);
}
}
//BUTTON 2 ONE KEY
final JFrame popup = new JFrame();
Mot monMot = new Mot();
boolean flag = false;
String key = JOptionPane.showInputDialog(popup, "Enter one key",null);
try {
while (flag == false) {
if (key.length() == 1) {
flag = true;
} else {
key = JOptionPane.showInputDialog(popup, "Enter one key",null);
}
}
} catch (Exception e) {
System.out.println(e);
}
}
And my Class public class Mot {
private String mot;
private char[] tab;
//getter et setter
public String getMot() {
return mot;
}
public void setMot(String mot) {
this.mot = mot;
}
//constructeur plein
public Mot(String mot, char[] tab) {
this.mot = mot;
this.tab = tab;
}
//constructeur vide
public Mot() {
}
//methodes
public void affichage() {
for (int i = 0; i < this.tab.length; i++) {
System.out.println(this.tab[i]);
}
}
//placage de chaque lettre dans un tableau
public void tableau() {
this.tab = this.mot.toCharArray();
}
}
My suggestion is to make a simple MVC.
I am not sure I understand you.
The controllers will implement the two buttons action. You should store the String in your Model or whatever when Button 1 action is performed and you should get the stored String when Button 2 action performed.
Example code:
public class Controller {
private View view;
private Model model;
//constructor will get the view and the model, and adds ActionHandlers
public Controller(final Model amodel, final View aview) {
this.view=aview;
this.model=amodel;
addOneButtonActionHandler();
addSecondButtonActionHandler();
}
public void addOneButtonActionHandler(){
ActionListener actionHandler= new ActionListener() {
@Override
public void actionPerformed(final ActionEvent e) {
//some action to get the String from user (?)
model.storeItem(string);
}
};
view.addActionToOneButton(actionHandler);
}
public void addSecondButtonActionHandler(){
ActionListener actionHandler= new ActionListener() {
@Override
public void actionPerformed(final ActionEvent e) {
//some action to get the stored String from Model
//String key =model.getStoredItem();
}
};
view.addActionToSecondButton(actionHandler);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.