Author Topic: Java Swing Doubt  (Read 4429 times)

0 Members and 1 Guest are viewing this topic.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Java Swing Doubt
« 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?

Ashbad

  • Guest
Re: Java Swing Doubt
« Reply #1 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 ;)

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Java Swing Doubt
« Reply #2 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.

Ashbad

  • Guest
Re: Java Swing Doubt
« Reply #3 on: February 25, 2011, 01:53:44 pm »
interesting, it looks like you're doing it correctly...

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Java Swing Doubt
« Reply #4 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?

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Java Swing Doubt
« Reply #5 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

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Java Swing Doubt
« Reply #6 on: February 25, 2011, 03:06:41 pm »
hah yay :D glad I was able to help ^^

Offline jnesselr

  • King Graphmastur
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2270
  • Rating: +81/-20
  • TAO == epic
    • View Profile
Re: Java Swing Doubt
« Reply #7 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)

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Java Swing Doubt
« Reply #8 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.

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: Java Swing Doubt
« Reply #9 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.