JPanel Slide Content in JForm in Java

I have a question. I want to create a rocking shape that, by clicking on the button, will shift the panel (with its contents) to the left, so that the panel on the right replaces it with a smooth effect.

I tried to do it for a while, checking the size of the panel, and then minimizing it and showing the following:

while (jpanelprincipal1.getWidth() < 439 || jpanelprincipal1.getHeight() > 250) { int panel1width = jpanelprincipal1.getWidth(); int panel2height = jpanelprincipal1.getHeight(); jpanelprincipal1.setSize(panel1width -- , panel2height --); jpanelprincipal2.setSize(440,250); } 

I used this trick in C #, but with Application.DoEvent (); (as it is obvious that it is not available in java).

Anyway, can I make a slide effect of 2 or more panels?

BTW: Sorry for my very bad English!

Thanks in advance, Luis Da Costa

+1
source share
3 answers

it slides the panel (with its contents) to the left, so the panel on the right replaces it with a smooth effect

You ask if you want the panel to “slide”, but the code looks like you are trying to make the panel “shrink”, so it is replaced by another panel.

Assuming you have two panels with the same size, you can “slide” one of them until the other is displayed.

To do this, you use the panel with GridLayout. Thus, each component will have the same size. Then you add a panel to scrollpane without any scrollbars. The size of the scrollpane should be set to the size of the first compnoent. Then you can “shift” the two panels by changing the position of the viewport. Therefore, in your timer, you will have a code similar to:

 JViewport viewport = scrollPane.getViewport(); Point position = viewport.getViewPosition(); position.x += 5; viewport.setViewPosition( position ); 

Then you stop the timer when the position is larger than the size of the component.

+6
source

As suggested by @HFOE, javax.swing.Timer is a good choice for animation. You can call the setDividerLocation() JSplitPane of ActionListener from the ActionListener . See How to use separation panels for additional options.

 import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; /** @see http://stackoverflow.com/questions/5069152 */ public class SplitPaneTest { double ratio = 0.5; double delta = ratio / 10; private void create() { JFrame f = new JFrame("JSplitPane"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MyPanel p1 = new MyPanel(Color.red); MyPanel p2 = new MyPanel(Color.blue); final JSplitPane jsp = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT, true, p1, p2); Timer timer = new Timer(200, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { ratio += delta; if (ratio >= 1.0) { ratio = 1.0; delta = -delta; } else if (ratio <= 0) { delta = -delta; ratio = 0; } jsp.setDividerLocation(ratio); } }); f.add(jsp); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); timer.start(); } private static class MyPanel extends JPanel { Color color; public MyPanel(Color color) { this.color = color; this.setPreferredSize(new Dimension(300, 300)); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(color); g.drawLine(0, 0, getWidth(), getHeight()); g.drawLine(getWidth(), 0, 0, getHeight()); } } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new SplitPaneTest().create(); } }); } } 
+6
source

I would do it with a Swing timer. Change the class field representing the x, y position of the moving JPanel in the ActionListener timer, and then rename it to the container containing the JPanels. JLayeredPane can work well as a container for sliding JPanels.

Edit 1: in relation to your request for code, I think you should try to create a very small compiled executable program that tries to do this, and then publish your code explaining the behavior of your program as editing your source post. Also send us a comment to notify us of your changes. Then we can check your code, test it, change it and help you turn it into a working program. This is called creating Short, Self Contained, Correct (Compilable), Example "or SSCCE (please check the link).

+4
source

All Articles