Results 1 to 10 of 10
  1. #1
    EBBOOO is offline Member
    Join Date
    Jun 2010
    Posts
    42
    Rep Power
    0

    Default what does this method do???

    Java Code:
    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    could you please tell me what does this method exactly do???
    especially in this code

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    
    public class Notepad extends JFrame implements ActionListener {
        JTextArea myTextArea = new JTextArea ();
    
    public static void main (String [] args) {
    SwingUtilities.invokeLater(new Runnable() {
    
    
    public void run() {
                Notepad frame = new Notepad ();
               frame.showGraphicUserInterface();
               
            AdjustmentListener adjustmentListener = new AdjustmentListener() {
            public void adjustmentValueChanged(AdjustmentEvent adjustmentEvent) {
            System.out.println("Adjusted: " + adjustmentEvent.getValue());
          }
        };
                
                JScrollBar myscrollbar1 = new JScrollBar(JScrollBar.HORIZONTAL);
                frame.add(myscrollbar1, BorderLayout.SOUTH);
                myscrollbar1.addAdjustmentListener(adjustmentListener);
                
                JScrollBar myscrollbar2 = new JScrollBar(JScrollBar.VERTICAL);
                frame.add(myscrollbar2, BorderLayout.EAST);
                myscrollbar2.addAdjustmentListener(adjustmentListener);
    
            } 
            }
        );
    
    
    }
    
    
    public Notepad () {
    
        Container myPane = getContentPane();
    
        
        setLayout(new BorderLayout());
        myPane.add(myTextArea, BorderLayout.CENTER);
        
        JMenuBar myMenu = new JMenuBar();
        setJMenuBar(myMenu);
        
        JMenu file = new JMenu("file");
        myMenu.add(file);
        
        JMenuItem open = new JMenuItem("Open");
        file.add(open);
        open.addActionListener(this);
        
        JMenuItem saveAs = new JMenuItem("Save As");
        file.add(saveAs);
        saveAs.addActionListener(this);
        
        JMenuItem exit = new JMenuItem("Exit");
        file.add(exit);
        exit.addActionListener(this);
        
            JMenu edit = new JMenu("Font Size");
            myMenu.add(edit);
            
            JMenuItem undo = new JMenuItem("10");
            edit.add(undo);
            undo.addActionListener(this);
            
            JMenuItem copy = new JMenuItem("20");
            edit.add(copy);
            copy.addActionListener(this);
            
            JMenuItem paste = new JMenuItem("30");
            edit.add(paste);
            paste.addActionListener(this);
            
            JMenuItem cut = new JMenuItem("40");
            edit.add(cut);
            cut.addActionListener(this);
            
            JMenu BackgroundColor = new JMenu("Background Color");
            myMenu.add(BackgroundColor);
            
            JMenuItem red = new JMenuItem("Red");
            BackgroundColor.add(red);
            red.addActionListener(this);
            red.setBackground(Color.red);
            
            JMenuItem blue = new JMenuItem("Blue");
            BackgroundColor.add(blue);
            blue.addActionListener(this);
            blue.setBackground(Color.blue);
            
            JMenuItem lightGray = new JMenuItem("Light Gray");
            BackgroundColor.add(lightGray);
            lightGray.addActionListener(this);
            lightGray.setBackground(Color.lightGray);
            
            JMenuItem cyan = new JMenuItem("Cyan");
            BackgroundColor.add(cyan);
            cyan.addActionListener(this);
            cyan.setBackground(Color.cyan);
            
            JMenuItem white = new JMenuItem("White");
            BackgroundColor.add(white);
            white.addActionListener(this);
            white.setBackground(Color.white);
            
            
            	Font myFont = new Font("Serif",Font.ITALIC,15);
            	myTextArea.setFont(myFont);
    }
    
    void showGraphicUserInterface() {
        setTitle("Note Pad");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
        setSize(500,500);
    }
    
    public void actionPerformed(ActionEvent myEvent) {
        String file = myEvent.getActionCommand();
        String doc = " ";
        
            
            if (file.equals("Exit")){
                System.exit(0);
            }
            else if (file.equals("Open")){
                Notepad frame = new Notepad (); 
                
                String filename = File.separator + "tmp";
                JFileChooser fc = new JFileChooser(new File(filename));
                
                // Show open dialog; this method does not return until the dialog is closed
                fc.showOpenDialog(frame);
                File selFile = fc.getSelectedFile();
            }
            
                    else if (file.equals("Save As")){
                Notepad frame = new Notepad (); 
                
                String filename = File.separator + "tmp";
                JFileChooser fc = new JFileChooser(new File(filename));
                
                // Show open dialog; this method does not return until the dialog is closed
                fc.showSaveDialog(frame);
                File selFile = fc.getSelectedFile();
            }
            
                             else if (file.equals("10")){
                                
                             Font myFont10 = new Font("Serif",Font.ITALIC,10);
                             myTextArea.setFont(myFont10);   
            }
        
                             else if (file.equals("20")){
                                
                             Font myFont20 = new Font("Serif",Font.ITALIC,20);
                             myTextArea.setFont(myFont20);   
            }
                             else if (file.equals("30")){
                                
                             Font myFont30 = new Font("Serif",Font.ITALIC,30);
                             myTextArea.setFont(myFont30);   
            }
                             else if (file.equals("40")){
                                
                             Font myFont40 = new Font("Serif",Font.ITALIC,40);
                             myTextArea.setFont(myFont40);   
            }
                             else if (file.equals("Red")){
                                myTextArea.setBackground(Color.red);
                                             
            }
                             else if (file.equals("Blue")){
                                myTextArea.setBackground(Color.blue);
                                             
            }
                             else if (file.equals("Light Gray")){
                                myTextArea.setBackground(Color.lightGray);
                                             
            }
            
                             else if (file.equals("Cyan")){
                                myTextArea.setBackground(Color.cyan);
                                             
            }
                             else if (file.equals("White")){
                                myTextArea.setBackground(Color.white);
                                             
            }
    
                
        }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    It makes sure that your Swing code is run on the Swing thread. Doing this in this situation will help prevent rare program crashes that only happen when you're demo'ing your code for your boss, teacher or a customer.

  3. #3
    EBBOOO is offline Member
    Join Date
    Jun 2010
    Posts
    42
    Rep Power
    0

    Default

    thanks Fubarable.
    but why only the scroll bar is in this method???
    and could you please tell me how it prevent these rare crashes to happen?

  4. #4
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    Not just the scrollbars are in the method. It also creates the Notepad object and call the showGUI method on it:
    Java Code:
    public void run() {
                Notepad frame = new Notepad ();
               frame.showGraphicUserInterface();

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by EBBOOO View Post
    thanks Fubarable.
    but why only the scroll bar is in this method???
    and could you please tell me how it prevent these rare crashes to happen?
    Agree with Collin. Please look carefully at your code since your whole Swing application is called from within the run method of the Runnable. This is where careful code indenting is crucial, and why you must clean up your indentation habits.

  6. #6
    EBBOOO is offline Member
    Join Date
    Jun 2010
    Posts
    42
    Rep Power
    0

    Default

    i forgot to ask, what is the GUI method?? i tested the program without it and it is working!!!

  7. #7
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    This is the GUI method

    Java Code:
    void showGraphicUserInterface() {
        setTitle("Note Pad");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
        setSize(500,500);
    }

  8. #8
    EBBOOO is offline Member
    Join Date
    Jun 2010
    Posts
    42
    Rep Power
    0

    Default

    yes i know but i can run the program without it why should i use it?? is there any thing special about it??

  9. #9
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    Sure you can run it, but you won't be able to see the JFrame, because in that method, it calls setVisible(true); which allows you to see the JFrame.

  10. #10
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    First program is much more stable.
    Second this is a part of a very good programming that brings better usage of the CPU and memory resources. It prevents memory and cpu time lost.

    When you want to do lot of things in your program at the same time you don't want to do 1.000 things immediately, but you must said well for this thing I want to give to it some resources and this thing must wait a little bit until some other thing is done and then I shall call it and so on.

Similar Threads

  1. Turning Recursion Method into Iterative method
    By mattakuevan in forum New To Java
    Replies: 9
    Last Post: 06-15-2010, 06:46 AM
  2. Replies: 2
    Last Post: 03-26-2010, 05:12 PM
  3. ArrayLists compareTo method, equals method
    By random0munky in forum New To Java
    Replies: 2
    Last Post: 10-26-2009, 07:20 PM
  4. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  5. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •