Hi I have a problem about getting the text from the buttons in a JPanel. My program will have a JPanel and there are 4 buttons inside it. Each button will have a random integer shown as a text. I want my program to be able to get the key that is pressed from the keyboard, and check if that key is matched with any of the buttons' text (Something like the calculator).
If the key is matched with any buttons in the JPanel, it will print out that key and make that button disabled.
My code is something like:
private void formKeyPressed(java.awt.event.KeyEvent evt) {
Component[] comp = numpanel.getComponents();
for (int i = 0;i<comp.length;i++) {
if (comp[i] instanceof JButton) {
//check if it matches with any button's text
}
}
}
and I get an error when I try to write comp[i].getText() in order to check the key and the button's text. In my understanding, it says that comp[i] is a Component, which doesn't have the method getText(), am I understand it correctly ?
How can i fix it or are they any alternative ways to do this?
it says that comp[i] is a Component, which doesn't have the method getText(), am I understand it correctly ?
Yes.
How can i fix it or are they any alternative ways to do this?
If you know that comp[i] is a JButton, such as within the if statement you have where you checked that it is with instanceof, then you can cast it as a JButton, and use the getText() method.
.... = ((JButton)comp[i]).getText();
Related
I am trying to finish up a GUI calculator in Java, and have run across a problem in which I cannot figure out how to add text onto the end of current text within a textfield that the cursor is currently positioned at.
Here is a picture of the calculator.
I have completed the operation buttons on the left and right side columns, and now am trying to complete the action listeners for the numbers buttons. I want to get it so that when my cursor was in the left textfield and I click on a number button, it adds that number into the textfield, and the same for the right textfield. I've tried using focus functions but couldn't figure out a way to add the button input into more whichever textfield my cursor was last in.
https://github.com/johnwaugh1/projects/blob/main/calculator
I hope I have successfully linked my code above, and the action listener which I was currently working on was for the button a0, towards the end of my code.
I tried many approaches, mostly using if else statements combined with focus functions, but none was able to insert text into more than one textfield.
Add a FocusListener to tf1 and tf2, and add text to tfLast.
Off the top of my head, something like this should work.
private JTextField tfLast;
FocusAdapter fa = new FocusAdapter() {
public void focusGained(FocusEvent e) {
tfLast = (JTextField)e.getSource();
}
}
tf1.addFocusListener(fa);
tf2.addFocusListener(fa);
tf1.requestFocusInWindow();
. . . .
tfLast.setText( . . . . );
I want my JTextPane to have some functionality when the user presses Enter and not just change the line. Now I understand how to implement the functionality I want, but I still can't negate the line feed from pressing Enter. I have tried the following but it doesn't seem to work, the new line will be created anyway.
To give a better idea of what I'm trying to achieve here, the textpane is supposed to contain a certain filepath, so I want the user to be able to scroll only horizontaly and not add new lines verticaly. Is the JTextPane component suitable for this use?
locationPane = new JTextPane();
locationPane.addKeyListener(new KeyAdapter() {
#Override
public void keyPressed(KeyEvent arg0) {
if(arg0.getExtendedKeyCode() == KeyEvent.VK_ENTER){
locationPane.setText(locationPane.getText().substring(0, locationPane.getText().length()));
}
}
});
KeyListener is never a suitable soution for text components. Instead you should be using a DocumentFilter to filter out things you don't want to be added to the underlying Document.
See Implementing a Document Filter and DocumentFilter Examples for more details
You could also alter the insert-break key binding, changing the behaviour or taking additional action.
See this example for more details
the textpane is supposed to contain a certain filepath, so I want the user to be able to scroll only horizontaly and not add new lines verticaly. Is the JTextPane component suitable for this use?
I would suggest there are better options.
If you only have a single line of text then just use a JTextField. To handle the Enter key you can add an ActionListener to the text field.
If you do need multiple line then I would suggest you could use a JList.
I have a JTextArea and a button which removes the selected text (from textArea).
RemoveButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String test = textArea.getSelectedText();
textArea.replaceSelection("");
}
});
The code above works fine, however, when I click the button the text shows like this:
Here I removed the first and third Test. How can I make this dynamically update so it displays the text without gaps?
I could call a method re-populate the JTextArea with the remaining elements but is there a better way to do this?
You can try something like
textArea.setText(textArea.getText().replaceAll("\n\n",""));
However, a TextArea may not be the best component for that kind of use. A Jlist could be more fitted if you want to store input on different lines.
This one may be easy for you. But I'm stuck and can't figure out an algorithm for doing that. I want to show a JTextField and change the text on the JButton to "Hide" if it's "Search". If
the text on the JButton is "Search" a JTextBox should appear and vice versa, if the text is "Hide" make the JTextField invisivle and change the text on JButton to "Search"
This is how I have done it:
private void switchBtnText(){
searchTxtField.setVisible(true);
btnSearch.setText("Hide");
if(btnSearch.getText().equals("Hide")){
btnSearch.setText("Search");
searchTxtField.setVisible(false);
}
}
If I comment the if section it works to show the JTextField. My problem is to go back to the default settings which is a JButton with "Search" as a text and with an invisible JTextField.
The method is then called in an ActionEvent. I've done this before, in C#, so I know I'm close.
Thank you in advance. The fastest and best answer will get upvoted and accepted.
This should work although I've not tested it.
//btn action
private void toggleVisible(){
String btnVal = btnSearch.getText();
if(btnVal.equals("Search")){
searchTxtField.setVisible(true); // or however you are showing search field
btnSearch.setText("Hide");
}else{
searchTxtField.setVisible(false);
btnSearch.setText("Search");
}
}
Take a look at your execution sequence....
setText to "Hide"
if text equals "Hide", change text to "Show"
Try changing the logic so you check the text first, then make decisions about what should be done...
If text equals "Hide", change text to "Show"
Else, change text to "Hide"
I cannot get the android "delete" key to register in my TextField (scene2d ui element in libgdx) listener. Here is my code to define the text field:
nameTextfield = new TextField("", skin);
nameTextfield.setMessageText("Some Text");
uiStage.addActor(nameTextfield);
I tried this listener just to decode the keycode for the DELETE key:
nameTextfield.setTextFieldListener(new TextFieldListener() {
public void keyTyped (TextField textField, char key) {
textField.setText(String.valueOf(Integer.valueOf(key)));
}
});
Although it gives code for almost for all buttons, it doesn't even react on DELETE button.
I tested this on a Nexus 7.
From the TextField.java source it looks like the "DELETE" (and "BACKSPACE", and "TAB" and a couple other keys ) are handled specially by the TextField. These keys are never forwarded to any listener.
The built-in handler should do "the right thing" (trimming characters off the string contents).
Is delete not behaving correctly for your case in some way that led you to try to decode it?
Well the DELETE button should be implemented differently.
I suggest trying to verify if the pressed key is the DELETE button. If it is, you just do textField.getText(), trim the last letter off it, and set the new text with setText.
I'm sure there's a much more elegant way to do this, but it's the only workaround I can think of. After all, DELETE isn't really a char which you can throw inside setText. Is it? :/
LATER EDIT:
Print the key variable inside your listener, put a breakpoint there, and see what value is assigned to it.
Then also print (or check the javadoc) KeyEvent.KEYCODE_DEL (documentation here) to see what value this one takes.
the best way to solve this is the following:
You will need to listen to inputs from another listner, for example the same screen.
MainMenuScreen implements Screen, InputProcessor
then you will need to create a multiplexer to let inputs been listen from both the stage and the listner.
multiplexer = new InputMultiplexer();
then add the two listners:
multiplexer.addProcessor(this);
multiplexer.addProcessor(stage);
Now you will have to simply delete the field from here:
#Override
public boolean keyDown(int keycode) {
Gdx.app.log("Debug:", "keydown : "+keycode);
//DO SMOTHING LIKE
// if(keycode==...) deleteTextField();
return true;
}
Let me know if you have questions about this solution.
Worked great for me.
This issue has been solved by the latest nightly version of libgdx, the issue is known and discussed in the following link:
nexus button