Advertisement
Guest User

custom jdialog

a guest
Feb 2nd, 2013
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.48 KB | None | 0 0
  1. package gui.windows;
  2.  
  3. import generic.StaticFunctions;
  4.  
  5. import java.awt.Container;
  6. import java.awt.Dimension;
  7. import java.awt.Font;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.awt.event.FocusAdapter;
  11. import java.awt.event.FocusEvent;
  12. import java.awt.event.KeyEvent;
  13.  
  14. import javax.swing.AbstractAction;
  15. import javax.swing.ActionMap;
  16. import javax.swing.BorderFactory;
  17. import javax.swing.Box;
  18. import javax.swing.BoxLayout;
  19. import javax.swing.InputMap;
  20. import javax.swing.JButton;
  21. import javax.swing.JComponent;
  22. import javax.swing.JDialog;
  23. import javax.swing.JFrame;
  24. import javax.swing.JLabel;
  25. import javax.swing.KeyStroke;
  26.  
  27. import configuration.ConfigurationOptions;
  28.  
  29. public class CustomConfirmDialog extends JDialog implements ActionListener
  30. {
  31.     /**
  32.      *
  33.      */
  34.     private static final long serialVersionUID = 1L;
  35.    
  36.     private ConfigurationOptions conf;
  37.    
  38.     Box box_message;
  39.     Box box_buttons;
  40.     Container content_pane;
  41.    
  42.     JLabel lbl_message;
  43.     JButton btn_yes;
  44.     JButton btn_no;
  45.     JButton btn_cancel;
  46.    
  47.     String message;
  48.    
  49.     int result;
  50.  
  51.     public CustomConfirmDialog(JFrame parent, String title, String message)
  52.     {
  53.         super(parent, title, true);
  54.         if (parent != null) {
  55.           setLocationRelativeTo(parent);
  56.         } else {
  57.             setLocationRelativeTo(null);
  58.         }
  59.        
  60.         setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
  61.        
  62.         result = 0;
  63.        
  64.         conf = new ConfigurationOptions();
  65.        
  66.         createObjects();
  67.         setLanguageElements();
  68.         addItemsToLayout();
  69.         pack();
  70.         setVisible(true);
  71.         addCustomKeyMaps();
  72.        
  73.         btn_yes.addFocusListener(new buttonfocusEventHandler());
  74.     }
  75.    
  76.     private void setLanguageElements()
  77.     {      
  78.         btn_yes.setText("Yes");
  79.         btn_no.setText("No");
  80.         btn_cancel.setText("Cancel");
  81.     }
  82.    
  83.     private void createObjects()
  84.     {
  85.         content_pane = getContentPane();
  86.         box_message = Box.createHorizontalBox();
  87.         box_buttons = Box.createHorizontalBox();
  88.        
  89.         box_message.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
  90.         box_buttons.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
  91.        
  92.         lbl_message = new JLabel("aaaasadfasdasdgsd");
  93.         StaticFunctions.setElementFontSize(lbl_message, Font.BOLD, conf.getTableFontSize());
  94.        
  95.         btn_yes = new JButton(new ResultYes());
  96.         btn_no = new JButton(new ResultNo());
  97.         btn_cancel = new JButton(new ResultNo());
  98.     }
  99.    
  100.     private void addItemsToLayout()
  101.     {
  102.         content_pane.add(box_message);
  103.             box_message.add(lbl_message);
  104.        
  105.         content_pane.add(box_buttons);
  106.             box_buttons.add(btn_yes);
  107.             box_buttons.add(Box.createRigidArea(new Dimension(10,10)));
  108.             box_buttons.add(btn_no);
  109.             box_buttons.add(Box.createRigidArea(new Dimension(10,10)));
  110.             box_buttons.add(btn_cancel);
  111.     }
  112.    
  113.     public int getResult()
  114.     {
  115.         return result;
  116.     }
  117.    
  118.     private class ResultYes extends AbstractAction
  119.     {
  120.  
  121.         /**
  122.          *
  123.          */
  124.         private static final long serialVersionUID = 1L;
  125.  
  126.         @Override
  127.         public void actionPerformed(ActionEvent arg0) {
  128.             // TODO Auto-generated method stub
  129.             result = 1;
  130.             setVisible(false);
  131.             dispose();
  132.         }
  133.        
  134.     }
  135.    
  136.     private class ResultNo extends AbstractAction
  137.     {
  138.  
  139.         /**
  140.          *
  141.          */
  142.         private static final long serialVersionUID = 1L;
  143.  
  144.         @Override
  145.         public void actionPerformed(ActionEvent e) {
  146.             // TODO Auto-generated method stub
  147.             result = 0;
  148.             setVisible(false);
  149.             dispose();
  150.         }
  151.        
  152.     }
  153.  
  154.     @Override
  155.     public void actionPerformed(ActionEvent e) {
  156.         // TODO Auto-generated method stub
  157.        
  158.     }
  159.    
  160.     //setVisible(false);
  161.     //dispose();
  162.    
  163.     private void addCustomKeyMaps()
  164.     {
  165.         InputMap inputMap = getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
  166.         ActionMap actionMap = getRootPane().getActionMap();
  167.        
  168.         inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "returnyes");
  169.         actionMap.put("returnyes", new ResultYes());
  170.        
  171.         inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "returnno");
  172.         actionMap.put("returnno", new ResultNo());
  173.     }
  174.    
  175.     private class buttonfocusEventHandler extends FocusAdapter {
  176.  
  177.           /** Checks buttons on dialog for focus
  178.            * and makes that button the default
  179.            *
  180.            * @param evt Holds event
  181.            */
  182.           public void focusGained(FocusEvent evt) {
  183.      
  184.              JButton button = (JButton) evt.getSource();
  185.              if (button instanceof JButton) getRootPane().setDefaultButton(button);
  186.              
  187.           }            
  188.     }  //  end buttonfocusEventHandler
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement