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
}