I'm making a fairly simple text editor, and I have a question about my style buttons. When I highlight text and click my "bold" button, the text bolds as expected, but my selection is not longer visible. I can still unbold the selection, italicize it, or underline it, but you just can't see what is selected. So, I'm wondering if there is a setting that will allow me to click the button, but keep my selection? I tried a JMenuItem instead of a JButton, and that seemed to work, but then it made my toolbar look quite bad. Sample code below.
//frame and pane creation up here
JToolBar tool = new JToolBar();
JToggleButton boldButton = new JToggleButton("Bold");
boldButton.addActionListener(new StyledEditorKit.BoldAction());
tool.add(boldButton);
Any help is appreciated.
So, I'm wondering if there is a setting that will allow me to click the button, but keep my selection?
boldButton.setFocusable( false );
As you noticed, the selection is still there but clicking on the toolbar button removes the focus from the text pane and hides the selection. You need to set the focus back using requestFocus. However, you will need to write your own action listener to add the focus code - you could extend BoldAction to do this.
Related
Hiļ¼there is a problem in my java application, the text in my Jbutton is too long so it only show"..." on the button. Now I want to add some component to help show the actual text on the button.
What method can I use to solve such problem rather than adjusting the text font or button size?
Thank you
Instead of a label, you should add a tooltip to the button:
jb.setToolTipText("The full text of the button");
A small downside is that this tooltip will also be shown if the button text is fully visible, and in that case the tooltip doesn't provide any additional information, which will be confusing. I don't know off my head how to solve this, but it's definitely possible.
I have a few radio buttons and then a checkbox, I have tooltip's for the radio buttons but not the checkbox. Which works okay, unless I get a radio to display it's tool tip text and then quickly move to the checkbox, before the tip disappears, and now I have my mouse on the checkbox but it is displaying the tooltip for the previously moused over radio button.
I tried setting a blank ("") tool tip for the checkbox, which works okay except that it displayed a very small tooltip square.
Is there anyway to either force the previous tooltip to go away or to properly set a nothing tooltip for the checkbox?
radioButtonsetText("text");
radioButton.setToolTipText("tooltip text");
checkBox.setText("text");
checkBox.setToolTipText("");
or to properly set a nothing tooltip for the checkbox?
Components don't have a tooltip by default. So don't set the tooltip and your won't have a problem.
If you set the text and want to remove it the try using:
component.setToolTipText(null);
I'm looking for a substitute of .setEnabled(false), so that the button is not clickable.
I have read that I could remove the ActionListener from the button, but I just want the specific buttons to be not clickable, and do not know how to do it anyway.
Or how can I get rid of the "selected" effect after clicking a button?
Because in my application I have a grid of JButtons that are used as icons
Then don't use buttons, use a JLabel which also supports an Icon.
Or maybe use a JList which can also support a grid.
Or if you use a JButton then you need code like:
JButton button = new JButton(...);
button.setBorderPainted( false );
button.setFocusPainted( false );
I assume that the problem is how the button looks grayed out, because setEnabled really is the way you disable a button - which just means making it not clickable (or did you want it to respond to keyboard input?). If that's the case, then you can change the way it looks by using html:
button.setText("<html><font color=black>3</font></html>");
I have a JTextPane sitting in a JFrame, with a popup menu that is assigned to the JTextPane through the JTextPane.setComponentPopupMenu method.
I want to give the JTextPane a "Word-like" popup behavior. By that I mean, if you right click outside of your current text selection, the caret will reposition to where you right clicked, with menu options that affect a text selection (such as cut, copy, or bold) disabled. If you right click within your current text selection, the popup will appear with options that effect text selection enabled, the text selection will persist, and the caret will not move.
The problem is I cannot seem to find where I can put the code that handles the selection change. I tried:
Using the "PopupMenuWillBecomeVisible" event which is triggered before a popup becomes visible. The event passed into this method does not contain any mouse event information so there is no way for me to use viewtomodel to find out how to modify the selection. I could use MouseInfo but that seems dubious at best.
Using MousePressed/MouseReleased events in the JTextPane or JFrame. Apparently, neither of these events are invoked when a popup menu is triggered. In fact, I still can't determine what the parent component of my popup menu is. (I did read that in windows "MouseReleased" is the popup trigger, while in other systems "MousePressed" is the trigger. I tried both and neither worked).
So, I guess the problem is that I can't seem to find a place to put code where it would be called before the popup menu becomes visible, but has awareness of the mouseEvent that triggered the popup menu. I must be missing something here.
with a popup menu that is assigned to the JTextPane through the JTextPane.setComponentPopupMenu method.
You can use the older approach of displaying the popup based on your own custom MouseListener.
See the section from the Swing tutorial on Bringing Up a Popup Menu. Now you have access to the MouseEvent so you can convert that point to a point in the Document so you know where the click was made, on selected or unselected text.
I am adding a button to a panel using Miglayout, and try what I might, I cannot get it to go to the right end of the panel. It insists on going flush left. Oddly, the demo is kind of short on such on a example (it only shows it in the context of other buttons on the same panel.
I have a panel like this:
dialog
->complex display retrieved from another class
OK Button here.
Except that it always insists on putting it like this:
dialog
->complex display retrieved from another class
OK Button here.
OK, I got the answer to this (finally). When adding the panel that contains the button add the component constraint of "gapbefore push." I had been trying that, but only on the button itself, not the panel.
panel.add(button, "skip(the amount of cells you have), al 100%")