How to jtextField focus event in NetBeas - java

I made a landing interface in Java using NetBeans. I want to set when the textfield input format does not meet the requirements, then Red tips occur, how to achieve this? I know it is related to focus event, but I do not know the details

I suggest that use ActionEvent and ActionListener for textfield.
then use textfield.getText() to get String data.
you can now examine the String's content by separate it.
If you wanna check the format is a number or not, just use NumberFormatException.
https://docs.oracle.com/javase/7/docs/api/java/lang/NumberFormatException.html
after that,
you want a red warning tip, use label with color red, and .setVisible(boolean) to control it shows or not.

Related

How to Remove Automatic Selection on Text Field when I Start to run my Java Program

Good Day! Why whenever I start my Java Program, the JtextField is always selected even I am not selecting it. Is there any way I can remove it?
Kindly see the picture. Thanks!
The encircled JTextField is what I am talking about
Note, what you refer to by "selected" is called "focus" in Swing. That said, there's no such thing as "no component has the focus".
The best thing you can do is give the focus to a non-input element, like the "Information" title field.
Assuming that is a JLabel, you'd do something like
JLabel informationHeaderLabel = new JLabel("Information");
// ... layout etc
informationLabel.requestFocusInWindow();
There's a tutorial on the Swing focus subsystem.

How do you append both pre-established text and user responses into a text area in Java?

I will do my best to explain-- I am trying to make a choose-your-own-adventure type game while using a TextField and a TextArea, where what is written in the TextField is appended into the TextArea (this I know how to do via ActionListener).
However, I need to have the TextArea start with a pre-written 'intro', which asks the user at the end if they want to continue or not. Therefore, I need it to be able to scan the user's response ('yes' or 'no') and choose the appropriate selection of pre-written text to follow.
I don't want to overwrite what is already in the TextArea, I want to add to it. I suppose what I'm confused about it how I'm supposed to lay out the entire file so that it functions properly, because the different choices for the adventure span different methods. Having
"String text = textField.getText();" only within the actionPerformed method means I can't use 'text' elsewhere, but moving that line up with my other variables tells me it can't reference the field before it's defined.
I am fairly new to Java and am working on this as a project for a non-programming school course. I've been through many iterations thus far and this is what seems to be my final attempt, as I've remade it repeatedly and don't have much time left. :(
Your questions/comments and my attempts to answer:
I am trying to make a choose-your-own-adventure type game while using a TextField and a TextArea, where what is written in the TextField is appended into the TextArea (this I know how to do via ActionListener).
As per my comment, be sure to create a Swing GUI which would use a JTextField and a JTextArea. You would then add your java.awt.event.ActionListener to the JTextArea, and the ActionListener would respond whenever the user pressed <ENTER> within the JTextField.
However, I need to have the TextArea start with a pre-written 'intro', which asks the user at the end if they want to continue or not. Therefore, I need it to be able to scan the user's response ('yes' or 'no') and choose the appropriate selection of pre-written text to follow.
This can be done easily, but sounds as if you may be trying to shoe-horn a linear console type program into a GUI. If so, consider reconsidering your program design since what works best for one often doesn't work well for another. If you do re-write, then you should consider redoing most including your program flow, but excepting perhaps the "model" portion of your previous program, the "business logic" that underlies everything.
I don't want to overwrite what is already in the TextArea, I want to add to it. I suppose what I'm confused about it how I'm supposed to lay out the entire file so that it functions properly, because the different choices for the adventure span different methods. Having "String text = textField.getText();" only within the actionPerformed method means I can't use 'text' elsewhere, but moving that line up with my other variables tells me it can't reference the field before it's defined.
Again as per comments a JTextArea has an append(String text) method that will add new text to existing text that is already displayed in your JText Area. So on that note, your ActionListener's actionPerformed method could be very simple and look something like:
public void actionPerformed(ActionEvent e) {
String text = textField.getText();
textArea.append(text):
}
Although you may need to add line feeds, "\n" either before and/or after the text you are going to append.

Java swing - creating text field on button's place

When specific action is performed, I want to replace button with text field. I know that I have to call button1.setVisible(false); but I don't know how to create text field on the exact same place. I am using NetBeans designer, if you can give me a hint, how to add 2 components at same place, and switch between then, something like switching between layers in photoshop, if something like that is possible, would be great. Thanks
For many components in one space, use a CardLayout as see in this short example.

Is there a way to do this with Java textareas?

I have a question relating to java textarea. I know I could probably accomplish a textarea by using swing, but there is a feature in my textarea I need that I don't know if the one in swing can do for me. I need it so that there is boolean H = false, when H = true and someone is typing, the text they type is hightlighted (simply want a color overlayed, not selected), and I need a way to save this highlight and be able to parse the text area for this highlight. I plan on using some Java Canvas to get what I want done, not including the textarea. I know if I wanted I probably could just program the textarea in the canvas, but I feel like this would be really complicated, and if I should use canvas is there any way I can make it so I can still click and drag to select areas of text?

Returning a JTextField to editing after a warning dialog

In my application I have a JTextField that will only allow the user to fill it with input formatted in a certain way. I have a method that checks if input into the field is valid and if not it displays a dialog. After this occurs I would like the focus to return to the text field so that the user can correct there mistake and type in the appropriately formatted input. I tried to do this by having the JTextField call requestFocus() on itself but this seems to only half work. The JTextField displays the flashing cursor but I cannot actually type in any text until I click on the text field again. Is there another method I need to call? I could not find anything else in the documentation.
It's not exactly the solution you asked for, but you might want to look at javax.swing.InputVerifier. This object allows you to verify the input to a field and prevent taking focus away from it if the format isn't right. I don't think it would allow you to put up a dialog explaining the error (though it might - try it) but it would certainly allow you to put a message in a 'message area'.
What you do is set an InputVerifier on the JComponent, have it test the contents for validity; if it isn't right then the JComponent won't release focus (and can write an error message).

Categories