Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I am currently trying to make a calculator in java. I created all the number buttons, operations,number display, etc. I also added the function to every button. Everything were working well. Then I added a button through which I could insert a dot(decimal point). But this is not properly getting inserted in JTextField. Here is the actionPerfomed() for button 1, and dot button....
one.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
numdisp.setText(numdisp.getText()+one.getText()); //numdisp is the number displayer(JTextField)
}
//Other buttons like button2,button3,button4,etc.... are not mentioned as they have the same set of code....
dot.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
numdisp.setText(dot.getText()+numdisp.getText());
});
Download the jar file here
When you press the one button the JTextField shows the number 1. Then press the dot button. The JTextField shows "1.". Everything is good till now. Then press 1 again. The expected result is "1.1" but instead It displays "11.". Why is this weird problem occurring? How to fix it?
The logic in your ActionListeners is different. In one case you add the text of the button at the beginning. In the other case you add the text of the button at the end.
So a better solution is to not use different ActionListeners, but to share the same ActionListener, so the logic will be same for all buttons and you don't have to create multiple listeners. This way you are less likely to make mistakes.
Check out setText method with panel and button. This example will show you how to:
create a single ActionListener to be shared by each button
"append" the text to the text field instead of replacing the text
use Key Bindings so the user can also just type the number
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
String EditableText = textField.getText(); //gets the text from a text box
System.out.println(EditableText);//prints it
wordParser.mainProcessor(EditableText);//sends it for processing for the main app
}
});
but when i run it , enter some text in the text box and click on the button it loops and spams in the console
i guess something is wrong with the action listener
one more thing that happens is the button greys outand i cant revert it until i stop the program
I think there is some issue with the
wordParser.mainProcessor(EditableText); //sends it for processing for the main app
Maybe some loop issues which blocks main thread.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to use two same action listener with different parameters on one button.
Is it possible. ?
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String value4 = text1.getText();
area1.setText(Integer.toString(value4.length()));
}
});
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String value5 = text2.getText();
area1.setText(Integer.toString(value5.length()));
}
});
Yes, it is absolutely allowed to add more than one ActionListener to a single JButton. If you're receiving an error, it is due to something else that you may not be showing us.
One problem that I see though is that both listeners call .setText(...) on the same JTextArea, area1, and so one call will overwrite and replace the text placed into the JTextArea by the other listener. It may be better to call area1.append(...) to avoid doing this (if area1 is in fact a JTextArea). This will append one String to the next String within the JTextArea.
Or another option is to combine the code into a single ActionListener, and this may be the cleanest way of doing what you want to do, because you must understand that you cannot control the order of how the listeners are called. That order is undefined.
Other issues:
Most Swing .addXxxx(...) methods allow you to add this type of item multiple times
While most Swing .setXxxx(...) methods allow you to set this property with only one item at a time, and so when this type of method is called, any previous similar property will be replaced by the new one.
Actions are like "super-charged" ActionListeners as they do all that ActionListeners do and more. You can only set one Action to a JButton, while you can add multiple ActionListeners.
You also can remove ActionListeners or Actions when need be, and this can allow you to swap them, very useful when you want the behavior of your button's to vary considerably depending on program state.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a project where some random text is displayed in a JScrollPane. This text is coded in and is not editable. Is there a way to create a mouse click event on each line of text and have that event take the text clicked on and have it display in a textField?
The part that stumps me is how to act on a line of text versus a clickevent on a button per se. Below is a rendering of the project and the areas containing the text.
Presumably your text is displayed in a JTextArea or JTextPane, so you would add a MouseListener to the component. Then when the MouseEvent is generated you can get the caret position. Using the caret position you can then use the Utilities class. It has methods like:
getRowStart(...)
getRowEnd(...)
Using these values you can then get the text from the Document using the getText(...) method.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to program a game that has a title screen, and when you press Enter it advances to the game. The issue that I'm running into is that you can't put add a JPanel from a KeyPressed method.
I have one panel for the title and another for the game board, what is likely the best way to go about this?
I have one panel for the title and another for the game board, what is likely the best way to go about this?
Use a Card Layout so you can easily switch from the title panel to the game panel.
Your title panel should also probably have a button like "Start Game" so the user knows what to do. Then they can either click the button or press Enter.
and when you press Enter it advances to the game
To make the button the default button (so it responds to Enter) for the panel you can use:
frame.getRootPane().setDefaultButton(gameButton);
If you don't like the idea of adding a button then you will need to use Key Bindings.
You might want to take a look at CardLayout.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
sorry for my blunders..
i am creating a simple java game.in my game is a rat image(jlabel) which appears randomly on the screen we need to click on the rat to win.if i click on the screen other than that includes rat image, the number of chances should decrease.i included levels in it.each level differs by rat speed.i included timer to change speed for levels.so wen i complete 1st level it will move 2 2nd level.here i am geting difficulty.when i click on level2 button it is called 2 times and when i click on rat image the rat image's listener is called 3 times and so on.. i am not getting how to do it.....
thank u :)
A JLabel doesn't support an ActionListener.
So I would guess you are using a MouseListener. If it keeps getting called one more time whenever you click on it then it sounds like you keep adding the MouseListener to the label in the event code.
You should only have one addMouseListener() statement in your code for each component.