I have a JScrollPane with JPanel on it. I have also many different components on JPanel (for example Labels, JTextFields, JTextAreas). These components are added programmatically (at runtime, on the user request).
JPanel uses SpringLayout. The program calculates preferred size of JPanel after adding components, because scrolling doesn't function properly without calculating. Adding components and calculating preferred size are part of my prepareGui() method.
The data displayed on components are periodically refreshed (in my refreshData() method, which is invoked by listener).
My problem: after refreshing, scroll bar always sets to fixed position (I don't know why). I want the scroll bar to stay in old position after refreshing.
I tried to find a place, where the scroll bar moves. I checked some values at the start and at the end of refreshData() method, but they was the same (they didn't change inside of refreshData()):
scrollPane.getVisibleRect().getX()
scrollPane.getVisibleRect().getY()
scrollPane.getVisibleRect().getWidth()
scrollPane.getVisibleRect().getHeight()
panel.getVisibleRect().getX()
panel.getVisibleRect().getY()
panel.getVisibleRect().getWidth()
panel.getVisibleRect().getHeight()
scrollPane.getHorizontalScrollBar().getX()
scrollPane.getHorizontalScrollBar().getY()
scrollPane.getHorizontalScrollBar().getWidth()
scrollPane.getHorizontalScrollBar().getHeight()
scrollPane.getHorizontalScrollBar().getValue()
scrollPane.getVerticalScrollBar().getX()
scrollPane.getVerticalScrollBar().getY()
scrollPane.getVerticalScrollBar().getWidth()
scrollPane.getVerticalScrollBar().getHeight()
scrollPane.getVerticalScrollBar().getValue()
Changing of layout manager didn't take effect.
I noticed one thing (I don't know if it has any meaning). When I set only preferred size (in prepareGui()), the scrollbar moves to the end. When I set also size, the scrollbar moves to strange, fixed position.
How can I prevent this?
The scroll bar should keep it's current position.
I finally found the answer here: https://stackoverflow.com/a/5230477/5694159 (thanks to Johan M) in the topic about JTextArea instead of JPanel.
When the refreshData() is calling in separate thread it works good.
I hope this question and answer will help someone.
Related
On a JPanel I have a few buttons and JScrollPane containing a JTable.
One of the buttons is custom made, I have overridden the paintComponent method to draw an image.
However, if I set the contentAreaFilled or isOpaque properties to false (so that the background is not displayed) whenever I roll over the button the content of the scroll pane is hidden (a grey image appears inside, instead of the table contents). I tried using different layouts for my panel, but got the same result. The only fix I found is setting rolloverEnabled to false for my custom button, but that is not really an option for me, as I want the button image to change at rollover.
The weird thing is, it only happens when I set one of those two properties mentioned above to false. The button acts perfectly if I do not hide it's background. Just out of curiosity, I tried the same on my other two buttons, which are just JButtons with nothing customized, except their size, and it had the same effect on the scroll pane, so I'm pretty sure it doesn't have anything to do with the implementation of the custom button. Has anyone encountered this problem or know anything that may help me with this?
I have a pane that contains image content which changes during scrolling. The content is properly updated via a scrollwheel event because I implemented a wheel listener which repaints the image before setting the new scroll value.
However, when the user drags the scrollbar handle with the mouse, the image content was not being updated during the manual drag-scroll. So I implemented a timer which grabs the current scroll value and repaints the content given the new scroll position.
This solution however (despite 10 millisecond adjustments) results in a jumpy scroll experience. The image moves (without the necessary image adjustments) and then gets corrected after-the-fact every 10 milliseconds.
I had originally tried an adjustmentlistener, but it only gets the event after the handle is released. How can I live-update the pane content during a jscrollbar handle drag BEFORE the scrollbar machinery starts to simply move my content as if it was a static image? Can I somehow give the scrollbar machinery a clue that content has changed or something every time it tries to redraw the content? Or can I disable the scrollbar's ability to move the image and just rely on my timer to do it?
I would recommend that you add a ChangeListener to the JScrollBar's model, a BounderedRangeModel, and then based on the value of the model as well as its maximum and minimum, change your image. If you're swapping images, the easiest way to do this is by swapping a JLabel's ImageIcon.
Very strange occurrence, am in need of a quick way to make my panel components (labels and textboxes) visible again on the form in NetBeans. As soon as I added the Table to the right of the panel, the panel seems to have disappeared. Strangely enough, the components continue to be available in the left side, in the Navigator box, so they are not completely gone, just seem to be hidden. I was unable to find any Visible property, that I could to set to true. Any help is much appreciated. Also, what exactly triggered this behaviour, is this a bug? Many thanks in advance.
Its a focus issue, it makes things easier when you have lots of components.
At the moment you have the scrollPane selected/focused, so you will only be able to see that and any child components.
If you want to see sister or parent components you need to set your focus to your jFrame (or whatever component you want to see).
You can do this from the box on the bottom left, just double click on jFrame, its the top item and also parent to both the scrollPane you have selected now and also parent of your jPanel that contains all the other buttons and labels.
This will have been caused when you double clicked the scrollPane.
I wanted to ask whether it is possible to get the correct sizes of an JPanel after it has been placed in another JPanel that uses GroupLayout as Layout Manager. I have already tried to use:
.getPreferredSize(): this results in the Preferred Size that has been set by me, not the actual size that is drawn on the JPanel in the frame (if frame get's resized, the element will expand horizontally; which is not seen in the values).
.getSize(): it returns 0.
.getHeight(): it returns 0.
.getWidth(): it returns 0.
Maybe the positioning of the code is relevant, but it is executed AFTER shown on screen so it should not matter.
To force it to do that after it is shown on screen, maybe I can use EventQueue, but I'm not sure how.
Thank you for your answers!
You can get the "correct" size of the component only after it has been rendered, either by calling pack or setVisible(true) on the top level container.
Maybe the positioning of the code is relevant, but it is executed AFTER shown on screen so it should not matter.
Then something's not right. Are you sure that you're calling these methods on the visible components and not some variables that shadow them? Without code it's hard to tell where your error lies.
I have a Java swing application with a panel that contains three JComboBoxes that do not draw properly.
The combox boxes just show up as the down arrow on the right side, but without the label of the currently selected value.
The boxes will redraw correctly if the window is resized either bigger or smaller by even one pixel.
All of my googling has pointed to calling revalidate() on the JPanel to fix this, but that hasn't worked for me.
Calling updateUI() on the JPanel has changed it from always displaying incorrectly to displaying incorrectly half of the time.
Has anyone else seen this and found a different way to force a redraw of the combo boxes?
Can you give us some more information on how you add the combo boxes to the JPanel? This is a pretty common thing to do in Swing so I doubt that it's a JVM issue but I guess anything is possible.
Specifically, I would double check to make sure you're not accessing the GUI from any background threads. In this case, maybe you're reading the choices from a DB or something and updating the JComboBox from a background thread, which is a big no-no in Swing. See SwingUtils.invokeLater().