Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  This isn't part of a game, but could someone help me out with this?  (Read 1294 times)
0 Members and 1 Guest are viewing this topic.
Offline cubemaster21
« Posted 2012-10-16 22:00:06 »

I'm having trouble with Swing lately and it doesn't always want to display my components. One time it will, and the next... bupkis.
Here's the code:
the Body.class
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
package com.amx.main;

import java.awt.Container;

import javax.swing.JFrame;

public class Body {
   public static Body frame;
   public JFrame window;
   private Container container;
   public FormBase form;
   
   public static void main(String[] args){
      frame = new Body();
   }
   public Body(){
      container = new Container();
      window = new JFrame("AMX Registry");
      window.setSize(1000, 600);
      window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      window.setLocationRelativeTo(null);
      window.setResizable(false);
      window.setContentPane(container);
      window.setVisible(true);
      window.requestFocus();
      init();
   }
   private void init(){
      load68();
   }
   protected void load68(){
      new FormBase(this);
      System.out.println("Form loaded");
   }
   protected void load69(){
     
   }
   protected void load70(){
     
   }
   protected Container getContent(){
      return window.getContentPane();
   }
}

and the FormBase.class
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
package com.amx.main;

import javax.swing.*;

public class FormBase {
   private Body body;
   protected JTextArea VIN;
   protected JButton test;
   public FormBase(Body b){
      test = new JButton("TEST");
      VIN = new JTextArea("VIN NUMBER");
      this.body = b;
      addComponent(test,10,10,60,60);
      addComponent(VIN,10,80,300,20);
     
   }
   public void addComponent(JComponent c, int x,int y,int w,int h){
      c.setLocation(x, y);
      c.setSize(w, h);
      body.getContent().add(c);
   }
}

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline philfrei
« Reply #1 - Posted 2012-10-16 22:33:31 »

Do the System.out.println messages all print, and you still see nothing?

I don't know if this will help, but I've had better success with the setBounds method than setSize, when dealing with components on a JFrame. For the JFrame itself, I do use setSize, as you do.

Then again, I don't use JFrame as you do, in terms of dealing with the ContentPane and all. I find it simpler to have a JPanel in the JFrame, set to the same size (less border areas). It is pretty straightforward to add components to a JPanel. Perhaps there are additional benefits you are getting that I am unaware of. I'm not exactly a Swing whiz.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline cubemaster21
« Reply #2 - Posted 2012-10-16 22:57:35 »

The messages print, and the window comes up, however, there are no buttons or text boxes.

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sproingie

JGO Kernel


Medals: 202



« Reply #3 - Posted 2012-10-16 23:07:19 »

You're manipulating components outside the EDT, so anything could happen.  Also, you're not assigning your FormBase instance to anything -- I suspect you meant to assign it to the "form" field you have in Body.
Offline ReBirth
« Reply #4 - Posted 2012-10-17 00:39:41 »

Yeah the formbase is kinda not used there.

Offline cubemaster21
« Reply #5 - Posted 2012-10-17 00:41:36 »

yeah, i changed it so that the "form" field in the body class was equal to the new FormBase, but it still doesn't work.

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline cubemaster21
« Reply #6 - Posted 2012-10-19 18:35:55 »

Bumping because the problem is still not rectified.

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline Rorkien
« Reply #7 - Posted 2012-10-19 20:28:00 »

I'm not used to swing, but this is so strange...

The components appear if:
-you set window.setResizable to true and resize the window
-minimize the window and restore it
-mouse over the components location

EDIT: oh, i get it now. After you add the components, you got to call the repaint() method manually:

1  
2  
3  
4  
5  
   protected void load68(){
      new FormBase(this);
      window.repaint();
      System.out.println("Form loaded");
   }
Offline cubemaster21
« Reply #8 - Posted 2012-10-19 20:51:41 »

I added what you just said, but its still not working.

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline sproingie

JGO Kernel


Medals: 202



« Reply #9 - Posted 2012-10-19 21:07:31 »

When you add or remove components, you need to call revalidate(), not just repaint()

And again, you ought to be doing it on the EDT.  You can usually get away with going off the EDT before any UI is visible, but once it is, all bets are off.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline cubemaster21
« Reply #10 - Posted 2012-10-19 21:18:16 »

When you add or remove components, you need to call revalidate(), not just repaint()

And again, you ought to be doing it on the EDT.  You can usually get away with going off the EDT before any UI is visible, but once it is, all bets are off.

Huzzah! adding revalidate() worked. As for EDT, I'm still rather new to this have honestly don't have a clue what that is.

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline sproingie

JGO Kernel


Medals: 202



« Reply #11 - Posted 2012-10-19 21:27:37 »

The EDT is the Event Dispatch Thread, and anything that works directly with the UI has to run on the EDT.  Sometimes you can get away with not using it, but then it's always a gamble.

Start with http://docs.oracle.com/javase/tutorial/uiswing/start/index.html and pay close attention to the way it uses SwingUtilities.invokeLater -- that's how you get things to run on the EDT from outside it.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

BurntPizza (12 views)
2014-09-21 02:42:18

BurntPizza (11 views)
2014-09-21 01:30:30

moogie (13 views)
2014-09-21 00:26:15

UprightPath (23 views)
2014-09-20 20:14:06

BurntPizza (27 views)
2014-09-19 03:14:18

Dwinin (40 views)
2014-09-12 09:08:26

Norakomi (70 views)
2014-09-10 13:57:51

TehJavaDev (96 views)
2014-09-10 06:39:09

Tekkerue (49 views)
2014-09-09 02:24:56

mitcheeb (70 views)
2014-09-08 06:06:29
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!