I have a problem with Java Swing text input. I have a method inputData() in class A and when I call it, the method should wait while user fill TextField input in class B and press ENTER. Finally, the method inputData() should have the text that user wrote. How could I solve it?
class A {
B b = new B();
public A() {
inputData();
}
public char[] inputData() {
// there I would like to get text
// from TextField from class B
}
}
//-------------------------------
class B extends JFrame{
private JTexField input;
public B() {
}
private void inputKeyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) { // pressed ENTER
input.getText()
input.setText(null);
}
}
}
You may not actually want a JTextField. It sounds like you're waiting for a line of input from the user, which should really be a JOptionPane. How to do this is described here:
http://download.oracle.com/javase/tutorial/uiswing/components/dialog.html#input
Basically, JOptionPane.showInputDialog() will cause a window to pop up that contains a text field and OK/Cancel buttons, and if you press enter it will take your input. This eliminates the need for another class.
You'd put it in your inputData() method:
inputData()
{
String input = JOptionPane.showInputDialog(...);
//input is whatever the user inputted
}
If that's not what you're looking for and you want a text field that stays open, perhaps what you really want is a "Submit" button next to your JTextField that allows the user to decide when to submit the text. In that case, you could have:
class B extends JFrame
{
private A myA;
private JTextField input;
private JButton submitButton;
public B()
{
submitButton.addActionListener(new SubmitListener());
}
private class SubmitListener
{
//this method is called every time the submitButton is clicked
public void actionPerformed(ActionEvent ae)
{
myA.sendInput(inputField.getText());
//A will need a method sendInput(String)
}
}
}
TextField? Since it's a Swing project, I hope you mean a JTextField, right? And don't add a KeyListener to it, but rather an ActionListener since these are triggered when the user presses Enter. One way to solve your problem is to give the GUI class (here named B) a public method that will allow outside classes to add an ActionListener to the JTextField. Perhaps you can call it addActionListenerToInput(ActionListener listener). Then Class A can add the listener to B, and the actionPerformed code will be called when enter is pressed.
e.g.,
class A {
B b = new B();
public A() {
//inputData();
b.addActionListenerToInput(new ActionListener() {
public void actionPerformed(ActionEvent e) {
inputActionPerformed(e);
}
});
}
private void inputActionPerformed(ActionEvent e) {
JTextField input = (JTextField) e.getSource();
String text = input.getText();
input.setText("");
// do what you want with the text String here
}
}
//-------------------------------
class B extends JFrame{
private JTextField input;
public B() {
}
public void addActionListenerToInput(ActionListener listener) {
input.addActionListener(listener);
}
}
Related
How to place Action Listener directly in definition of class that extends Button ?
If object of class Button is created then we could simply use anonumous inner class :
b = new Button("Click me");
b.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("stringToPrint");
}
}
);
how to do the same in below :
class CustomizedButton extends Button{
String customClass;
Button(String stringToPrint){
super(customClass); //customClass is also button name
this customString = stringToPrint;
}
/*this.addActionListener( //don't work this way
new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println(customClass);//use outer(?) field
}
}
);*/
}
I need to create 20 almost identical but slightly different buttons, so anonymous inner is too long
You could declare a private nested class, like so:
public class CustomizedButton extends Button{
String customClass;
CustomizedButton(String stringToPrint){
super(customClass); //customClass is also button name
this.customString = stringToPrint;
addActionListener(new MyListener());
}
private class MyListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
// TODO: listener code here
}
}
}
But it's not much different from using an anonymous inner class or lambda:
public class CustomizedButton extends Button{
String customClass;
CustomizedButton(String stringToPrint){
super(customClass); //customClass is also button name
this.customString = stringToPrint;
addActionListener(e -> myListenerCode(e));
}
private void myListenerCode(ActionEvent e) {
// TODO: listener code here
}
}
Having said this, other issues come to mind:
Usually it's best to favor composition over inheritance. I would bet that what you really want is some sort of factory method that creates your button complete with listener
Why use AWT components such as the java.awt.Button class when it is 20+ yrs out of date? Why not Swing JButtons instead?
If you were using Swing JButtons, best would be to create a custom Action rather than extend JButton. Actions can hold and change many button properties, including a listener, the displayed text, icons, the tool tip text (displayed on hover)....
For that matter, you should favor JavaFX if this is a new project, since this is the current best-supported Java GUI library.
For example an AbstractAction class could look something like:
public class CustomizedAction extends AbstractAction{
String text;
CustomizedAction(String text, int mnemonic){
super(text); //text is also button name
this.text = text;
putValue(MNEMONIC_KEY, mnemonic); // for alt-key short cut if desired
}
#Override
public void actionPerformed(ActionEvent e) {
String currentName = getValue(NAME); // same value as the text field
System.out.println(currentName);
// TODO: more listener code here
}
}
and could be used like so:
JButton button = new JButton(new CustomizedAction("Foo", KeyEvent.VK_F));
How to check from a class ModalDialog extends JDialog implements ActionListener if actionPerformed(ActionEvent e) method ocurred in another class (Connect extends JFrame implements ActionListener)? And one step further, how to check which of two buttons that I have in ModalDialog fired ActionPerformed method? (I know about event.getSource, but I need to check it from another class).
public ModalDialog()
{
btn8 = new Button("human");
btn8.setMaximumSize(new Dimension(60,40));
btn8.addActionListener(this);
btn9 = new Button("robot");
btn9.setMaximumSize(new Dimension(60,40));
btn9.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
}
class Connect extends JFrame implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
ModalDialog md = new ModalDialog();
if(md.ActionPerformed(e)....)...something like that...
}
}
How to check from a class ModalDialog extends JDialog implements ActionListener if actionPerformed(ActionEvent e)
This is a basic problem of how to return information from one class to another. The simple answer is to provide a getter method, which returns the selected value.
Start by defining the value to be returned, here I used a enum, as it clearly defines what could be returned
public enum Option {
HUMAN, ROBOT;
}
Update your ModalDialog to provide a getter to return the selected value
public class ModalDialog extends JDialog implements ActionListener {
private Option selection;
public ModalDialog() {
setModal(true);
Button btn8 = new Button("human");
btn8.addActionListener(this);
Button btn9 = new Button("robot");
btn9.addActionListener(this);
setLayout(new GridBagLayout());
add(btn8);
add(btn9);
pack();
}
public Option getSelection() {
return selection;
}
public void actionPerformed(ActionEvent e) {
//...
}
}
When the dialog is closed, the caller can now call getSelection to get the selected value (or null if the user closed the dialog via the [X] button
And one step further, how to check which of two buttons that I have in ModalDialog fired ActionPerformed method?
This is not an uncommon problem, and there a number of ways you might implement it. Since you've already implemented ActionListener at the class level, you could just make use of the actionCommand support available in buttons, which defaults to the text of the button
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
switch (cmd) {
case "human":
selection = Option.HUMAN;
break;
case "robot":
selection = Option.ROBOT;
break;
}
setVisible(false);
}
So now, when the dialog is closed, you can just request the selected value...
ModalDialog dialog = new ModalDialog();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
Option selection = dialog.getSelection();
System.out.println("You choose " + selection);
I'm very new to coding(2 months) and i attempted to make Tic-Tac-Toe in java. I'm in a little over my head but i managed to create it using swing. My main problem is in the button1 class. I was going to use the getText() method but ended up not needing it or so i thought. I tried deleting it but as it turns out my tictactoe buttons don't switch letters without it. The compiler told me it overrides AbstractButton's getText() method but i don't see why that should matter since i never actually used it i thought. I'm thinking it's maybe a scope issue handled by it being overwritten somehow but i'm not sure. I was trying to use the text variable to update the button with setText() and that doesn't seem to work like i thought it should. I also don't understand why the 3 by 3 gridlayout seems to work properly most of the time but sometimes the number of buttons added is wrong.
So in summation the program works(mostly) but i'm not fully understanding how the button1 class is working.
TicTacToe.java
import java.awt.*;
import java.util.*;
import javax.swing.*;
public class TicTacToe extends JFrame {
public static void main(String[] args) {
JFrame window = new JFrame("Tic-Tac-Toe");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
window.setSize(600, 600);
window.setLayout(new GridLayout(3, 3));
ArrayList<button1> buttonArrayList = new ArrayList<>(9);
for (int i = 0; i < 9; i++) {
button1 newbutton = new button1();
buttonArrayList.add(newbutton);
window.add(buttonArrayList.get(i));
}
}
}
button1.java
import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
public class button1 extends JButton {
int value = 0;
String text = "";
public button1() {
class ButtonAction extends AbstractAction {
public ButtonAction() {}
#Override
public void actionPerformed(ActionEvent Switcher) {
System.out.println(text + " " + value);
value++;//value is a relic from earlier attempts that i just felt like keeping.
if (text.equals("O")) {
text = "X";
} else if (text.equals("X")) {
text = "";
} else if (text.equals("")) {
text = "O";
}
}
}
this.setAction(new ButtonAction());
this.setText(text);
this.setFont(new Font("Arial",Font.PLAIN,120));
}
public String getText()// <----culprit
{
return text;
}
}
A JButton class has a methods defined for it, including setText() (which will set the displayed text on the button) and getText() (which will return the current text that is displayed on the button).
You created a class button1 (note: classes should start with Capital Letters).
You added an Action to the button1 class, which means that when the action is activated, something happens. Note that in that actionPerformed method, you should call setText(text) to update the displayed value.
You have also defined a getText() method that overrides the getText() method defined in JButton. This approach is fine if it is a conscious design decision. As it is, I think you should remove the getText() method from the button1 class, and allow the standard JButton class to handle the update. Right now, you are attempting to keep an instance variable text with the value, but it is possible for that instance variable to not be in alignment with the actual displayed value of the button (consider another class calling .setText() on the button).
EDIT: It is true that this referring to the JButton in the ButtonAction is not available. However, the Action itself contains the button that was pressed.
#Override
public void actionPerformed(ActionEvent e)
{
JButton btn = (JButton)e.getSource();
// if desired, String cur = btn.getText() may be called to find the
// current setting; get and process if needed
btn.setText(WHAT_EVER_TEXT);
}
Unless it is a specific requirement to process the current text, however (allowing selecting an O to an X to a blank), I would implement something to keep track of the current turn. This code is something I was experimenting with, and has good and bad points to it (as it is illustrative):
static class TurnController
{
// whose turn it is; start with X
private Player whoseTurn = Player.X;
// the instance variable
private static final TurnController instance = new TurnController();
private TurnController()
{
}
public static Player currentTurn()
{
return instance.whoseTurn;
}
public static Player nextTurn()
{
switch (instance.whoseTurn) {
case X:
instance.whoseTurn = Player.O;
break;
case O:
instance.whoseTurn = Player.X;
break;
}
return instance.whoseTurn;
}
public static String getMarkerAndAdvance()
{
String marker = currentTurn().toString();
nextTurn();
return marker;
}
enum Player
{
X,
O,
;
}
}
Using this TurnController, the actionPerformed becomes:
#Override
public void actionPerformed(ActionEvent e)
{
JButton btn = (JButton)e.getSource();
btn.setText(TurnController.getMarkerAndAdvance());
}
and the Button1 class may have the String text instance variable removed.
What you have tried is Try to make a Custom Button Class and its EventHandler just by extending AbstractAction namee button1 as we See in Your Question.
You have Override the method actionPerformed(ActionEvent Switcher) which actually belongs to Class AbstractAction by your own definition (What should Performed on Action Event of Every Button).
class ButtonAction extends AbstractAction {
public ButtonAction() {}
#Override
public void actionPerformed(ActionEvent Switcher) { // Your Definition For actionPerformed..
System.out.println(text + " " + value);
value++;//value is a relic from earlier attempts that i just felt like keeping.
if (text.equals("O")) {
text = "X";
} else if (text.equals("X")) {
text = "";
} else if (text.equals("")) {
text = "O";
}
}
}
this.setAction(new ButtonAction()); // add ActionListener to each Button.
this.setText(text); // Setting Text to each Button
this.setFont(new Font("Arial",Font.PLAIN,120)); //add Font to each Button.
}
Now In this Code.
ArrayList buttonArrayList = new ArrayList<>();
for (int i = 0; i < 9; i++) {
button1 newbutton = new button1(); // Creating 9 new buttons.
buttonArrayList.add(newbutton); // add each button into the ArrayList.
window.add(buttonArrayList.get(i)); // each Button to the the AWT Window.
}
Above Code will generate 9 Button and add it to Your AWT Window. each button have actionPerformed() method which contains the overrided Definition.
Now Each button will performed action as per the definition you give to actionPerformed() Method.
Thank You.
I'm writing a pretty big class and don't want to post it here. The question is the following, how do I refer to the button that was pressed in the constructor of a different class? Let's say, I want to disable it after some actions in the listener. If the listener were anonymus or were an inner class of the SomeClass, I would just use the name of the variable like this:
button.setEnabled(false);
But how can I do it when my listener is a separate class? Tried using e.getModifiers().setEnabled(false) and e.getSource().setEnabled(false), didn't work.
public class SomeClass extends JPanel {
private JButton button = new JButton("Button");
public SomeClass() {
button.setActionCommand("button");
button.addActionListener(new ButtonListener());
}
public static void main(String[] args) {
// TODO code application logic here
}
}
class ButtonListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
String src = e.getActionCommand();
if (src.equals("button")) {
//some actions here
//then
}
}
}
Try this ((JButton)e.getSource()).setEnabled(false)
It must work)
e.getSource() return component to which this event refers( docs)
Is it possible to pass a String to an ActionListener? I'm creating a number guessing game, and I need to pass the difficulty selected to the the ActionListener, because it's passed from the first GUI opened. How would I do this as it's passed directly to the Game() constructor?
Have your ActionListener access a variable that contains the result of the guess.
I would do something like:
final JTextArea textArea = new JTextArea();
JButton button =new JButton("press");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
String text = textArea.getText();
if (text.equals("something")) {
doSomething();
} else {
doSomethingElse();
}
}
});
Where does the string come from? You can pass that component to the ActionListener when constructed. For example, your string comes from a JLabel object. Then
class GuessNumberActionListener implements ActionListener {
private JLabel difficulty;
public GuessNumberActionListener(JLabel difficulty) {
this.difficulty = difficulty;
}
// implement other methods
}
Then inside your action listener, you can access/update what you want.
ActionListener is an interface. try extending the functionality of the interface using inheritance. this will do the trick
Sure you can. There are lots of ways but one of them is to pass it in to the the ActionListener's constructor:
public class MyClass implements ActionListener {
private int difficultyLevel;
public MyClass(int difficultyLevel) {
this.difficultyLevel = difficultyLevel;
}
public void actionPerformed(ActionEvent e) {
...//code that reacts to action and does something based on difficultyLevel
}
}
UPDATE:
Looks like the design patterns police are out in full force today. You may want to quickly rewrite your app in MVC before you get shot in the foot :)
http://java.sun.com/products/jfc/tsc/articles/architecture/