Libgdx native TextInput limit text Length - java

I am writing a game using Libgdx. I used what was suggested here to handle virtual keyboard when user enters player names. This actually works like a charm. However if user enters more than 8 characters in a name field, it breaks the UI design of my game. So I want to prevent user from entering more than 8 characters.
TextField has a setMaxLength method as defined here. If I set this value to 8, no matter what user enters, the first 8 characters are put in my text field. But this is kind of annoying and misleading because user can still enter, let's say 20 characters without having a clue that the only first 8 will be used.
So, my question is whether there is a mechanism to stop user entering more than 8 characters even if I use "native" way of handling TextField inputs.
Thanks in advance.

I tested this just before typing to you, so I know it works. The below code will make it so your textfield will only allow for you to enter in 8 characters into the TextField widget. Be careful though, some characters are larger (in length) than others ( chars like - are short ).
textField.setMaxLength(8); // Maximum chars will be 8
textField.setAlignment(1); // If you wanted to center the text
// (1 = Center, 2 = Right Align)
On another note, if you are adding the TextField to a table, you can change the visible width of the widget.
table.add(textField).width(50); // I believe this is in pixels
You didn't really give me much to go off from (no code), so I hope this answer helps you out.
Best of luck with your game!

Related

Java - Capture value from text box, store in array and return max value in an output field

So I have been asked to help my friend with a small java project, I have a decent enough idea about programming from using VBA but not enough to help him, Im not too clued up on Java
Basically, he has a form with a text box, 2 buttons and an output field
The text box is for entering a value - when he clicks one button, it has to store the value within the text box to an array - and the other button is to output the max value within the array - I would appreciate any help with this if possible
Many thanks,
Sean
To me it sounds like homework. Since you didn't bother posting any code you have written yourself I will outline what I would do. You could use the getText() from a JTextField and store the output in a String. Then you could split that string by white space, provided it only contains integers. After this I would call the Integer.parseInt() method while iterating over the array. Be careful as the input in the box must be only integers, otherwise your program will throw exceptions.

JTextArea indentation

I'm having a problem with JTextArea (not a problem though).
I want to create my own college program, one of the function is to write down and save a list of homework each day. It's text area having the main function here, which is to type everything my lecturer said. But text area will act like Windows original notepad, which is not keeping the indentation. So my questions are:
How do I have a function where I press the SHIFT+TAB key it will decrease indentation by one.
If number 1 is possible, then how do my program have behaviour when I press ENTER key, it will have a new line with previous indentation? Just like NetBeans and other IDE.
Edited: I'm a beginner in Java, in truth, I'm making this program while studying Java in my college.
How do I have a function where I press the SHIFT+TAB key it will decrease indentation by one.
You need to use key bindings to listen for the user typing Shift+Tab.
If number 1 is possible, then how do my program have behavior when I press ENTER key, it will have a new line with previous indentation? Just like NetBeans and other IDE.
Use a similar piece of logic to capture the Enter key presses. Then, check the previous line in the editor and count the number of tab characters preceding the text. Output a similar number at the start of the new line.
you could use Javascript/jquery for indenting by inserting empty space of specific line. while pressing your hot key combination call function to insert five spaces on your cursor.

Manual keypads shown to user when an editText is highlighted

I have a number of editText's in my view and I want to be able to make some adjustments to the keypad that points up for the user when they highlight the EditText.
In particular
- Making a capitals only keypad?
- Removing the auto-correct feature?
- Adding non-english characters to the first screen of the keypad e.g. "ñ"
Is there a simple approach for changing this keypad?
In general, you don't have that much control of the on-screen keyboard that is displayed to the users. However, turning off autocorrect and making all characters capitals is a possibility, you simply need to set the android:inputType attribute in your XML. Here are the values that you can use (you can also OR | them). It would look like android:inputType="textCapCharacters|textNoSuggestions".
I wouldn't worry about the Ñ or other weird characters. Most of those are easy to get by just long pressing on a key (in this case, the N).

Change color of JButton when selected, & Set mask on JFormattedTextField?

I'm writing my 1st Java program (in Netbeans) and I'm lost. I have 2 questions at the moment, if anyone is kind enough to help me.
Here's what the program is supposed to do:
take 1 of 4 "status" options, plus a 5 digit number (both of these items are entered by a user via a touch-screen monitor) and then email this info to someone with the subject line of: "Item #[5 digit number from JFormattedTextField] is currently [1 of 4 possible status options].
Email command would command after user clicks "enter" button, and then user clicks "OK" on a pop-up which asks user to confirm message about to be emailed. As far as my 3rd question, it's about the e-mailing part, and I figured that would be a another thread after I get this button & text field stuff ironed out.
Here's a picture of the touch screen UI I have so far:
(can't post images as a rookie, go to krisbunda.com/gui.png for this image)
Question #1:
the 4 status options (4 JButtons) are wrapped inside of a JPanel. I want the most recent button to have been pushed in the "statusPanel" JPanel to change the background to blue and the button text to white.
Can I put a mouselistener on the button's parent JPanel to listen for click events on the children (the 4 status JButtons), and then whichever button was last clicked, it will turn blue w/ white text? Please point me in the right direction.
Question #2:
I have a JFormattedTextField named "display" that shows the numbers as they're clicked, which are appended from a StringBuffer named "current". I want the text field to only accept a total of 5 numbers.
When I tried putting a mask of "#####" on the field, it would only chime a warning beep when I pushed the number pad's buttons. Currently I've chosen "Category: number" and "Format: custom" and then typed "#####" in the "Format:" field. This allows me to click number buttons and see their text displayed, but it doesn't stop me from typing more than 5 characters.
I'm doing this through the "Properties> FormatterFactory" dialog box. A screen shot is shown below:
(go to krisbunda.com/text-formatterFactory.png to view this image)
And here's the code I have so far:
(my post was too long with this code, so go to: krisbunda.com/java-sampleCode.txt to view)
Thanks in advance for any help!
Your code looks fine, and you already have fields set up to hold references to all your buttons, so now you just need to write the code inside the status setting buttons and then make them call a subroutine with the new status. This subroutine should then reset all the buttons to their default color and then set the special selected color on the button that corresponds to the new or existing status.
Edit: adding code here in response to your comment...
Firstly, never use == with Strings. You MUST use equals() otherwise when you get two Strings that are identical, but are different objects, they will not be the same and your comparisons will fail.
There are much better ways of coding this up, including using enums etc. but this should work for you:
// Reset all the buttons
outsideNotReadyButton.setBackground(...);
loadedButton.setBackground(...);
outsideReadyButton.setBackground(...);
shippedButton.setBackground(...);
// Now set the one of the button's colors conditionally
String status = ...
if(status.equals("SHIPPED")) {shippedButton.setBackground(Color.BLUE);}
else if(status.equals("LOADED")) {loadedButton.setBackground(Color.BLUE);}
// ...and so on
An ActionListener is the more common approach to buttons, as discussed in How to Use Buttons, etc. A FocusListener, also used in this example, is one way to change a button's appearance in the way you describe.
An sscce showing just your JFormattedTextField problem will be more helpful. Several such examples may be found in the article How to Use Formatted Text Fields.

Swing: JLabel which shortens too long text in the middle

Normally a left-orientated JLabel shortens text at the right by adding periods, e.g. Hello wo.... Anyone knows an JComponent (TableCellRenderer is not sufficient) which can shorten text in the middle (Hel...rld), e.g. useful for displaying file names?
The LeftDotRenderer displays the dots on the left. I know its not what you asked for, but you might be able to use the concepts in the code to create your own.

Categories