Note that there are some explanatory texts on larger screens.

plurals
  1. POSwing resize slow/jumpy
    text
    copied!<p>I made my own BottomBar with a simple gradient extending JComponent and adjusting the paintComponent() method.</p> <p>Then I add it to the SOUTH of my JFrame which uses BorderLayout.</p> <p>Everything looks correct at the beginning. When I resize the frame the BottomBar gets repainted and set to the new position correctly. The think is, it happens a few milliseconds to late, so that one can see the JFrame 's background for a second.</p> <p>The funny thing is, that when I set the execution environment to Java-SE 1.6 it works... (instead of 1.7) Also, Im running it on a mac, if that makes a difference.</p> <p><img src="https://i.stack.imgur.com/QAJGz.png" alt="Screenshot comparing window when resized and when not"></p> <hr> <h2>Code - JButton Example</h2> <pre><code>import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JButton; import javax.swing.JFrame; public class Main { public static void main(String args[]){ EventQueue.invokeLater(new Runnable() { @Override public void run() { JFrame frame = new JFrame("Resize Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new JButton(), BorderLayout.SOUTH); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); } }); } } </code></pre> <hr> <h2>Code - BottomBar Example</h2> <p>Main:</p> <pre><code>public class Main { public static void main(String args[]){ Frame window = new Frame(); window.setSize(500, 400); window.setVisible(true); } } </code></pre> <p>Frame:</p> <pre><code>import java.awt.BorderLayout; import javax.swing.JFrame; public class Frame extends JFrame{ private static final long serialVersionUID = 1L; public Frame() { setLayout( new BorderLayout() ); getContentPane().add( BorderLayout.SOUTH, new BottomBar() ); } } </code></pre> <p>BottomBar</p> <pre><code>import java.awt.Color; import java.awt.Dimension; import java.awt.GradientPaint; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import javax.swing.JComponent; public class BottomBar extends JComponent { private static final long serialVersionUID = 1L; public BottomBar() { setSize(200, 30); setPreferredSize( new Dimension(200, 30) ); } protected void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); GradientPaint gradient = new GradientPaint(0, 0, new Color(185, 185, 185), 0, getHeight() , new Color(151, 151, 151) ); g2.setPaint(gradient); g2.fillRect(0, 0, getWidth(), getHeight()); g2.setColor( new Color(64, 64, 64) ); g2.drawLine(0, 0, getWidth(), 0); g2.setColor( new Color(215, 215, 215) ); g2.drawLine(0, 1, getWidth(), 1); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload