Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Problems with rendering a jprogressbar

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problems with rendering a jprogressbar

    Hello forum,

    I'm a newbie in Java and I'm having problems with the rendering of UI components. I need some help to understand the problem and so avoid making it in the future. To make you get an idea of my problem, I'll show you an example of it.

    First at all, say that I'm using netbeans to drop a JButton and a JPanel over the JFrame. I have a class called 'Frame' which extends JFrame and it contains a JPanel and a JButton. I did remarkable the code written by netbeans with blue color.Here it is:

    public class Frame extends javax.swing.JFrame {

    /** Creates new form Frame */
    public Frame() {
    initComponents();
    this.p = new PanelOptions();
    this.jPanel1.add(p);
    this.jPanel1.validate();
    }
    private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jButton1 = new javax.swing.JButton();
    jLabel1 = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);

    jButton1.setText("Go to Card3");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton1ActionPerformed(evt);
    }
    });
    jPanel1.add(jButton1);

    jLabel1.setText("prueba 1");
    jPanel1.add(jLabel1);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addContainerGap()
    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 457, Short.MAX_VALUE)
    .addContainerGap())
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addContainerGap()
    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 341, Short.MAX_VALUE)
    .addContainerGap())
    );

    pack();
    }// </editor-fold>


    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    this.p.change_progressbar_value(50);
    this.p.change_text("Hello World");
    this.jPanel1.validate();
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new Frame().setVisible(true);
    }
    });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration
    PanelOptions p;}

    I have another class which extends JPanel and it has a JProgressBar called PanelOptions. I have two methods in PanelOptions class , one for changing the jlabel text and another one for changing the value of the JProgressBar.

    When I make a new PanelOptions object, its cosntructor should make the jprogressbar with an initial value of 50 over 100, but it is not display in the frame, instead I just get the jprogressbar empty. Of course, if I push the jbutton from the Frame class, I get nothing when the jButton1ActionPerformed should change the value of the jprogressbar and the label text from the PanelOptions class.

    Do you know what the problem is? I think that I miss some important core concepts about programming with java. Sorry for mistakes that I did writing in English. Thanks for those people who try to help me.

    Alberto

    public class PanelOptions extends javax.swing.JPanel {

    /** Creates new form PanelOptions */
    public PanelOptions() {
    initComponents();
    this.jProgressBar1 = new JProgressBar(0,100);
    this.jProgressBar1.setValue(50);
    this.jProgressBar1.setVisible(true);
    this.jProgressBar1.setStringPainted(true);
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    jLabel1 = new javax.swing.JLabel();
    jProgressBar1 = new javax.swing.JProgressBar();

    jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18));
    jLabel1.setText("Probando");
    add(jLabel1);
    add(jProgressBar1);
    }// </editor-fold>


    public void change_progressbar_value(int valor){
    this.jProgressBar1.setString("Haciendo algo");
    this.jProgressBar1.setValue(valor);
    this.validate();
    }

    public void change_text(String texto){
    this.jLabel1.setText(texto);
    }
    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    private javax.swing.JProgressBar jProgressBar1;
    // End of variables declaration

    }


  2. #2
    Junior Member
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problems with rendering a jprogressbar

    Upppsss! Now I solved the problem. The main problem was that Netbeans created a new JProgressBar by its own, in the code that it made automatically, but after that, in the constructor, I associated to that jprogressbar(created by Netbeans) a new object like "this.jprogressBar = new JProgressBar(min, max)" so I was making reference to another object, not the one which shows up in the UI.

    Thanks anyway.

Similar Threads

  1. rmi problems
    By deepthought in forum Java SE APIs
    Replies: 7
    Last Post: September 7th, 2010, 07:25 PM
  2. JMonkeyEngine 3 not rendering.
    By scribbleno1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 13th, 2010, 02:29 AM
  3. How to compile and run java program?
    By ebalari56 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 27th, 2009, 09:33 PM
  4. Problem in JSP rendering
    By srkumarj2ee@gmail.com in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: September 13th, 2009, 02:26 PM
  5. Replies: 1
    Last Post: February 16th, 2009, 11:52 AM