Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Refreshing from teh main class

  1. #1
    Member
    Join Date
    Feb 2012
    Location
    Azle/Arlington
    Posts
    31
    My Mood
    Torn
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Refreshing from teh main class

    Is there a way to refresh a graphic from the main method? What i have is


     
    public static void main(String ad[])
        {
    	// Create the menu bar
    	JMenuBar menuBar = new JMenuBar();
     
    	// Create a menu
    	JMenu file = new JMenu("File");
    	JMenu menu = new JMenu("Theme");
    	menuBar.add(file);
    	menuBar.add(menu);
     
    	// Create a menu item
    	JMenuItem exit = new JMenuItem("exit");
    	JMenuItem def = new JMenuItem ("Updates on next move");
    	JMenuItem item = new JMenuItem("Xmas");
    	JMenuItem BB = new JMenuItem("Black and Blue");
    	JMenuItem RB = new JMenuItem("Black and Red");
    	JMenuItem WB = new JMenuItem("White and Blue");
    	file.add(exit);
    	menu.add(def);
    	menu.add(item);
    	menu.add(BB);
    	menu.add(RB);
    	menu.add(WB);
    	exit.addActionListener(new ActionListener() {
     
                public void actionPerformed(ActionEvent e)
                { System.exit(0); }
            });
    	def.addActionListener(new ActionListener() {
     
                public void actionPerformed(ActionEvent e)
                {
    			r1 = 0;		r2 = 255;
    			g1 = 0;     g2 = 255;
    			b1 = 0;		b2 = 255;
     
    			ThemeBC = new Color(r1,g1,b1);
    			ThemeF = new Color(r2,g2,b2);
    			}
            });
     
    	WB.addActionListener(new ActionListener() {
     
                public void actionPerformed(ActionEvent e)
                {
    			r1 = 255;		r2 = 0;
    			g1 = 255;     	g2 = 0;
    			b1 = 255;		b2 = 255;
     
    			ThemeBC = new Color(r1,g1,b1);
    			ThemeF = new Color(r2,g2,b2);
    			}
            });
         BB.addActionListener(new ActionListener() {
     
                public void actionPerformed(ActionEvent e)
                {
    			r1 = 0;	r2 = 0;
    			g1 = 0;     g2 = 0;
    			b1 = 0;		b2 = 255;
     
    			ThemeBC = new Color(r1,g1,b1);
    			ThemeF = new Color(r2,g2,b2);
    			}
            });
         RB.addActionListener(new ActionListener() {
     
                public void actionPerformed(ActionEvent e)
                {
    			r1 = 0;		r2 = 255;
    			g1 = 0;     g2 = 0;
    			b1 = 0;		b2 = 0;
     
    			ThemeBC = new Color(r1,g1,b1);
    			ThemeF = new Color(r2,g2,b2);
    			}
            });
    	item.addActionListener(new ActionListener() {
     
                public void actionPerformed(ActionEvent e)
                {
    			r1 = 255;	r2 = 0;
    			g1 = 0;     g2 = 128;
    			b1 = 0;		b2 = 0;
     
    			ThemeBC = new Color(r1,g1,b1);
    			ThemeF = new Color(r2,g2,b2);
    			}
            });
     
    	// Install the menu bar in the frame
    		JFrame jwill = new JFrame();
            JDub a=new JDub();
            jwill.getContentPane().add(a, BorderLayout.CENTER);
            jwill.setSize(new Dimension(700,700));
            jwill.setResizable(false);
            jwill.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            jwill.setVisible(true);
            jwill.setJMenuBar(menuBar);
     
     
        }//end of main class

    thats only a snippet of my code (the main class) The variables are all private(global variables)

    What i want if for when 1 of the action listeneres are performed it updated my graphic to show the new color layout of the action listener. so how could i force it to update without waiting till the next (action is performed on screen) i have a game that goes with the code and it runs the problem is waiting on the swap method to be called to update the screen with the new themes.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,358
    Thanks
    182
    Thanked 834 Times in 777 Posts
    Blog Entries
    5

    Default Re: Refreshing from teh main class

    I might be the only one, but I have no clue what you are asking. Do you want to just repaint a component? Call repaint() from the appropriate location, or perform the appropriate method calls to update the user interface.

  3. #3
    Member
    Join Date
    Feb 2012
    Location
    Azle/Arlington
    Posts
    31
    My Mood
    Torn
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Refreshing from teh main class

    I want the entire scren to refresh when 1 of the actionlisteners are activated.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,358
    Thanks
    182
    Thanked 834 Times in 777 Posts
    Blog Entries
    5

    Default Re: Refreshing from teh main class

    Quote Originally Posted by J-Will View Post
    I want the entire scren to refresh when 1 of the actionlisteners are activated.
    Did you call repaint on the parent container? If yes, and it didn't work, I recommend trimming your code down to an SSCCE to illustrate your issue more clearly

  5. #5
    Member
    Join Date
    Feb 2012
    Location
    Azle/Arlington
    Posts
    31
    My Mood
    Torn
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Refreshing from teh main class

    basically is there a way to run a non static method in a static location? I.E. the nonstatic method of repaint(); in the static method of the main class?

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,358
    Thanks
    182
    Thanked 834 Times in 777 Posts
    Blog Entries
    5

    Default Re: Refreshing from teh main class

    Quote Originally Posted by J-Will View Post
    basically is there a way to run a non static method in a static location? I.E. the nonstatic method of repaint(); in the static method of the main class?
    There shouldn't be a need to call these methods from the main method, which actually exists as soon as your code creates the GUI. Call it from the EDT, for instance within the actionPerformed method, or use a SwingTimer if you wish to do this at a later time and/or periodically. Again, I recommend posting an SSCCE and more clearly define your requirements to better illustrate the problem.

  7. #7
    Member
    Join Date
    Feb 2012
    Location
    Azle/Arlington
    Posts
    31
    My Mood
    Torn
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Refreshing from teh main class

    I would but im on a computer without a jdk so I can not get to the data right this second to see if it is compilable. It will be roughly 2 hours before i can create a SSCCE

Similar Threads

  1. [SOLVED] How to make a class be static if it's the main class.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 18
    Last Post: January 10th, 2012, 05:14 AM
  2. How do I call a class in a main class
    By Leprechaun_hunter in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 26th, 2011, 04:11 AM
  3. Creating a scaleUp main method in a new class
    By Brainz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 16th, 2010, 08:58 AM
  4. Main Class Not Found Problem
    By shadow in forum Java Theory & Questions
    Replies: 3
    Last Post: September 29th, 2009, 10:42 AM
  5. could not find the main class
    By Tisofa in forum Object Oriented Programming
    Replies: 1
    Last Post: September 27th, 2009, 03:58 AM