Set mouse click event on text? [closed] - java

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.

Related

JTextField dosen't properly insert characters [closed]

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

What does setBorderPainted mean? [closed]

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 6 years ago.
Improve this question
Very general question, I am just confused on what setBorderPainted does. I'm creating a GUI and I need it. What happens if I don't include it in my GUI, will it effect anything?
Thanks
It is a property of javax.swing.AbstractButton.
When you set this property to true, and if the button has a border, it will paint the border.
As far as I know in certain OS like MAC, when you set the background color of the button, only the border of the button is painted with color. Some people will set this property to false so that the background can be seen colored.

Keyboard shortcut unintentionally triggering Java's UndoManager undo function [closed]

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 8 years ago.
Improve this question
I have set up my application so that when I press Ctrl+Z, my UndoManager will undo the last entry. However, for some reason, Ctrl+H will also do this, and I have no idea why.
Here is my source code.
when I press Ctrl+Z, my UndoManager will undo the last entry. However, for some reason, Ctrl+H will also do this,
Ctrl+H is not invoking your UndoManager.
Ctrl+H, is a Key Binding to delete the previous character in a text component.
This is easy to test. Just type some text into the text component. Then set the caret to a different position in the text component. The character deleted will be the character at the current caret position, not the last character typed into the text component.
You can check out Key Bindings for a program that displays all the default Key Bindings for a given component.

add jbuttons with rows into jtable by clycking jbutton using netbeans [closed]

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
I want to insert lines and buttons to jtable while clicking on the button add using netbeans, I used JScrollPane but with only 4 buttons.How can I create a jtable with lines that contain are buttons in the last columns.
Many thanks.
How can I create a jtable with lines that contain are buttons in the last columns.
You need to add a column in the TableModel to display on the button. Then you need to use a custom renderer and editor for that column.
Check out Table Button Column for and class you can use.

How to do the best lookup textfield for JAVA Swing [closed]

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
How to do the best lookup textfield for JAVA Swing like below:
This is actually not very easy to achieve, not if you want a flexible and re-usable solution at the end of the process.
You need a JTextField (well, okay, that was obvious), a JList and a JPopupMenu or JWindow.
You need some kind of filtering mechanism that can take a String and produce a List of matches, which can then be displayed within the JList. This will require you to provide a ListCellRenderer for you particular implementation.
You will need to attach a DocumentListener to the JTextField. When the document is updated/changed, you will need to pass the text of the text field to your filtering engine to find all the possible matches and display them within the JList.
If the popup is not visible, you will need to make it visible, taking into account that the list might not fit on the screen based on where the text field is. If it is visible, you may need to re-pack the window to better accomidate the available options (if any)
If the user leaves the field, you will need to decide if you want to auto complete the text based on the first match, invalid the field or leave the text as is and close the popup if it's visible.
If the user clicks on a value, you will need to extract the String representation of the item and apply it to the text field and close the popup.
You will need to add key bindings to the text field to accommodate the down arrow which could open the popup window and/or move to the first item in the list. This will trigger a lose of focus, so you need to be prepared for it.
You will probably also want to provide a Escape keyboard binding for the popup/field so the user can dismiss the popup

Categories

Resources