tags:

views:

47

answers:

4

OK so I have this applet thats like this

  • BorderLayout.CENTER - (Within this is JPanel)
  • BorderLayout.EAST - (Within this is a new GridLayout (4,5)
  • BorderLayout.SOUTH - (Within this is a TextArea)

Anyway, on the applet, I have to HOVER over the buttons to see them. They don't paint there I guess but I'm adding them in the init() method... so I don't know what I am doing wrong and why it's doing this.

setLayout( new BorderLayout() );
JPanel invOne = new JPanel(new GridLayout(5,4));
JPanel game = new JPanel();
add(invOne, BorderLayout.EAST);
add(game, BorderLayout.CENTER);
add(c, BorderLayout.SOUTH);

invOne.setBounds(416,0, 60, 28);

for (int i = 0,  j = 20;  i < 20;  i = i+1, j = j-1)  {
   invOne.add(new JButton("SLOT " + j));
   invOne.setBounds(32,32,100,100);
   invOne.setFocusable(false);
}

game.setBounds(0,0, 416, 288);
repaint();
+1  A: 

After adding all your components in the panel, do you explicitely call the "pack()" (or the "repaint()") method ? Not calling theses methods can result in graphic troubles in your Frames...

Benoit Courtine
OK, I added the code. As you can see I added repaint() but to no luck... (By the way, it's an applet not a Frame)
Dan
+1  A: 

What are you trying to accomplish with all the setBounds() calls? Either you let pack() set your panel's size according to what's inside, or you set bounds once to where you want to see that panel sit. Especially the calls with a size of 32x32 pixels are not helping at all.


EDIT:

I found these problems:

  • As one other poster mentioned, you're mixing Swing and AWT components. That doesn't work well. Essentially, if some of the components you use have a "J" at the beginning, you'll want to go with "J"'s for all of them. AWT is now considered "old school". It's a bit confusing because some classes and components used in GUIs don't have J's. I guess you need to work carefully with good examples or look the classes up.

  • For some reason, the applet didn't want to work well until I gave explicit row/column counts to the TextArea (now called JTextArea). I changed new TextArea() to new JTextArea(3,20).

  • The biggest problem may have been the empty paint() method. I wonder how the applet displayed anything at all? You could have removed the paint() method; I fixed it by calling super.paint().

  • Finally, class names (such as bl) should start with uppercase characters. The compiler in IdeOne grumbled at me for that.

Here's my fixed code.

Happy hacking!

Carl Smotricz
OK here is code itSELF... http://www.dasdas.pastebin.com/ftwpx7rK It just has the GUI... and when I tried compiling I get: C:\Documents and Settings\djasnowski\My Documents\bl.java:33: cannot find symbolsymbol : method pack()location: class bl this.pack();
Dan
I hope to have it fixed soon. I'll report back.
Carl Smotricz
I got it. I changed Applet to JApple but now the grey jPanel is overlapping the drawn tiles (drawImage) tiles...
Dan
Updated my post. Read my comments carefully.
Carl Smotricz
Great! But my image is still being overlapped by a JPanel. I assume JPanel over laps image for whatever reason I do not know about :S
Dan
Is the component you're showing the image in large enough to show the whole image?
Carl Smotricz
I think so. It's a 32x32 image and the JPanel it's in is 12x8 of 32x32 sqaure tiles.
Dan
Hmm. Sorry, I'll have to go soon. So: You know the drill, please post a new question on SO with "why are my images being overlaid?", post your complete code and with luck someone will be along to fix it.
Carl Smotricz
+1  A: 

Found one page (in german language) which describes the same problem: JButton widgets only show up after hovering over them.

The problem there was that AWT and Swing components/widgets have been mixed. I can't see from your code fragment if this is the case, but if you have java.awt.* imports, disable them, refactor your code to only use Swing classes and try again / hope for the best.

Another suggestion was to explicitely do a setVisible(true) for every button, but the questioner said, that this didn't help in his case.

Andreas_D
+1  A: 

You are using Swing components in an Applet. You should use JApplet. Just change extends Applet to extends JApplet.

tulskiy