0

I am working on a sidebar for an application, this sidebar only extends if it has been pressed. I am using a Thread loop to update the size of the JPanel but for some reason the JFrame doesn't update the size of the it. The MouseListener works, and the loop works too, but it still does'nt seem to update.

Heres the code :-

public class DisplayHandler implements Runnable{
    
    Thread thread;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(()->new DisplayHandler());
    }
    
    JFrame f = new JFrame();
    JPanel westData = new JPanel();
    
    boolean startExtending = false;
    int westDataW = 100;
    
    private DisplayHandler() {
        
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(1280, 720);
        f.setLocationRelativeTo(null);
        f.setLayout(new BorderLayout());
        
        onStart();
        
        f.setVisible(true);
        
        thread = new Thread(this);
        thread.start();
    }

    private void onStart() {
        
        Color sideBar = new Color(0,0,0,50);
        
        westData.setPreferredSize(new Dimension(westDataW,720));
        westData.setBackground(sideBar);
        westData.setFocusable(true);
        westData.addMouseListener(new MouseListener() {

            @Override
            public void mouseClicked(MouseEvent e) {
                startExtending = true;
            }

            @Override
            public void mousePressed(MouseEvent e) {
            }

            @Override
            public void mouseReleased(MouseEvent e) {
            }

            @Override
            public void mouseEntered(MouseEvent e) {
            }

            @Override
            public void mouseExited(MouseEvent e) {
                startExtending = false;
            }
            
        });
        
        f.add(westData, BorderLayout.WEST);
        
    }
    
    @Override
    public void run() {
        while(thread!=null) {
            
            if(startExtending) {
                if(westDataW<300) {
                    westData.setPreferredSize(new Dimension(westDataW, 720));
                    westDataW++;
                }
            }
            if(!startExtending) {
                if(westDataW>100) {
                    westData.setPreferredSize(new Dimension(westDataW, 720));
                    westDataW--;
                }
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
  • 2
    This code ignores Swing threading rules by trying to make mutational changers to the GUI off of the Swing event thread, the EDT. Better to use a Swing Timer. Also, after re-sizing, you may need to call `pack()` or at least revalidate/repaint. – Hovercraft Full Of Eels May 14 '23 at 12:46
  • 2
    The way I've seen a sliding `JPanel` done before is to create an image of the `JPanel` and use a Swing `Timer` to show more and more parts of the image. When the image is fully shown, stop the animation and display the `JPanel`. Oracle has a helpful tutorial, [Creating a GUI With Swing](https://docs.oracle.com/javase/tutorial/uiswing/index.html). Skip the Learning Swing with the NetBeans IDE section. – Gilbert Le Blanc May 14 '23 at 14:14
  • You should never ignore interrupts. However, if you take the above comments’ advice and use a javax.swing.Timer (*not* java.util.Timer!), you won’t have to worry about interrupts at all. – VGR May 14 '23 at 15:18
  • You were already given the recommendation to use a Swing Timer in your [last question](https://stackoverflow.com/q/76075301/). Why are you asking questions but then ignoring recommendations given? – Hovercraft Full Of Eels May 14 '23 at 16:30
  • @HovercraftFullOfEels using the `pack()` method resizes the JFrame, and both revalidate and repaint don't make a change, also I felt like this problem was caused by something else, but I will try the Swing Timer ASAP – Grinding For Reputation May 15 '23 at 12:41
  • I found the method `resize` in JPanel, it seems to work but is it practical? – Grinding For Reputation May 15 '23 at 12:53

0 Answers0