0

How do i refresh jPanels after i change it height? After i compile the code, it does shows it, but doesn't display it the way i want after i click the random button twice (i use random button to change the height to the random numbers).

And antoher thing. the jPanel is upside down. How do i change that?

Here is the code i had written so far :

package learn;

import java.util.Random;

public class Sorting extends javax.swing.JFrame {

/**
 * Creates new form Sorting
 */
public Sorting() {
    initComponents();
    jPanel1.setVisible(false);
    jPanel2.setVisible(false);
    jPanel3.setVisible(false);
    jPanel4.setVisible(false);
    jPanel5.setVisible(false);
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    randomizingButton = new javax.swing.JButton();
    sortingButton = new javax.swing.JButton();
    viewer = new javax.swing.JPanel();
    jPanel1 = new javax.swing.JPanel();
    jPanel2 = new javax.swing.JPanel();
    jPanel3 = new javax.swing.JPanel();
    jPanel4 = new javax.swing.JPanel();
    jPanel5 = new javax.swing.JPanel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    randomizingButton.setText("Randomizing");
    randomizingButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            randomizingButtonActionPerformed(evt);
        }
    });

    sortingButton.setText("Sorting");

    jPanel1.setBackground(new java.awt.Color(0, 0, 0));

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 20, Short.MAX_VALUE)
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 0, Short.MAX_VALUE)
    );

    jPanel2.setBackground(new java.awt.Color(0, 0, 0));

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 20, Short.MAX_VALUE)
    );
    jPanel2Layout.setVerticalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 0, Short.MAX_VALUE)
    );

    jPanel3.setBackground(new java.awt.Color(0, 0, 0));

    javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
    jPanel3.setLayout(jPanel3Layout);
    jPanel3Layout.setHorizontalGroup(
        jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 20, Short.MAX_VALUE)
    );
    jPanel3Layout.setVerticalGroup(
        jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 0, Short.MAX_VALUE)
    );

    jPanel4.setBackground(new java.awt.Color(0, 0, 0));

    javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
    jPanel4.setLayout(jPanel4Layout);
    jPanel4Layout.setHorizontalGroup(
        jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 20, Short.MAX_VALUE)
    );
    jPanel4Layout.setVerticalGroup(
        jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 0, Short.MAX_VALUE)
    );

    jPanel5.setBackground(new java.awt.Color(0, 0, 0));

    javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
    jPanel5.setLayout(jPanel5Layout);
    jPanel5Layout.setHorizontalGroup(
        jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 20, Short.MAX_VALUE)
    );
    jPanel5Layout.setVerticalGroup(
        jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 100, Short.MAX_VALUE)
    );

    javax.swing.GroupLayout viewerLayout = new javax.swing.GroupLayout(viewer);
    viewer.setLayout(viewerLayout);
    viewerLayout.setHorizontalGroup(
        viewerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(viewerLayout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    viewerLayout.setVerticalGroup(
        viewerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, viewerLayout.createSequentialGroup()
            .addContainerGap(155, Short.MAX_VALUE)
            .addGroup(viewerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(jPanel5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addContainerGap())
    );

    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()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(viewer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createSequentialGroup()
                    .addGap(12, 12, 12)
                    .addComponent(randomizingButton)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(sortingButton)
                    .addGap(0, 0, Short.MAX_VALUE)))
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(randomizingButton)
                .addComponent(sortingButton))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(viewer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addContainerGap())
    );

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

private void randomizingButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                  
    int height[] = new int[10];

    Random rand = new Random();
    for(int i=0; i<5; i++){
        height[i] = rand.nextInt(50)+1;
    }

    jPanel1.setSize(jPanel1.getWidth(), height[0]);
    jPanel2.setSize(jPanel2.getWidth(), height[1]);
    jPanel3.setSize(jPanel3.getWidth(), height[2]);
    jPanel4.setSize(jPanel4.getWidth(), height[3]);
    jPanel5.setSize(jPanel5.getWidth(), height[4]);

    jPanel1.setVisible(true);
    jPanel2.setVisible(true);
    jPanel3.setVisible(true);
    jPanel4.setVisible(true);
    jPanel5.setVisible(true);


    // Testing if the height of the bar was set to the height in the array
    for(int i=0; i<5; i++){
        System.out.println(height[i]);
    }
    System.out.println("");
    System.out.println(jPanel1.getHeight());
    System.out.println(jPanel2.getHeight());
    System.out.println(jPanel3.getHeight());
    System.out.println(jPanel4.getHeight());
    System.out.println(jPanel5.getHeight());

    repaint();
//        revalidate();
}                                                 

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(Sorting.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Sorting.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Sorting.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Sorting.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Sorting().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
private javax.swing.JPanel jPanel5;
private javax.swing.JButton randomizingButton;
private javax.swing.JButton sortingButton;
private javax.swing.JPanel viewer;
// End of variables declaration                   
}
QuartZ
  • 154
  • 3
  • 12
  • I'm using netbeans IDE, and jFrame Form. – QuartZ Mar 24 '15 at 12:32
  • Managing the size of components is the job of the layout manager, and that tries to set the panels to a certain size when you make the panels visible, overriding the size you try to set manually. You'll see the same effect if you resize the window => the panels are made all equal height, because the layout manager does its job. It is likely better to have panels that just *draw* a certain height area black, regardless of the panel's actual height. – kiheru Mar 24 '15 at 12:44
  • Well i wan't to use the height of the panel to represent the randomizing result. And then sorting it from the shortest to the highest. – QuartZ Mar 24 '15 at 13:59

2 Answers2

1

Try using the repaint() method.
repaint() does not invoke paint() directly. It schedules a call to an intermediate method, update(). Finally, update() calls paint().

adiSuper94
  • 23
  • 10
  • I tried repaint(). I still need to click the Randomizing button twice. Tried revalidate(), it shows the jPanel but didn't change the height of the jPanel. It just shows it. And it still upside down. – QuartZ Mar 24 '15 at 13:26
  • insert repaint() right after you set the component size. – adiSuper94 Mar 24 '15 at 13:38
  • Tried it. Still didn't work. I updated my code above. By the way, is my code alright? – QuartZ Mar 24 '15 at 13:56
1

Layout managers use the information provided by the getPreferredSize(), getMinimumSize() and getMaximumSize() method.

If you want a random size then you should create a custom class that supports your requirement. Something like:

public class RandomPanel extends JPanel
{
    private Dimension randomSize;

    public void setRandomSize(Dimension randomSize)
    {
        this.randomSize = randomSize;
    }

    @Override
    public Dimension getPreferredSize()
    {
        if (randomSize == null)
            return super.getPreferredSize();
        else
            return randomSize;
}

Then in your code instead of invoking the setSize() method you invoke setRandomSize(...).

After you set the random sizes of all the panel you then need to invoke the layout manager:

revalidate();
repaint();
camickr
  • 321,443
  • 19
  • 166
  • 288
  • can you help me with https://stackoverflow.com/q/54623741/5113188 please? –  Feb 12 '19 at 02:37