TextField selectAll works half the time - java

I'm using Vaadin and have a set of textfields in a form. The textfields has focusListeners that triggers a method that focuses all the text in the texfield if there is any.
My problem though is that the auto selection works literally half the time. If I paste some text in a textfield, clicks outside the textfield and then clicks inside it the text will be selected. However, if I click outside again and then in the textfield the text will for a fraction of a second be selected to then only having the input marker where ever it was I clicked in the text.
Here's the code:
class FormTextField extends FormLayout {
private static final long serialVersionUID = -2738069810605965508L;
String caption;
final STextField textField = Cf.formTextField(caption, "", 22);
public FormTextField(String textField) {
addStyleName("panelform");
setWidth(formWidth, UNITS_EM);
this.textField.setCaption(textField);
this.textField.setImmediate(true);
this.textField.addListener(new FieldEvents.FocusListener() {
#Override
public void focus(FocusEvent event) {
textFieldSelectAll();
}
});
addComponent(this.textField);
}
private void textFieldSelectAll() {
this.textField.selectAll();
}
public STextField getTextField() {
return textField;
}
}
}
I'm very interested to know if someone of you are familiar with this problem and have been able to sort it out?
If you want any more information from me then please ask!

I think you need to declare your FormTextField/STextField immediate too.
Hope this helps.

Related

Calling a JFrame method from another JFrame on Button Click

I searched on stack overflow for the similar answers for my question, but neither of them helped me.
So my problem is the following:
I have a main JFrame called Main_Window, on which I have a JTable and a JButton. After clicking the Button another JFrame (Update_Window) opens from Which I can update the table. The Update_Window JFrame has two TextFields and a SUBMITButton.
Briefly, I want to update my JTable in the Main_Window from the Update_Window JFrame. After I type something in the TextFields and Submit with the Button, the data should appear in the Main_Window's JTable, but it is not working.
This is my Main_Window JFrame:
private void updateBtnActionPerformed(java.awt.event.ActionEvent evt) {
Update_Window newWindow = new Update_Window();
newWindow.setVisible(true);
newWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void putDataIntoTable(Integer data, int row, int col) {
jTable1.setValueAt(data,row,col);
}
This is my Update_Window JFrame:
private void submitBtnActionPerformed(java.awt.event.ActionEvent evt) {
quantity = Integer.parseInt(quantityTextField.getText());
price = Integer.parseInt(priceTextField.getText());
Main_Window mw = new Main_Window();
mw.putDataIntoTable(price,3,2);
}
I think my problem is here Main_Window mw = new Main_Window();, because this creates a new Instance and it doesn't add the data to the correct window, or something like that.
Yes, you are right. The line Main_Window mw = new Main_Window(); is definitely wrong.
Better solution is:
public class UpdateWindow extends JFrame {
private final MainWindow mainWindow;
public UpdateWindow(MainWindow mainWin) {
mainWindow = mainWin;
}
private void submitBtnActionPerformed(java.awt.event.ActionEvent evt) {
quantity = Integer.parseInt(quantityTextField.getText());
price = Integer.parseInt(priceTextField.getText());
mainWindow.putDataIntoTable(price,3,2);
}
}
Also you need to correct the call of constructor for UpdateWindow
private void updateBtnActionPerformed(java.awt.event.ActionEvent evt) {
UpdateWindow newWindow = new UpdateWindow(this);
newWindow.setVisible(true);
newWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
Please note: I've corrected your class names as it proposed by Java naming convention. Main_Window -> MainWindow, Update_Window -> UpdateWindow.
When my suggestion don't solve your problems, please provide a [mcve] so we can better identify your problems.

Toggle TextField required/not required when checkbox checked

I have a text field which I want to toggle as a required field or not based on if a checkbox is checked. I can't get the ValueChangeEventListener to play nice.
This is what I have so far:
private TextField myTextField = new TextField("Name");
myTextField.setRequired(true);
private CheckBox myCheckBox;
myCheckBox = createCheckBox();
private CheckBox createCheckbox() {
CheckBox checkBox = new CheckBox("My checkbox");
checkBox.setImmediate(true);
checkBox.addValueChangeListener(new ValueChangeListener() {
#Override
public void valueChange(ValueChangeEvent event) {
toggleRequired();
}
});
return checkBox;
}
private void toggleRequired() {
if (myCheckBox.getValue() != true) {
myTextField.setRequired(false);
} else {
myTextField.setRequired(true)
}
}
The valueChangeListener gets fired way too many times going from true to false back to true. Not sure what I'm doing wrong here and would really appreciate some help.
Thank you
Ok I figured it out! It was a very easy solution all I had to change was
checkBox.addValueChangeListener(new CheckBox.ValueChangeListener() {});
I was referencing some the following properties class instead:
properties.ValueChangeListener();

How to disable and re-enable label

In the below code, I have a label named card with a mouse click event. I only want the click event to implement once. Meaning it will implement the first time I click the label, but not the following times. How do I do this? I imagine I must disable its Listener.
private void cardMouseClicked(java.awt.event.MouseEvent evt) {
// displays backside of each flashcards when label (flashcard) is clicked
i++;
card.setText(cardB[i]);
}
I think we all would do the same.
It's really simple. Just declare a boolean then change its status when you click the first time.
boolean labelClicked = false;
private void cardMouseClicked(java.awt.event.MouseEvent evt) {
// displays backside of each flashcards when label (flashcard) is clicked
if(!labelClicked){
i++;
card.setText(cardB[i]);
labelClicked=true;
}
else{
//doNothing
}
}

How do I get the String in a JTextField from an action of JButton?

For a while I have been trying to figure this out, searching online, finally decided to join just to figure this out. So my dilemma is this.
addActionListner(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
//some code
}
});
What goes in some code that could actually pull from a JTextField (and for arguments sake let us call it myText)? Like how could I, if possible, call the ActionListner? so has when the button is clicked it actually performs the actionPeformed of the ActionListener in myText?
Add the action listener to the button and then retrieve myText's contents in the button's action listener. Hope it helps.
Assuming your button is named something like "myButton":
String someString = "";
myButton.addActionListner(new ActionListener(){
public void actionPerformed(ActionEvent e) {
//this will give you myText's contents. Do what you will with them.
someString = myText.getText();
}
});
Now you should be able to do whatever you want with the contents.

Java Swing default focus on frame

I am learning java and Swing right now and trying to develop simple programms for education purposes.
So here is the question.
I have gridlayout and fields on my frame with default text
accNumberField = new JTextField("0", 10);
accNumberField.addFocusListener(new FocusListener() {
int focusCounter = 0;
#Override
public void focusGained(FocusEvent arg0) {
// TODO Auto-generated method stub
if (focusCounter > 0)
accNumberField.setText("");
focusCounter++;
}
What I want is that when user click on field for the first time the default text is disappered. So I add focus listener and used accNumberField.setText(""); in focusGained method.
But the problem is that for default first field in my frame getting focus right in time of frame creation. And default text is disappearing from the begining. I used counter as you can see. But that's not what I wanted.
I want that no field would get focus in time of creation and every field would be able to get focus from the time when user would click on one of them.
Sorry if I spelled something wrong. English is not my native language.
Found a thread having a code example of your desired functionality, Java JTextField with input hint. Precisely, you need to provide your own implementation of JTextField which will be holding the "default-text" in a field, specially created for that.
For your second question, you can set the focus to some button or frame itself.
Is there any reason that you use focusListener()? why not use mouseListener() as follow?
accNumberField.addMouseListener(new MouseAdapter()
{
#Override
public void mouseReleased(MouseEvent e)
{
accNumberField.setText("");
}
});
if you want to clear the text for the first click, you can simply use a boolean:
//outside constructor
private boolean isTextCleared = false;
//in constructor
accNumberField.addMouseListener(new MouseAdapter()
{
#Override
public void mouseReleased(MouseEvent e)
{
if (!isTextCleared)
{
accNumberField.setText("");
isTextCleared = true;
}
}
});

Categories