Java gridbaglayout using scrollpane with draggable jpanel - java

Im using a gridbaglayout on a jpanel with a scrollpane, all that works fine. Later in my code i add another jpanel using the constraints(x,y) to the same panel on top of everything else thats already there using the index, this also works fine. This added jpanel uses a listener to let me drag the panel around which also works but my problem is when i scroll or resize the jframe it puts the jpanel back where it was before i dragged it. Why?

This added jpanel uses a listener to let me drag the panel around which also works but my problem is when i scroll or resize the jframe it puts the jpanel back where it was before i dragged it. Why?
Because you are using a layout manager. When the frame is resized the layout manager is invoked and components are assigned a size/location based on the rules of the layout manager.
Check out the Drag Layout. It will allow you to drag components around a panel without resetting the location of the components.

Related

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'

how to automatically resize jPanel

I´ve created through NEtBeans design of jframe, when its components is created automatically.
Now I put on this jFrame component jLayeredPane called agendaLayer, cause I need more panes here and switch.
I´ve set horizontal and vertical resize to layout that component belong so it is automatically resize to some value when windows (jFrame) is resized..
Then I´ve created also through designer new class stock which extends jPanel,
now I put this jPanel to JLayredPane and need to get its properties about resizable..
stock st = new stock();
st.setBounds(0,0,agendaLayer.getWidth(),agendaLayer.getHeight());
agendaLayer.add(st);
But It did not work, jLayredPane is automatically resized when window is changed, but jPanel not it remains the same..
, jLayredPane is automaticaly resized when window is changed, but jPanel not it remains the same..
A JLayeredPane uses a null layout by default so components are never resized.
cause I need more panes here and switch.
If you need to switch panels then use a CardLayout. See the Swing tutorial on Using a Card Layout for more information and examples.
You should look into layout managers:
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

Scrollbars in JScrollPane don't refresh

I have a JPanel in a JScrollPane. When JPanel changes (in my case, I draw an image inside), the scroll bars are not refreshing accordingly. I need to move them sligthly or resize the whole frame and then everything is fine. How to force JScrollPane to show correct scroll bars (ie. after loading the picture)?
You should include these methods when you want to modify the JScrollPane according to some changes:
JScrollPane.setViewportView(this);
JScrollPane.revalidate();
JScrollPane.repaint();

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.

Swing changing JTabbedPane to a JPanel

I'm having this strange issue with Swing. I have a main JPanel to which I am adding a JTabbedPane. Inside of this JTabbedPane I am adding another panel:
myTabbedPane.add(innerPanel, "Title", 0);
outerPanel.add(myTabbedPane);
Now, I no longer want myTabbedPane to be JTabbedPane, I want it to be a JPanel. When I change its type (and remove the extra params from its add() method), nothing within the outerPanel is visible anymore. (I am using setBounds() and I set the layouts to null).
Why does it work when using a tabbed pane but suddenly stop when switching to a JPanel? I know that this can be done differently (such as adding the innerPanel directly to the outerPanel), but please don't just tell me to do it differently. I'd just like to know why it suddenly doesn't work when using a JPanel instead. Is there an issue with adding a JPanel to a JPanel? Thanks!
Stop using null layout. Use BorderLayout and then use add inner panel to the center.
Tabbed pane used it own layered layout - that is why it worked before.

Categories