Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: Munchor on February 25, 2011, 01:50:15 pm

Title: Java Swing Doubt
Post by: Munchor on February 25, 2011, 01:50:15 pm
Code: [Select]
package gamepanel;

import javax.swing.*;

public class Main {
    public static void main(String[] args) {
        JFrame loadingBarFrame = new JFrame();
        loadingBarFrame.setSize(400,100);
        loadingBarFrame.setVisible(true);
        JProgressBar progressBar = new JProgressBar();
        progressBar.setVisible(true);
        progressBar.setLocation(5,5);
        progressBar.setSize(1000,1000);
        progressBar.setValue(0);
        progressBar.setStringPainted(true);
    }
}

I have this code which is supposed to create a simple progress bar and a frame, however, the progress bar doesn't appear for some obvious reason I'm not aware of, any ideas?
Title: Re: Java Swing Doubt
Post by: Ashbad on February 25, 2011, 01:51:50 pm
are you sure that it is rendering the screen correctly?  you see, if you were using netbeans instead it has a built in editor that lets you actually place the buttons and bars and such manually in a graphical way ;)
Title: Re: Java Swing Doubt
Post by: Munchor on February 25, 2011, 01:52:49 pm
are you sure that it is rendering the screen correctly?  you see, if you were using netbeans instead it has a built in editor that lets you actually place the buttons and bars and such manually in a graphical way ;)

I know, but I'm not using that. I'm programming the GUI, I only use that for big frames and complicated stuff.
Title: Re: Java Swing Doubt
Post by: Ashbad on February 25, 2011, 01:53:44 pm
interesting, it looks like you're doing it correctly...
Title: Re: Java Swing Doubt
Post by: Builderboy on February 25, 2011, 01:54:19 pm
Hmmm it seems that you create the progress bar, but it never interacts with the loadingBarFrame.  I am not very familiar with swing components, do you need to add it into the frame or whatnot?
Title: Re: Java Swing Doubt
Post by: Munchor on February 25, 2011, 02:10:58 pm
Hmmm it seems that you create the progress bar, but it never interacts with the loadingBarFrame.  I am not very familiar with swing components, do you need to add it into the frame or whatnot?

Thanks, the .add( method did it ;D
Title: Re: Java Swing Doubt
Post by: Builderboy on February 25, 2011, 03:06:41 pm
hah yay :D glad I was able to help ^^
Title: Re: Java Swing Doubt
Post by: jnesselr on February 25, 2011, 10:28:54 pm
Yeah, and I would be careful with just using .add(.  In fact, what I do is create what I want in netbeans and study the code for it.

Also, Builderboy, you have +256 respect (2^8), and are 17 posts away from 4096 (2^12)
Title: Re: Java Swing Doubt
Post by: Munchor on February 26, 2011, 02:20:02 am
Yeah, and I would be careful with just using .add(.  In fact, what I do is create what I want in netbeans and study the code for it.

Also, Builderboy, you have +256 respect (2^8), and are 17 posts away from 4096 (2^12)

I do hate add(, so in the tiny program I just made (Catalan Number GUI) I decided to use the NetBeans IDE, but the designer is quite bad when compared to Visual Studio's or Sharp Develop's. Sometimes the window resizes itself, and it won't let me make what I want.
Title: Re: Java Swing Doubt
Post by: nemo on February 26, 2011, 01:55:50 pm
in this situation the code would be
Code: [Select]
loadingBarFrame.getContentPane().add(progressBar);

however, because the java swing designers realized having to call getContentPane() everytime you want to add a component to a frame, they made it so loadingBarFrame.add(progressBar); will automatically add progressBar to the content pane. learning to use a layout like SpringLayout is the best solution.