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

helpcrypto helpcrypto helpcrypto at gmail.com
Mon Oct 27 13:06:18 UTC 2014


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 BorderLayout seem 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
setLocationRelativeTo after 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);
           }
       }
   }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/swing-dev/attachments/20141027/6dc2f7be/attachment.html>


More information about the swing-dev mailing list