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.
Related
I am trying to edit codes in java eclipse neon but i cant push words using the space button but instead it over rights the next letters. how do i fix this.
For instance if i want to type the word new between the word Object and rowData[][] in the code below.
Object rowData[][] = { { "Row1-Column1"...
The result will be
Object newData[][] = { { "Row1-Column1"...
It does not push the other codes forward. it just over rights the next letters instead.
Try pressing the "Insert" button on your key board, my guess is you hit it by accident, I know it happens to me all the time. If you have a mac, I believe Fn and enter (the one from the numeric pad) keys combined should give same effect as the insert key so press that to reverse it. You may need to restart eclipse after pressing it, I know I sometimes have to do that as well.
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!
I have done a GUI in Java with a JTextArea. It is filled with the content of a file.
When I select words with the mouse on the textarea, a new frame pops up on which I do some operations on the selected words. To do these operations, I need to know the line number of the selected text...
Does someone know how to get the line number?
(I look to some methods on the classes JTextArea and MouseListener, but i dont know how to do that...)
Thanks ;)
Check out the Text Utilities. The getLineAtCaret() method is close to what you need. It uses the offset of the caret to get the line number. In you case you will need to use the start offset of the selected text.
I'm designing a program simulating a vending machine. You know how vending machines have that one large text box that displays whatever messages you need to know...that's what I want to do. So basically, if the user clicks a button and if the item is out of store, expired, they don't have enough credit, whatever, the message should be displayed in this box.
Then, after a second or 2, return to displaying how much money the user put into the machine. I also want to make the box so that, well theres a button next to the text box to click to insert money. When they click that, I want to make the text box editable, they then enter the amount of money they want to enter, then press insert again, and the money is inserted. The text box becomes uneditable again, and displays the credit they have in the machine.
Does anyone have any suggestions as to how to do this?
I also was wondering how I could implement the delay before getting rid of the message and returning to displaying the credit in the machine. Thank you.
As Andrew Thompson says, you would use a JTextField. Set the desired text using the setText() method and you could then use a sleep function (usleep() I think should work for your application) and then set the text again back to the Dollar value.
Edit: didn't see the last half. To enable and disable the textfield entirely, use setEnabled(true/false), to stop it from being editable, use setEditable(true/false)
Also, just thinking, you could get the current date in a timestamp format and then enter a loop where you continually get the timestamp and compare it to the first one. If the desired difference is reached then exit the loop and update the textfield. Have a look at the Java doc for Date ;)
I want to make an auto completion textfield, which replaces the input with the first hit and selects the part which wasn't entered by the keyboard yet. To do so, I'll have to count the chars typed (or deleted). How can I filter if the KeyEvent is a printable Char (and not SHIFT f.e.)?
Look at https://swingx.dev.java.net/
This lib is already have great autocomplete text fields
i prefer to do it with the KeyListener
Well, a KeyListener is the wrong tool for the job.
If you really want to write your own code then you should start with a DocumentListener. Every time text is added or removed from the text field you will be notified. Then you can implement your auto completion code.