I have a custom auto complete JTextField. I use the JPopupMenu to contain the selection.
I want to pop the JPopupMenu right under the JTextField using this method.
Rectangle r = textField.getBounds();
popup.show(textField, (int)(r.getX()), (int)(r.getY() + textField.getHeight));
popup.setVisible(true);
It works when I put the component in a simple JFrame. But when I put the component in a complex layout with JScrollPane. The location becomes random and inconsistent. I'd like the popupMenu to "shadow" the textField in any condition. How do I achieve that? Thanks.
The popup location is relative to the parent component.
popup.show(textField, 0, textField.getHeight());
should place it right under the textField.
Related
I would like to make a custom component that is much like a JList, except there is a little "x" button on the right side of each cell that removes that cell from the list (and triggers an event). I know that you would have to extend JList, but looking at the code for JList I have no idea where to go from there. For reference, I would like the list to be like on the macOS Messages app (except the "x" button can always be visible, not just when the mouse is over the cell).
I would like to make a custom component
I suggest you do that by extending JPanel and adding real components to your panel. Then you can actually add JButton with the "x" that can respond to the mouse event.
A JList does not display real components, only rendered images of the component and therefore is does not respond to events if you try to click on the "x".
The other option is to use a JTable. A JTable does allow you to display values in a column format. In this case it does support the concept of editors, which would allow you to add a button to a column. For example check out Table Button Column.
How to auto scroll JTextArea on middle mouse button click? To be clear, when we click the mouse wheel (middle button) in Firefox (for example) and push it down/up we will be able to scroll the page automatically when we release the middle button, we can alo see a round icon at the point of middle button click.
Here is a screenshot of it.
I think there is no such functionality for JTextArea in Java. Is there any hack to implement it.
Any answer is appreciated.
To determine which of the Mouse buttons is pressed, these three methods from SwingUtilities could help you:
isLeftMouseButton
isMiddleMouseButton
isRightMouseButton
Based on the mouse button clicked, you can take appropriate action to scroll the JTextArea programmatically. Probably, you can use something like this:
textArea.setCaretPosition(textArea.getDocument().getLength()); -> to move to the end of the JTextArea
You can look at this link to get a good idea of positioning the cursor position in JTextArea
I have a scrollable JPanel in which are added many Editor Panes (having their respective Scroll Pane) in Box layout (Vertical Axis).
My problem is that on clicking a particular button I want a particular Editor Pane to gain focus and also be visible on the screen.
I am unable to make that Editor pane visible.
I tried
scrollRectToVisible(jScrollPane5.getVisibleRect()); //It did nothing !
I also tried
scroller.setViewportView(jScrollPane5); // It made the particular editor pane occupy the entire panel !
Kindly suggest what to do ?
Thanks.
Invoke scrollRectToVisible() on the "Editor Pane" that you want to be visible in the scroll pane.
Also, after clicking on the button it will have focus so you need to use requestFocusInWindow() on the edtitor pane that you want to have focus.
You can also check out Scrolling a Form which will do this for you whenever any component in the scroll pane gets focus.
scrollRectToVisible(theWantedEditorPane.getBounds())
I've trying to create a JToolTip over a JPanel. The idea is, that user move over the JPanel (mouseMove) and behind the cursor will be the tooltip. But I dont know, how to set up tooltip to JPanel. I mean, that I create instance of JToolTip, setLocation by cursor (in mouseMove) and then I don't know how to set up this new tooltip to panel (like JPanel.setToolTip(newJToolTip)).
You can set a tooltip on a JPanel using setToolTipText(). When the mouse cursor stops, the tooltip is displayed. If you no longer need this behavior, just call the method again with null as a parameter.
i have jFrame = frame
it have jcombobox = combo
then i have jpanel = panel
i have many component inside this panel
i try to add this panel into combobox popupmenu
so if combobox clicked,
panel that have many components will show up
it is possible to add panel into combobox popup menu?!?!
how to do it???
i already read
http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html
and
http://docs.oracle.com/javase/tutorial/uiswing/examples/components/ComboBoxDemoProject/src/components/ComboBoxDemo.java
but still not have any clue
how to do it?
thankz a lot for any help...
So from your description, you have a panel that is not visible, that you would like to appear if the combobox is clicked? So it will appear for any option in the combobox?
That should be simple enough. Lets modify the JLabel in this ComboBoxDemo from the Java Tutorials. Since they both inherit from JComponent, we will be able to make the JLabel and the JPanel visible in the same way.
First, make sure you understand what the demo is doing. The Combobox options are changing the format of the date's text in the JLabel. We want to edit the demo in such a way that this JLabel is not visible until after we select any option in our JComboBox.
First, we will want to include a boolean as a class variable, so that we can access it in any of our methods.
boolean visibleComp;
Next, in the constructor, you will want to change the JLabel "result" to be invisible by default. We can do this by using the setVisible method of the JComponent.
result.setVisible(false);
Now we need to control when and how result becomes visible -- as we continue through the code, we see that the actionPerformed method handles our events, and passes the formatting details off to another method called reformat.
Since reformat is also called in our constructor, we will want to set our boolean value in the actionPerformed method.
visibleComp = true;
We will then want to add a conditional statement to the try block in reformat -- this will check to see if our boolean is true, which would only occur if an action had been performed by the user. We can use this to set our component's visibility.
if(visibleComp){
result.setVisible(true);
}
You can easily interchange a JPanel with this example. Hope that helps.