JScrollpane mouseListener - java

I hava a JScrollPane used to dram structures(diagrams) and I want to invoke a method every time user releases mouse click in this JScrollPane drawing area.
I used
yourJScrollPane.getViewport().getView().addMouseListener(yourMouseListener);
as per given here Use JScrollPane mouse listener over viewport components
This works. On every mouse release the desired method is invoked which updates the JTable in the GUI.
But, the problem is, the diagram which user draws moves all around the JScrollPane's available drawing/edit window area. For every click the drawing moves to a new place. If I remove the JScrollPane mouseListener, the structure does not move.
Is there any other way I can listen to the JScrollPane clicks and the view(diagrams) is not affected?
Thank you for the inputs.
Edit:
JScrollPane jScrolPane = (JScrollPane)(((JPanel) readyPanel.getComponent(1)).getComponent(0));
jScrolPane.getViewport().addMouseListener(new calculateDimensions(readyPanel, propertiesTable));
Here, readyPanel is a panel I receive from another method which is all stuffed with tool bars and JScrollPane.
I am trying to get the JScrollPane from this stuffed JPanel and invoke the method to calculate dimensions.

Related

JScrollPane automagically scrolls down to lowest component in JPanel

In my JPanel (which is in a JScrollPane) I have several JTextArea. When launching the program these areas are populated by JTextArea.setText("text");
The problem is that the JScrollPane scrolls down to the JTextArea with the lowest Y-position as if the user would have focus on the JTextArea, no matter when it was populated with text. I thought that I could work around the problem by populating the JTextArea at the bottom first, if the JScrollPane maybe want to scroll to the "current" JTextArea that's being used. It didn't work so I'm assuming it just remembers the lowest point it's been and then just stays there for some reason.
Setting the view like this doesn't make any difference.
JScrollPane.getViewPort().scrollRectToVisible(new Rectangle(0,0,1,1));
I've tested by neglecting to populate the last JTextAreas and the JScrollPane is then viewing the top of the JPanel, as I want.
What's happening, and why is it ignoring my code?
Update, answer selected
You have to set the viewport a tad later when the GUI is ready
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
scrollPane.getViewport().setViewPosition(new Point(0, 0));
}
});
JScrollPane.getViewPort().scrollRectToVisible(new Rectangle(0,0,1,1));
The scrollRectToVisible() method is meant to be used on the component added to the viewport (the panel containing the text areas), not the viewport itself. Not sure what happens when you invoke it directly on the viewport.
Instead I would do the following:
scrollPane.getViewPort().setViewPosition( new Point(0, 0) );
Note, you may need to wrap that statement in a SwingUtilities.invokeLater() to make sure the code is executed on the EDT after all the components have been positioned on the panel by the layout manager.

How to make a JFrame scrollable?

I would like to scroll through the contents of my JFrame up and down, preferably with a scroll bar. I don't want to wrap the contents inside a JPanel or JScrollPane, because this causes some visual glitches with my application.
Any idea on how to do this?
JScrollPane would be the easiest way; you say there are glitches, but that probably indicates a problem in your code that will still be a problem even without using a JScrollPane.
If you're absolutely set on not using a JScrollPane, you should create a JPanel using BorderLayout, add a JPanel (call it 'center') with BorderLayout.CENTER and layout set to null. Add your content within 'center', and add another JScrollBar to BorderLayout.EAST, add an AdjustmentListener to the JScrollBar. When the adjustmentListener triggers, you need to move your content (Component.setLocation(...)) that's in center to the relative y offset of the JScrollBar and call repaint on 'center'

Overlapped JLabel in Frame

I am writing a code in Java Swing in which a label is added in frame on the click of the button. And the Labels are added such that it overlap each other to create a stack like view.
I am creating my own layout using label.setbouds(x,y,100,100) function.
And each time button is clicked in its action listener a new label is added.
For example
if(e.getSource()==button){
label.setBounds(x+5,y+5,100,100);
frame.add(label);
frame.repaint();
}
Now the problem is when these labels are painted the first label added always remain on the top and newly add Labels are overshadowed by odd label.
I need a help if someone could explain how i can paint the frame such that newly added frames comes on the top and old labels get overlapped.Any suggestion is appreciated
Regards
ACoder
Insert your labels in a JLayeredPane with the proper index. Also make sure to set your JLabel as opaque (setOpaque(true)) so that it will actually obscure the content of the labels below. However, if a label below is bigger than the label on top, you will still see some part of it

JScrollPane mouse-wheel area

I have a JScrollPane and a JPanel inside. I see the scrollbars when needed, but the mouse wheeling works only when the mouse is over the scrollbars. Is there a property or something to make mouse wheeling work when the mouse pointer is over the whole panel, not just the scrollbars? You know like in browsers - you can scroll the page even if the mouse pointer is not over the scrollbars.
Thanks in advance!
It works for me (Java 6, Windows, JScrollPane containing a JPanel, mouse wheeling over JPanel). JPanel with rounded 100 pixel borders, min size 1000x1000, preferred size (4000x4000).
So I guess, that your existing code interferes. Try a separate example, and then rework your app.
Make sure main window(possibly a JFrame) implements Scrollable.

Java Swing JPanel Stay on top of JTable

I have a Jpanel that is used for displaying messages. When mouse enters a JLabel it expands to normal size and when mourse leaves it disappear.
There are overlaps between JTable and JPanel when the JPanel expands to normal size. As JTable's height cannot be changed during runtime only way I can think of is let JPanel stay on top of JTable.
I am very new to Java ans swing. Any idea please?
Sound like you should be using a tool tip.
Edit:
Try using a BorderLayout. Add your main panel to the CENTER and your message panel to the SOUTH. When you make the message panel visible you can revalidate() the main panel and the Center panel will shrink in size to take whatever space is not required by the south panel.

Categories