Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

bobber205

macrumors 68020
Original poster
Nov 15, 2005
2,182
1
Oregon
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");
}






    
}
 

ldenman

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

blasto333

macrumors regular
Jan 3, 2004
247
2
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");
}






    
}
 

bobber205

macrumors 68020
Original poster
Nov 15, 2005
2,182
1
Oregon
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
 

TrumanApple

macrumors member
Jan 2, 2005
87
0
bobber205 said:
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

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.
 

bobber205

macrumors 68020
Original poster
Nov 15, 2005
2,182
1
Oregon
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:
 

HiRez

macrumors 603
Jan 6, 2004
6,253
2,579
Western US
bobber205 said:
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:
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.
 

blasto333

macrumors regular
Jan 3, 2004
247
2
import java.util.FlowLayout

then do this.getContentPane().setLayout(new FlowLayout());
 

bobber205

macrumors 68020
Original poster
Nov 15, 2005
2,182
1
Oregon
It worked. Thanks very much.
Have any idea why my button is being centered and not put on the right?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.