Advertisement
Guest User

Test_Disable_JFrame

a guest
Apr 21st, 2013
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.47 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.GridLayout;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.awt.event.KeyEvent;
  6.  
  7. import javax.swing.*;
  8.  
  9.  
  10. public class MainWindow extends JFrame{
  11.  
  12.     private JButton button1;
  13.     private JTextField textField;
  14.    
  15.     private JMenuBar menuBar;
  16.  
  17.     private JPanel contentPane;
  18.    
  19.     public MainWindow () {
  20.         super("Main window");
  21.         initComponents();
  22.         }
  23.    
  24.     private void initComponents(){
  25.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  26.        
  27.         contentPane = new JPanel(new GridLayout(1,2));
  28.         contentPane.setOpaque(true);
  29.        
  30.         //We add one button
  31.         initButton1();
  32.         contentPane.add(button1);
  33.        
  34.         //We add another one button
  35.         textField = new JTextField("Enable");
  36.         contentPane.add(textField);
  37.        
  38.         //We add a MenuBar
  39.         initMenuBar();
  40.         this.setJMenuBar(menuBar);
  41.        
  42.         //Now, we can add the contentPane to the JFrame
  43.         this.setContentPane(contentPane);
  44.     }
  45.    
  46.     private void initButton1() {
  47.         button1 = new JButton("Disable Frame");
  48.         button1.addActionListener(new ActionListener (){
  49.             public void actionPerformed(ActionEvent arg0) {
  50.                 MainWindow.this.setEnabled(false);
  51.                 MainWindow.this.textField.setText("Disabled");
  52.                 // MainWindow.this.contentPane.setEnabled(false); //This don't disable any component.
  53.                 MainWindow.this.menuBar.setEnabled(true); //This doesn't allow to use menuBar
  54.                 }
  55.             }
  56.         );
  57.     }
  58.    
  59.     private void initMenuBar(){
  60.         menuBar = new JMenuBar();
  61.         JMenu file = new JMenu("File");
  62.        
  63.         file.add(new JSeparator());
  64.        
  65.         JMenuItem quit = new JMenuItem("QUIT",KeyEvent.VK_Q);
  66.         quit.addActionListener(new ActionListener() {
  67.             public void actionPerformed(ActionEvent arg0) {System.exit(0);}}
  68.         );
  69.         file.add(quit);
  70.        
  71.         menuBar.add(file);
  72.  
  73.     }
  74.    
  75.        /**
  76.      * Create the GUI and show it.  For thread safety,
  77.      * this method should be invoked from the
  78.      * event-dispatching thread.
  79.      */
  80.     private static void createAndShowGUI() {
  81.         //Create and set up the window.
  82.         MainWindow test = new MainWindow();
  83.  
  84.         //Display the window.
  85.         test.pack();
  86.         test.setVisible(true);
  87.     }
  88.  
  89.     public static void main(String[] args) {
  90.         //Schedule a job for the event-dispatching thread:
  91.         //creating and showing this application's GUI.
  92.         javax.swing.SwingUtilities.invokeLater(new Runnable() {
  93.             public void run() {
  94.                 createAndShowGUI();
  95.             }
  96.         });
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement