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

Thread: GUI: JButtons aren't visible until clicked

  1. #1
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default GUI: JButtons aren't visible until clicked

    I am making a simple GUI that will graph one of three parent functions (linear, quadratic, cubic) onto a graph based on the user's selection.

    Looking at my code, I don't really have an issue with anything showing up except for the quadratic and cubic buttons. For some reason, they are invisible until you click the exact spot that they are located. I've never had this issue... Any help is greatly appreciated!

     
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class main extends JFrame{
     
    	functions functions = new functions();
    	int z = 11;
    	int y = 11;
    	JPanel panel = new JPanel();
    	JButton linear, quadratic, cubic;
     
     
     
    	public main(){
    		setLayout(new FlowLayout());
     
    		panel = new JPanel();
    		panel.setLayout(new GridLayout(1,3));
    		add(panel);
     
    		linear = new JButton("Linear");
    		panel.add(linear);
     
    		quadratic = new JButton("Quadratic");
    		panel.add(quadratic);
     
    		cubic = new JButton("Cubic");
    		panel.add(cubic);
    	}
     
    	public void paint(Graphics g){
    		for(int x = 0; x<20;x++){
    			g.setColor(Color.DARK_GRAY);
    			g.drawLine(z, 0, z, 500);
    			z += 20;
    		}
    		for(int x = 0; x<20;x++){
    			g.setColor(Color.DARK_GRAY);
    			g.drawLine(0, y, 500, y);
    			y += 20;
    		}
    		g.setColor(Color.BLACK);
    		g.drawLine(250, 0, 250, 500);
     
    		g.setColor(Color.BLACK);
    		g.drawLine(0, 250, 500, 250);
    	}
     
     
     
    	public static void main(String args[]){
    		main frame = new main();
    		frame.setBackground(Color.WHITE);
    		frame.setSize(500,500);
    		frame.setVisible(true);
    		frame.setResizable(false);
    		frame.setTitle("Graph");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    	}
    }
    Simplicity calls for Complexity. Think about it.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: GUI: JButtons aren't visible until clicked

    What does the functions class do?
    The code works for me when I comment out the functions line

    Just recompiled and now it doesn't work the same way.

    If I rename the paint method to paintXXX the buttons display OK.

    What do you want the paint method to do? Where is it to draw? Where do you want the buttons to show?
    Last edited by Norm; August 7th, 2011 at 12:40 PM.

  3. #3
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Re: GUI: JButtons aren't visible until clicked

    Quote Originally Posted by Norm View Post
    What does the functions class do?
    The code works for me when I comment out the functions line
    The functions class draws the parent functions onto the graph.. It can be removed for this example, but it still shows up with the quadratic and cubic buttons hidden.

    Quote Originally Posted by Norm
    If I rename the paint method to paintXXX the buttons display OK.
    It takes away the graph for me, but reveals the buttons okay as well.

    Quote Originally Posted by Norm
    What do you want the paint method to do? Where is it to draw? Where do you want the buttons to show?
    The paint method is supposed to create the graph, by creating 40 dark gray lines.. It is also supposed to draw 2 lines of origin in the color black..

    Apparently the paint method hides the two buttons. If it's commented out, then the buttons are revealed correctly. What does this mean?
    Simplicity calls for Complexity. Think about it.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: GUI: JButtons aren't visible until clicked

    Apparently the paint method hides the two buttons. If it's commented out, then the buttons are revealed correctly. What does this mean?
    Either you paint on the frame or you layout components on the frame. Not both
    Create a JPanel extension class, put your paintComponent method in that and add that to the frame.

  5. #5
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Re: GUI: JButtons aren't visible until clicked

    Quote Originally Posted by Norm View Post
    Either you paint on the frame or you layout components on the frame. Not both
    Create a JPanel extension class, put your paintComponent method in that and add that to the frame.
    Alright, I'm going to do that now. Can you explain why you can't both layout components and paint components on a JFrame?
    Simplicity calls for Complexity. Think about it.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: GUI: JButtons aren't visible until clicked

    why you can't both layout components and paint components on a JFrame?
    I suppose it is a question of who paints last. If the layout of the components is done first, and the painting after, the painter will win. If the painter goes first then the layout will win.

    Doing both on the same component doesn't make any sense to me.

Similar Threads

  1. Adding JButtons to Frame
    By iKlaush in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 12th, 2011, 10:52 AM
  2. Replies: 33
    Last Post: May 25th, 2011, 06:59 AM
  3. button update when clicked
    By prettynew in forum AWT / Java Swing
    Replies: 4
    Last Post: March 13th, 2011, 04:47 PM
  4. [SOLVED] Executable .jar file isn’t launched after being double-clicked
    By voltaire in forum Java Theory & Questions
    Replies: 6
    Last Post: May 18th, 2010, 03:37 PM
  5. [SOLVED] JTextField not visible in swing
    By Sterzerkmode in forum AWT / Java Swing
    Replies: 4
    Last Post: May 21st, 2009, 07:37 AM