Results 1 to 10 of 10
Thread: what does this method do???
- 07-30-2010, 09:45 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 42
- Rep Power
- 0
what does this method do???
Java Code:SwingUtilities.invokeLater(new Runnable() { public void run() {
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); } } }
-
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.
- 07-30-2010, 10:01 PM #3
Member
- Join Date
- Jun 2010
- Posts
- 42
- Rep Power
- 0
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?
- 07-30-2010, 10:15 PM #4
Senior Member
- Join Date
- Nov 2009
- Posts
- 236
- Rep Power
- 11
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();
-
- 07-30-2010, 10:33 PM #6
Member
- Join Date
- Jun 2010
- Posts
- 42
- Rep Power
- 0
i forgot to ask, what is the GUI method?? i tested the program without it and it is working!!!
- 07-30-2010, 10:40 PM #7
Senior Member
- Join Date
- Nov 2009
- Posts
- 236
- Rep Power
- 11
This is the GUI method
Java Code:void showGraphicUserInterface() { setTitle("Note Pad"); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); setSize(500,500); }
- 07-30-2010, 10:42 PM #8
Member
- Join Date
- Jun 2010
- Posts
- 42
- Rep Power
- 0
yes i know but i can run the program without it why should i use it?? is there any thing special about it??
- 07-30-2010, 10:53 PM #9
Senior Member
- Join Date
- Nov 2009
- Posts
- 236
- Rep Power
- 11
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.
- 07-30-2010, 10:59 PM #10
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 10
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
-
Turning Recursion Method into Iterative method
By mattakuevan in forum New To JavaReplies: 9Last Post: 06-15-2010, 07:46 AM -
method not abstract, does not override actionperformed method.
By Theman in forum New To JavaReplies: 2Last Post: 03-26-2010, 06:12 PM -
ArrayLists compareTo method, equals method
By random0munky in forum New To JavaReplies: 2Last Post: 10-26-2009, 08:20 PM -
Calling a method in a different class from within a method problem
By CirKuT in forum New To JavaReplies: 29Last Post: 09-25-2008, 08:55 PM -
cannot call private method from static method
By jon80 in forum New To JavaReplies: 3Last Post: 05-07-2008, 09:37 AM
Bookmarks