<Swing Dev> Autosize dialog pack() seems to be buggy on Linux

Alexander Scherbatiy alexandr.scherbatiy at oracle.com
Wed Oct 29 21:33:10 UTC 2014


   Hi,

   Could you create an issue on it: http://bugreport.java.com/bugreport

   Thanks,
   Alexandr.

On 10/27/2014 5:06 PM, helpcrypto helpcrypto wrote:
> Hi.
>
>
> I'm usually at icedtea web maillist, but this bug seems to be 100% 
> swing-related.
>
> I'm trying to display a dialog, centered on screen, which size is 
> fitted to changing contents.
> AFAIK, this should be done using pack().
>
>
> Running the "attached testcase", I have noticed two things:
>
>  - After each pack, as the BorderLayoutseem to be center-resized, the 
> window moves down after each click (but pack always seem to work). 
> IMHO this is not a friendly behaviour, but can be solved adding a 
> setLocationRelativeToafter pack
>  - If I add a setLocationRelativeTo(as shown below) pack fails from 
> time to time and incorrectly sizes the dialog.
>
> Can you guys help me?
> Registered this bug a few weeks ago: 
> http://icedtea.classpath.org/bugzilla/show_bug.cgi?id=1798
>
>
>     package testcase;
>     import java.awt.BorderLayout;
>     import java.awt.event.ActionEvent;
>     import java.awt.event.ActionListener;
>     import java.lang.reflect.InvocationTargetException;
>     import java.util.logging.Level;
>     import java.util.logging.Logger;
>     import javax.swing.*;
>     import javax.swing.border.EmptyBorder;
>     public class Testcase extends JDialog {
>     private JPanel panel;
>     private JLabel label;
>     private JButton foo;
>     private JButton bar;
>     public Testcase() {
>             setTitle("Test");
>             setModal(true);
>             setResizable(false);
>             setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
>             foo = new JButton();
>             foo.setText("foo");
>             foo.addActionListener(new ActionListener() {
>                 @Override
>     public void actionPerformed(ActionEvent e) {
>                     foo();
>     }
>     });
>     foo.setVisible(true);
>             bar = new JButton();
>             bar.setText("bar");
>             bar.addActionListener(new ActionListener() {
>                 @Override
>     public void actionPerformed(ActionEvent e) {
>                     bar();
>     }
>     });
>     bar.setVisible(true);
>             label = new JLabel();
>             label.setBorder(new EmptyBorder(10, 10, 10, 10));
>             label.setText("a");
>             panel = new JPanel();
>             panel.setLayout(new BorderLayout());
>     panel.setVisible(true);
>             panel.setBorder(new EmptyBorder(10, 10, 10, 10));
>             panel.add(foo, BorderLayout.NORTH);
>             panel.add(label, BorderLayout.CENTER);
>             panel.add(bar, BorderLayout.SOUTH);
>             getContentPane().add(panel);
>             pack();
>             //validate();
>     //repaint();
>             setLocationRelativeTo(null);
>     }
>     public void foo(){
>                     label.setText("<html>"+
>     "foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoo"+
>     "<br/>"+
>     "foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoo"+
>     "<br/>"+
>     "foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoo"+
>     "<br/>"+
>     "foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoo"+
>     "<br/>"+
>     "foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoo"+
>     "</html>");
>                     pack();
>                     //validate();
>     //repaint();
>                     setLocationRelativeTo(null);
>     }
>     public void bar(){
>                     label.setText("<html>bar</html>");
>                     pack();
>                     //validate();
>     //repaint();
>                     setLocationRelativeTo(null);
>     }
>     public static void main(String[] args) {
>     try {
>     SwingUtilities.invokeAndWait(new Runnable() {
>                     @Override
>     public void run() {
>     JDialog test= new Testcase();
>                         test.setVisible(true);
>     }
>     });
>     } catch (InterruptedException | InvocationTargetException ex) {
>                
>     Logger.getLogger(Testcase.class.getName()).log(Level.SEVERE, null,
>     ex);
>     }
>     }
>     }
>




More information about the swing-dev mailing list