I try to use gwt to create a textarea and a counter under it with the length of the characters, but it doesn't counts the backspace and with 1 character it has length 0. Here's my code. What can be the problem?
public class Test implements EntryPoint {
TextArea textArea;
Label counter;
public void onModuleLoad() {
textArea = new TextArea();
counter = new Label("Number of characters: 0");
textArea.addKeyPressHandler(new KeyPressHandler() {
public void onKeyPress(KeyPressEvent event) {
counter.setText("Number of characters: " + textArea.getText().length());
}
});
RootPanel.get("myContent").add(textArea);
RootPanel.get("myContent").add(counter);
}
Perhaps you want to track KeyUp event instead:
textArea.addKeyUpHandler(new KeyUpHandler() {
public void onKeyUp(KeyUpEvent event) {
counter.setText("Number of characters: " + textArea.getText().length());
}
});
I think checked this code should work
public class TextAreaEx implements EntryPoint {
final TextArea textArea = new TextArea();
final Label counter = new Label("Number of characters: 0");
public void onModuleLoad() {
RootPanel.get().add(textArea);
RootPanel.get().add(counter);
addlistener();
}
private void addlistener() {
textArea.addKeyUpHandler(new KeyUpHandler() {
public void onKeyUp(KeyUpEvent keyUpEvent) {
counter.setText(" Number of characters:"+textArea.getText().length());
}
});
textArea.addChangeHandler(new ChangeHandler() {
public void onChange(ChangeEvent changeEvent) {
counter.setText(" Number of characters:"+textArea.getText().length());
}
});
}
}
Sounds like you're counting characters before the key press event completes. Perhaps if you try a KeyUpHandler instead, then the text area will include the newly added character.
Related
I am making a matching card program and I want to make sure the user only selects two card. So I have made Changelisteners and inside those changelisteners I would like to have an integer that would increase when there is a change in the state of the button. I have tried to use int, but it gave me the error where it says to use a final or effectively final. Is there some way that I can use an int inside of the changelistener method.
Here is an example:
card1Button.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
//int increases here
}
});
You have two basic choices to solve the immediate issue
You could...
Make the counter an instance field
public class MyAwesomeCardGame extends ... {
private int counter;
//...
public MyAwesomeCardGame() {
//...
card1Button.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
counter++;
}
});
}
}
You could...
Make the counter a instance of field of the anonymous class
public class MyAwesomeCardGame extends ... {
//...
public MyAwesomeCardGame() {
//...
card1Button.addChangeListener(new ChangeListener() {
private int counter;
public void stateChanged(ChangeEvent e) {
counter++;
}
});
}
}
Alternatively
Depending on what you're doing, you could use two ButtonGroups instead, it would ensure that only one button from each group can be selected at a time
You could change the scope of your variable
public class CardGame {
private int x;
public CardGame() {
card1Button.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
x++;
}
});
}
}
Here is an example that shows how you can do this when using a JCheckBox:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class CheckBoxGroup
{
private Set<GroupButtonModel> models = new HashSet<GroupButtonModel>();
private int groupSize;
public CheckBoxGroup(int groupSize)
{
this.groupSize = groupSize;
}
public void register(JCheckBox checkBox)
{
ButtonModel groupModel = new GroupButtonModel();
groupModel.setSelected ( checkBox.getModel().isSelected() );
checkBox.setModel( groupModel );
}
private class GroupButtonModel extends JToggleButton.ToggleButtonModel
{
#Override
public void setSelected(boolean selected)
{
if (!selected)
{
models.remove( this );
super.setSelected( selected );
return;
}
// Check number of currently selected check boxes
if (models.size() == groupSize)
{
System.out.println("Only " + groupSize + " items can be selected");
}
else
{
models.add( this );
super.setSelected( selected );
}
}
}
private static void createAndShowGUI()
{
JPanel panel = new JPanel();
CheckBoxGroup group = new CheckBoxGroup(3);
for (int i = 0; i < 10; i++)
{
JCheckBox checkBox = new JCheckBox( String.valueOf(i) );
panel.add( checkBox );
group.register( checkBox );
}
JFrame frame = new JFrame("Check Box Group");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( panel );
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
It adds a custom model to the component to check the number of currently selected components before allowing you to select another component.
It will also work for a JToggleButton. Just change the register(...) method to register toggle buttons.
Hello I am trying to display one String ("Character Count: ") and one dynamic character count on the bottom of JTextArea.
When I run this code below, there is a panel that opens up without characterCountTitle. Only when I start typing, characterCountTitle displays and the number is correctly dynamic.
My goal is to show characterCountTitle (string + character count) as soon as the panel is open to users.
private void initComponents() {
this.notePanel.getNoteDocument().addDocumentListener(new DocumentListener() {
TitledBorder characterCountTitle;
Border emptyBorder;
public void insertUpdate(DocumentEvent e) {
displayEditInfo(e);
}
public void removeUpdate(DocumentEvent e) {
displayEditInfo(e);
}
public void changedUpdate(DocumentEvent e) {
displayEditInfo(e);
}
private void displayEditInfo(DocumentEvent e) {
Document document = e.getDocument();
emptyBorder = BorderFactory.createEmptyBorder();
//displays a string of "Character Count: " and another string of dynamic character count
characterCountTitle = BorderFactory.createTitledBorder(emptyBorder, "Character Count: " + document.getLength());
characterCountTitle.setTitlePosition(TitledBorder.BOTTOM);
panel.setBorder(characterCountTitle);
}
});
this.panel.add(notePanel, BorderLayout.CENTER);
this.panel.add(navigation.buildPanel(), BorderLayout.SOUTH);
}
Due to this issue, I was trying to create two titles; one for string(outside of addDocumentListener) and one for character count (inside displayEditInfo method), but it messes up the variable scope.
I'd greatly appreciate your input!
You may simply create and add your border outside of the DocumentListener, and just change the title text on document events :
private void initComponents() {
Border emptyBorder = BorderFactory.createEmptyBorder();
final TitledBorder characterCountTitle = BorderFactory.createTitledBorder(emptyBorder, "Character Count:");
characterCountTitle.setTitlePosition(TitledBorder.BOTTOM);
panel.setBorder(characterCountTitle);
this.notePanel.getNoteDocument().addDocumentListener(new DocumentListener() {
public void insertUpdate(DocumentEvent e) {
displayEditInfo(e);
}
public void removeUpdate(DocumentEvent e) {
displayEditInfo(e);
}
public void changedUpdate(DocumentEvent e) {
displayEditInfo(e);
}
private void displayEditInfo(DocumentEvent e) {
Document document = e.getDocument();
//displays a string and dynamic character count
characterCountTitle.setTitle("Character Count: " + document.getLength());
panel.repaint();
}
});
this.panel.add(notePanel, BorderLayout.CENTER);
this.panel.add(navigation.buildPanel(), BorderLayout.SOUTH);
}
In java , i am trying to make simple currency converter, but for that i need a text field which can restrict input to numbers only and more importantly double numbers. I tried using JFormatedTextField but it only format the input after you have done your input and click elsewhere but i need to restrict TextField to consume() each invalid character while doing input.
Possible Attempts:
Using JFormatedTextField:
JFormatedTextField textField = new JFormatedTextField(new DoubleFormat());
textField.setBounds(190, 49, 146, 33);
frame.getContentPane().add(textField);
textField.setColumns(10);
Using KeyTyped Event:
char c = arg0.getKeyChar();
if(!(Character.isDigit(c) || c == KeyEvent.VK_BACK_SPACE || c== KeyEvent.VK_DELETE)){
arg0.consume();
}
Using KeyTyped Event with regex:
if(!((textField.getText().toString+arg0.getKeyChar()).matches("[0-9]*(.[0-9]*)?"))){
arg0.consume();
}
Second and third attempt were close but then second attempt failed on point values and third attempt always read first character on textField no matter what it is, So any suggestions ? i am not very fond of JAVA GUI so kindly be patient.
If you know how many places before and after decimal point you want, you can also use MaskFormatter. For example:
JFormattedTextField field = new JFormattedTextField(getMaskFormatter("######.##"));
(...)
private MaskFormatter getMaskFormatter(String format) {
MaskFormatter mask = null;
try {
mask = new MaskFormatter(format);
mask.setPlaceholderCharacter('0');
}catch (ParseException ex) {
ex.printStackTrace();
}
return mask;
}
However it will chenge a look of JTextField, so it will be always visible 000000.00 in it.
EDIT
Another way, not too elegant, but in my opinion working. Try with DecumentListener, maybe it will suit your needs:
field = new JFormattedTextField();
field.getDocument().addDocumentListener(new DocumentListener() {
#Override
public void insertUpdate(DocumentEvent e) {
Runnable format = new Runnable() {
#Override
public void run() {
String text = field.getText();
if(!text.matches("\\d*(\\.\\d{0,2})?")){
field.setText(text.substring(0,text.length()-1));
}
}
};
SwingUtilities.invokeLater(format);
}
#Override
public void removeUpdate(DocumentEvent e) {
}
#Override
public void changedUpdate(DocumentEvent e) {
}
});
I used regex: \\d*(\\.\\d{0,2})? because two decimal places is enough for currency.
You would need to use a DocumentFilter. Read the section from the Swing tutorial on Implementing a DocumentFilter for an example to get you started.
Your implementation will be more complex because you will need to take the text already in the Document and then insert the new text in the appropriate location in the String and then invoke Double.parseDouble(...) on the String to make sure it is a valid double value.
If the validation succeeds then you continue with the insertion otherwise you can generate beep.
You can add a key listener to the text field and implement the keyReleased() method to determine if they value in the text field is a double after every key stroke by the user.
public class CurrencyJTF extends JFrame {
JButton jButton = new JButton("Unfocus");
final JFormattedTextField textField = new JFormattedTextField(new DecimalFormat());
double lastDouble = 0.0;
public CurrencyJTF() throws HeadlessException {
textField.setColumns(20);
textField.setText(lastDouble + "");
this.setLayout(new FlowLayout());
this.add(textField);
this.add(jButton);
textField.addKeyListener(new KeyAdapter() {
#Override
public void keyReleased(KeyEvent e) {
handleKeyReleased();
}
});
}
private void handleKeyReleased() {
String text = textField.getText();
if (text.isEmpty()) return;
try {
lastDouble = Double.parseDouble(text);
} catch (NumberFormatException ex) {
textField.setText(lastDouble + ""); // or set to other values you want
}
}
public static void main(String[] args) {
JFrame frame = new CurrencyJTF();
frame.setVisible(true);
frame.pack();
}
}
You can write your own KeyListener something like that:
public class DoubleNumbersKeyListener implements KeyListener {
final HashSet<Character> valid_keys = new HashSet<>();
final ArrayList<Character> sequence = new ArrayList<>();
public DoubleNumbersKeyListener() {
valid_keys.add('.');
valid_keys.add('0');
valid_keys.add('1');
valid_keys.add('2');
valid_keys.add('3');
valid_keys.add('4');
valid_keys.add('5');
valid_keys.add('6');
valid_keys.add('7');
valid_keys.add('8');
valid_keys.add('9');
valid_keys.add((char) KeyEvent.VK_BACK_SPACE);
valid_keys.add((char) KeyEvent.VK_DELETE);
}
#Override
public void keyTyped(KeyEvent event) {
char c = event.getKeyChar();
if (!valid_keys.contains(c)) {
event.consume();
} else {
if (c == KeyEvent.VK_DELETE || c == KeyEvent.VK_BACK_SPACE) {
if (!sequence.isEmpty()) {
char last = sequence.remove(sequence.size() - 1);
if (last == '.') {
valid_keys.add(last);
}
}
} else {
sequence.add(c);
if (c == '.') {
valid_keys.remove(c);
}
}
}
}
#Override
public void keyPressed(KeyEvent e) {
}
#Override
public void keyReleased(KeyEvent e) {
}
}
I wanted to develop a console-like interface, similar to IDLE. That involved determining how to prevent a certain part of the text in a JTextField from being edited. For example:
>>> help
Where the ">>> " is uneditable. The caret must never move behind a certain position, and the text behind that position cannot be edited in any way.
I looked at NavigationFilter, but it doesn't seem to prevent keyboard driven manipulation of the caret.
This shows how to do it with a NavigationFilter:
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class NavigationFilterPrefixWithBackspace extends NavigationFilter
{
private int prefixLength;
private Action deletePrevious;
public NavigationFilterPrefixWithBackspace(int prefixLength, JTextComponent component)
{
this.prefixLength = prefixLength;
deletePrevious = component.getActionMap().get("delete-previous");
component.getActionMap().put("delete-previous", new BackspaceAction());
component.setCaretPosition(prefixLength);
}
#Override
public void setDot(NavigationFilter.FilterBypass fb, int dot, Position.Bias bias)
{
fb.setDot(Math.max(dot, prefixLength), bias);
}
#Override
public void moveDot(NavigationFilter.FilterBypass fb, int dot, Position.Bias bias)
{
fb.moveDot(Math.max(dot, prefixLength), bias);
}
class BackspaceAction extends AbstractAction
{
#Override
public void actionPerformed(ActionEvent e)
{
JTextComponent component = (JTextComponent)e.getSource();
if (component.getCaretPosition() > prefixLength)
{
deletePrevious.actionPerformed( null );
}
}
}
private static void createAndShowUI()
{
JTextField textField = new JTextField("Prefix_", 20);
textField.setNavigationFilter( new NavigationFilterPrefixWithBackspace(7, textField) );
JFrame frame = new JFrame("Navigation Filter Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(textField);
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
Spent a little while figuring this out, so I thought I would share my solution for anyone else with the same dilemma. I don't know if it's optimal, but it does seem to work.
It prevents the user from using backspace behind the postion n. It also moves the caret back to n for any other events, such as (illegally) changing the caret position with the arrow keys or mouse. Finally, it resets the text and caret position after a entry is processed.
EDIT: While I'm leaving this answer here for posterity, see the accepted answer for the best way to solve this problem.
JTextField in = new JTextField();
final String protectMe = ">>> "; //protect this text
final int n = protectMe.length();
in.setText(protectMe);
in.setCaretPosition(n);
in.addCaretListener(new CaretListener()
{
#Override
public void caretUpdate(CaretEvent e)
{
if (e.getDot() < n)
{
if (!(in.getText().length() < n))
in.getCaret().setDot(n);
}
}
});
in.addKeyListener(new KeyListener()
{
#Override
public void keyPressed(KeyEvent arg0)
{
if (in.getCaret().getDot() <= n)
{
in.setText(protectMe + in.getText().substring(n));
arg0.consume();
}
}
#Override
public void keyReleased(KeyEvent arg0){}
#Override
public void keyTyped(KeyEvent arg0){}
});
in.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
String input = in.getText().substring(n).trim();
//do something
in.setText(protectMe);
in.setCaretPosition(n);
}
});
As usual, let me know if there's anything I missed!
I have a simple JavaFX application which has a TextArea. I can update the content of the textArea with the code below inside the start() method:
new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 2000; i++) {
Platform.runLater(new Runnable() {
public void run() {
txtarea.appendText("text\n");
}
});
}
}
}).start();
The code just write the text string into the TextArea 2000 times. I want to update this textArea from a function which is implemented outside of the start() method.
public void appendText(String p){
txtarea.appendText(p);
}
This function can be called from arbitrary programs which use the JavaFX application to update the TextArea. How can I do this inside the appendText function?
You could give the class which needs to write to the javafx.scene.control.TextArea an reference to your class which holds the public void appendText(String p) method and then just call it. I would suggest you also pass an indication from which class the method was called, e.g.:
public class MainClass implements Initializable {
#FXML
private TextArea txtLoggingWindow;
[...more code here...]
public void appendText(String string, String string2) {
txtLoggingWindow.appendText("[" + string + "] - " + string2 + "\n");
}
}
public class SecondClass {
private MainClass main;
public SecondClass(MainClass mClass) {
this.main = mClass;
}
public void callMainAndWriteToArea() {
this.main.appendText(this.getClass().getCanonicalName(), "This Text Goes To TextArea");
}
}