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 3 of 3

Thread: Why do my changes not show up?

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    17
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Why do my changes not show up?

    Hey everyone!

    I have some problem i don't really understand the source.

    Unfortunately i don't have a runnable example because this is part of a more complex structure but maybe it is self-explaining.
    My problem is: i want a red area to appear when i click the button start, which doesn't work. And i don't really understand why it doesn't work, because i know (i tested it with a simple system output) that my testroutine is invoked by clicking my start-menuitem. When i add the same three lines (as in the method) in my constructor i get the visual result i want, so i figured that those 3 lines are not the problem. i tried a repaint in my testroutine which didn't work either.

    i'm thankful for help

    this is my code for this class:
        public class RTwindow extends MenuWindow {
    	public RTwindow(){
    		this.setExtendedState(MAXIMIZED_BOTH);
    		JMenu mainmenu = new JMenu();
    		JMenuItem start = new JMenuItem();
    		this.addmenu(mainmenu,"Mainmenu");     
    		this.addmenuitem(mainmenu, start, "Start");
    //addmenu and addmenuitem are methods of my class MenuWindow
    		start.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent AE){
    				testroutine();
    			}
    		});		
    	}
     
    	public void testroutine(){
    		JPanel mainpanel = new JPanel();
    		mainpanel.setBackground(Color.red);
    		add(mainpanel);
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Why do my changes not show up?

    Can you write a small complete program that compiles, executes and shows the problem?

    --- Update ---

    Have you read the API doc for the setBackground() method?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,459
    My Mood
    Mellow
    Thanks
    148
    Thanked 607 Times in 594 Posts

    Default Re: Why do my changes not show up?

    It's easy enough to create a short, runnable example to demonstrate a problem you're having with code. Saying it's too complex is just laziness. Here's 1) an example of how to do that, and 2) a demonstration of what I suspect is the source of your problem. Note the comment in the method buildJPanel(). If not, come back with a small, runnable example that demonstrates what you're seeing.
    import java.awt.Color;
    import java.awt.Dimension;
     
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
     
    // a simple class to demonstrate the effect of opacity on background color
    public class TestClass
    {
    	public TestClass()
    	{
    		JFrame mainFrame = new JFrame( "Test Frame" );
    		mainFrame.add( buildJPanel() );
    		mainFrame.setLocationRelativeTo( null );
    		mainFrame.pack();
    		mainFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    		mainFrame.setVisible( true );
    	}
     
    	public JPanel buildJPanel()
    	{
    		JPanel mainPanel = new JPanel();
     
    		// change the 'false' in the next line to 'true' to see what happens
    		mainPanel.setOpaque( false );
    		mainPanel.setBackground(Color.red);
    		mainPanel.setPreferredSize( new Dimension( 100, 100 ) );
    		return mainPanel;
    	}
     
    	// to launch TestClass on the EDT
    	public static void main( String[] args )
    	{
    		SwingUtilities.invokeLater( new Runnable()
    		{
    			public void run()
    			{
    				new TestClass();
    			}
    		});
    	}
    }

Similar Threads

  1. New frame should only show GUI once!
    By gerre in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 30th, 2012, 10:34 PM
  2. detect agent and show name.
    By aecosis in forum Java Theory & Questions
    Replies: 0
    Last Post: May 29th, 2012, 10:26 PM
  3. My bullseye won't show
    By JeremiahWalker in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 30th, 2012, 06:33 PM
  4. Hide/Show Form?
    By chickenhawk in forum What's Wrong With My Code?
    Replies: 11
    Last Post: July 1st, 2011, 06:26 AM
  5. Beginner: Show Image in Label when Results Show Up
    By Big Bundy in forum Java Theory & Questions
    Replies: 3
    Last Post: April 4th, 2011, 02:43 PM