My JButton Is Hiding Itself

Discussion in 'Mac Programming' started by bobber205, Dec 28, 2005.

  1. macrumors 68020

    bobber205

    Joined:
    Nov 15, 2005
    Messages:
    2,182
    Location:
    Oregon
    #1
    http://www.alexwait.com/Java/ImageDisplay.jar


    Please download that Jar file. So far all it does it add a button to the JFrame.

    But there's a weird happening. The button doesn't always appear.
    Once in awhile it will appear immediately.
    But not often.

    But if I immediately press where it should be, it comes up and stays.
    I tried putting it "changeButton.setVisible(true)" in, but it didn't help.

    Any ideas at all????

    The entire code is below.


    Code:
    /**
     * Write a description of class ImageDisplay here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
    
    
    public class ImageDisplay extends JFrame //implements ActionListener
    {
        
    
    
    
    
    ImageDisplay(String title) {
        //super(title);
        
        CreateTheGUI();
        SetWindowListeners();
        
        JFrame mainFrame = new JFrame("Alex");
        mainFrame.setSize(250,301);
        mainFrame.setResizable(false); //don't let the user mess with the window
        mainFrame.setVisible(true);
    
        Dimension changeButtonD = new Dimension(40,40);
        
        
        JButton changeButton = new JButton("Change Image");
        changeButton.setSize(40,100);
        changeButton.setMaximumSize(changeButtonD);
        changeButton.setToolTipText("main Button this is");
        changeButton.setHorizontalAlignment(SwingConstants.RIGHT);
        changeButton.setVisible(true);
        mainFrame.getContentPane().add(changeButton);
        
        
    }
    
    
    
    
    public static void CreateTheGUI() { //just a basic method
        System.out.println("Creating The GUI!(3)"); 
      
    }
    
    
    public static void SetWindowListeners() {
    
    
    
    
    
    }
    
    
    public static void main(String[] args) {
        ImageDisplay app = new ImageDisplay("ImageDisplay App");
    }
    
    
    
    
    
    
        
    }
    
    
    
    
     
  2. macrumors regular

    Joined:
    Jul 20, 2005
    Messages:
    229
    #2
    i don't have a solution, but i can verify that it did the same thing on my machine. Thats pretty weird.
     
  3. macrumors regular

    Joined:
    Jan 3, 2004
    Messages:
    195
    #3
    Try This Code

    Basically I changed up the style a little, used the JFrame class that you are extending instead of creating a seperate one, moved the setVisiable to the end (that should fix your display problem).

    Let me know how it works.

    Code:
    /**
     * Write a description of class ImageDisplay here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
    
    
    public class ImageDisplay extends JFrame //implements ActionListener
    {
        
    
    	public ImageDisplay(String title) 
    	{
    	    //super(title);
    	    
    	    CreateTheGUI();
    	    SetWindowListeners();
    	    
    	    this.setSize(250,301);
    	    this.setResizable(false); //don't let the user mess with the window
    		this.setLayout(null);
    		Dimension changeButtonD = new Dimension(40,40);
        
        
     	    JButton changeButton = new JButton("Change Image");
     	    changeButton.setSize(40,100);
     	    changeButton.setMaximumSize(changeButtonD);
     	    changeButton.setToolTipText("main Button this is");
     	    changeButton.setHorizontalAlignment(SwingConstants.RIGHT);
     	    changeButton.setVisible(true);
     	   
     	    this.getContentPane().add(changeButton);
            this.setVisible(true);
    
        
    }
    
    
    
    
    public static void CreateTheGUI() { //just a basic method
        System.out.println("Creating The GUI!(3)"); 
      
    }
    
    
    public static void SetWindowListeners() {
    
    
    
    
    
    }
    
    
    public static void main(String[] args) {
        ImageDisplay app = new ImageDisplay("ImageDisplay App");
    }
    
    
    
    
    
    
        
    }
    
     
  4. macrumors 68020

    bobber205

    Joined:
    Nov 15, 2005
    Messages:
    2,182
    Location:
    Oregon
    #4
    Yay!

    It worked. Thanks so much.
    The only thing I had to change was

    Code:
    this.setLayout(null);
    to

    Code:
    this.getContentPane().setLayout(null);
    But it's working great now.
    Thanks again!:D
     
  5. macrumors 68020

    bobber205

    Joined:
    Nov 15, 2005
    Messages:
    2,182
    Location:
    Oregon
    #5
    How do I make my buttons look like regular Mac OS X buttons?
     
  6. macrumors member

    Joined:
    Jan 2, 2005
    Messages:
    87
    #6
    be carefull when setting the layout to null, i have had some pretty wierd things happen that were hard to figure out what the problem was.
     
  7. macrumors 68020

    bobber205

    Joined:
    Nov 15, 2005
    Messages:
    2,182
    Location:
    Oregon
    #7
    Maybe you wonderful folks here can explain this to me.

    Why when I commented out the setLayout to null line, my button suddenly became as large as the window?:confused:
     
  8. macrumors 601

    HiRez

    Joined:
    Jan 6, 2004
    Messages:
    4,861
    Location:
    Western US
    #8
    It's been a long time since I've done anything in Java, but I assume that not specifically setting a layout probably invokes a default layout mode for the contents (in this case, your button) to fill the available frame.

    EDIT: Note that setting the layout to null is not the same as not setting one at all; you would be removing the default one in the case of assigning null.
     
  9. macrumors regular

    Joined:
    Jan 3, 2004
    Messages:
    195
    #9
    import java.util.FlowLayout

    then do this.getContentPane().setLayout(new FlowLayout());
     
  10. macrumors 68020

    bobber205

    Joined:
    Nov 15, 2005
    Messages:
    2,182
    Location:
    Oregon
    #10
    It worked. Thanks very much.
    Have any idea why my button is being centered and not put on the right?
     
  11. macrumors regular

    Joined:
    Jan 3, 2004
    Messages:
    195
    #11
    You could do
    this.getContentPane().setLayout(new FlowLayout(FlowLayout.RIGHT));
     

Share This Page