Swing Modal Action

Submitted by code_admin on Fri, 07/20/2018 - 12:53

Modal action in swing form

Make form implement the interface LongRunningSubActionInterface
e.g.

  1. public class RelationshipFrame extends JFrame implements LongRunningSubActionInterface {
  1.     private void startLongOperation() {
  2.         setEnabled(false);
  3.         this.getGlassPane().setVisible(true);
  4.         this.getGlassPane().repaint();
  5.     }
  6.     private void endLongOperation() {
  7.         setEnabled(true);
  8.         this.getGlassPane().setVisible(false);
  9.          this.setVisible(true);
  10.     }

Put the following in jbinit()

  1.         this.setGlassPane(new JComponent() {
  2.                 public void paint(Graphics g) {
  3.                     super.paint(g);
  4.                     Graphics2D g2d = (Graphics2D)g;
  5.                     g2d.setColor(Color.red);
  6.                     g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
  7.                                                                 5 * 0.1f));
  8.                     g.fillRect(0, 0, getWidth(), getHeight());
  9.  
  10.                 }
  11.             });

Then add actions as

  1.     private transient Action m_OpenAction = new ModalFormAction(this,this) {
  2.         {
  3.             putValue(NAME, m_strMenuOpen);
  4.             putValue(LARGE_ICON_KEY, new ImageIcon(RelationshipFrame.class.getResource("openfile.gif")));            
  5.             putValue(SMALL_ICON, getValue(LARGE_ICON_KEY));        
  6.         }
  7.  
  8.         public void actionPreformetDerived2(ActionEvent actionEvent) throws Exception {
  9.             //Write Code
  10.             //No need to if enabled
  11.             //No need for start or end long running operation
  12.             //No need for exception messagebox
  13.         }
  14.     };

google juice

Block Form Red Long Operation

Tags

RJM Article Type
Quick Reference