View unanswered posts | View active topics It is currently Sun Mar 26, 2017 12:42 am



Reply to topic  [ 2 posts ] 
 Unnecessary calls to revalidate() 
Author Message

Joined: Mon Aug 23, 2010 6:24 am
Posts: 54
Post Unnecessary calls to revalidate()
I've run across a bug similar to http://migcalendar.com/forums/viewtopic.php?f=8&t=3760 without using hideMode(). Here is a new code sniplet:

Code:
public class FirmwarePanel extends JPanel
{
   private static final long serialVersionUID = 1L;
   private final JTree deviceTree;
   private final JTextField pathValue = new JTextField(new PlainDocument(), null, 0);
   private final Color originalPathForeground = pathValue.getForeground();
   private final JButton startButton = new JButton("Start");
   private final JPanel statusPanel = new JPanel(new MigLayout(new LC().debug(debugFrequency)));
   private static final int debugFrequency = 0;
   private final Logger log = LoggerFactory.getLogger(FirmwarePanel.class);

   @Inject
   public FirmwarePanel(@Assisted DefaultTreeModel deviceTreeModel, final Dispatchers dispatchers,
      @Named("nameLimit") final String nameLimit)
   {
      super(new MigLayout(new LC().debug(debugFrequency)));

      this.deviceTree = new JTree(deviceTreeModel);
      deviceTree.setOpaque(false);
      FirmwareTreeRenderer cellRenderer = new FirmwareTreeRenderer(new JTreeTransparentCells());
      deviceTree.setCellRenderer(cellRenderer);
      deviceTree.setCellEditor(new FirmwareTreeEditor(deviceTree, new JTreeTransparentCells()));
      deviceTree.setEditable(true);
      deviceTree.setVisibleRowCount(10);
      deviceTree.setRootVisible(false);

      JScrollPane devicePane = new JScrollPane(deviceTree);
      devicePane.setBorder(BorderFactory.createEmptyBorder());

      JPanel devicePanel = new JPanel(new MigLayout(new LC().debug(debugFrequency)));
      add(devicePanel, new CC().pushX().grow().spanY());
      devicePanel.setBorder(BorderFactory.createTitledBorder("Devices"));
      devicePanel.add(devicePane, new CC().push().grow().wrap());

      JPanel filePanel = new JPanel(new MigLayout(new LC().debug(debugFrequency)));
      add(filePanel, new CC().pushX().growX().wrap());
      filePanel.setBorder(BorderFactory.createTitledBorder("File"));
      filePanel.add(pathValue, new CC().pushX().growX());
      pathValue.setEditable(false);
      pathValue.setText("Please select a file");
      pathValue.setForeground(SystemColor.textInactiveText);

      final JButton browseButton = new JButton("Browse...");
      filePanel.add(browseButton, new CC().wrap());
      SelectFile browseFile = new SelectFile();
      browseButton.addActionListener(browseFile);
      pathValue.addFocusListener(browseFile);

      startButton.setEnabled(false);
      startButton.addActionListener(new StartUpdate());
      filePanel.add(startButton, new CC().skip(1).growX().wrap());

      add(statusPanel, new CC().push().grow().spanY());
      statusPanel.setBorder(BorderFactory.createTitledBorder("Status"));
   }

   /**
    * Builds a FirmwarePanel.
    */
   @SuppressWarnings("PublicInnerClass")
   public static interface Builder
   {
      /**
       * Creates a new FirmwarePanel.
       * <p/>
       * @param deviceTreeModel the device tree model
       * @return a new FirmwarePanel
       */
      FirmwarePanel create(DefaultTreeModel deviceTreeModel);
   }

   /**
    * Selects a firmware file.
    */
   private class SelectFile extends FocusAdapter
      implements ActionListener
   {
      @Override
      public void actionPerformed(ActionEvent e)
      {
         JComponent source = (JComponent) e.getSource();
         FileDialog dialog = new FileDialog((JFrame) SwingUtilities.getWindowAncestor(source),
            "Select File", FileDialog.LOAD);
         dialog.setVisible(true);
         boolean fileReturned = false;
         for (File file : dialog.getFiles())
         {
            pathValue.setText(file.getAbsolutePath());
            pathValue.setForeground(originalPathForeground);
            startButton.setEnabled(true);
         }
      }

      @Override
      public void focusGained(FocusEvent e)
      {
         JComponent source = (JComponent) e.getSource();
         source.setFocusable(false);
         actionPerformed(new ActionEvent(e.getSource(), ActionEvent.ACTION_PERFORMED, e.paramString()));
      }

      @Override
      public void focusLost(FocusEvent e)
      {
         JComponent source = (JComponent) e.getSource();
         source.setFocusable(true);
      }
   }

   /**
    * Starts the firmware update
    */
   private class StartUpdate implements ActionListener
   {
      @Override
      public void actionPerformed(ActionEvent e)
      {
         JLabel message = new JLabel("Uploading file");
         statusPanel.add(message);

         JProgressBar progress = new JProgressBar(0, 100);
         statusPanel.add(progress, new CC().grow());
         
         revalidate();
      }
   }
}


In the above code, if I remove revalidate() then statusPanel does not repaint whenever StartUpdate.actionPerformed() is invoked and you will not see the added components. Why does the above code need revalidate()? Isn't this a bug?

Gili


Wed Dec 28, 2011 12:57 am
Profile

Joined: Mon Aug 23, 2010 6:24 am
Posts: 54
Post Re: Unnecessary calls to revalidate()
This problem is reproducible in versions 3.5.5 through MigLayout 4.2 (the latest version at this time) and is very annoying. Sometimes I have to add both revalidate() *and* repaint() after adding components into a container. Can you please take a look?

Thanks,
Gili


Tue Jan 03, 2012 11:57 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.