Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JPanel Components invisible - Common problem :(

843806Jun 20 2008 — edited Jun 20 2008
Hello guys,
I've done a bunch of searching on these forums, and everywhere else, and I just cant seem to find a general solution to this problem. Things I have tried:

(All Seperate)
setVisible(true);
setContentPane(jScrollPane1);
jScrollPane1.invalidate();
jScrollPane1.revalidate();
jScrollPane1.validate();
jScrollPane1.repaint();
I inserted these in the end of the .addRecord() method.

and still, nothing is appearing, save for the Buttons when I mouse over them.

I need the contents of the jScrollPane1 to be shown at startup, and on re-size. Please lend me a hand!


Note both initComponents() where generated via designer.

Here is the Parent Frame:
/*
 * VCMSPreviewer.java
 *
 * Created on June 19, 2008, 4:39 PM
 */



import java.awt.Graphics;
import java.awt.GridBagLayout;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.ScrollPaneLayout;

/**
 * Actual Frame of the Tool.
 * @author HrPeanut
 */
public class VCMSPreviewer extends javax.swing.JFrame 
{
    private final int COMPONENT_HEIGHT = 150;
    private int location = 1;
    
    
    
    /** Creates new form VCMSPreviewer */
    public VCMSPreviewer() 
    {
        initComponents();
        //jScrollPane1.setLayout(new ScrollPaneLayout());
        //this.setContentPane(jScrollPane1);

    }
    
    /** 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.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                formComponentResized(evt);
            }
        });

        jMenu1.setText("File");

        jMenuItem1.setText("Item");
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("View");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 460, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 417, Short.MAX_VALUE)
        );

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

    private void formComponentResized(java.awt.event.ComponentEvent evt) {
        
       
    }
    
    
    
    
    public void addRecord(VCMSRecord data)
    {
        VCMSComponentFrame f = new VCMSComponentFrame(data);
        jScrollPane1.add(f);
        f.setLocation(0, location);
        f.setSize(jScrollPane1.getWidth(), COMPONENT_HEIGHT - 5);
        this.pack();
        location += COMPONENT_HEIGHT;
      
    }
    
    public void removeRecord(int index)        
    {
        if (index != jScrollPane1.getComponentCount() - 1)
        {
            jScrollPane1.remove(index);
            for (int i = index; i < jScrollPane1.getComponentCount(); i++)
            {
                jScrollPane1.getComponent(i).setLocation(0, (int)jScrollPane1.getComponent(i).getLocation().getY() - COMPONENT_HEIGHT);
            }
            location -= COMPONENT_HEIGHT;
        }
        else
        {
            jScrollPane1.remove(index);
        }
    }
    
    
    
    
    
    
    
    
    
    
    /**
     * @param args the command line arguments
     */
    
    public static void main(String args[]) 
    {
        java.awt.EventQueue.invokeLater(new Runnable() 
        {
            public void run() {
                new VCMSPreviewer().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration
    
}
And here is the Custom control I am trying to add to the JScrollPane.
/*
 * VCMSComponentFrame.java
 *
 * Created on June 20, 2008, 8:48 AM
 */



/**
 * JPanel to contain the information to be displayed in the
 * VCMSPreviewer JFrame.
 * @author  HrPeanut
 */
public class VCMSComponentFrame extends javax.swing.JPanel {
    
    private VCMSRecord data;
    
    
    /** Creates new form VCMSComponentFrame */
    public VCMSComponentFrame(VCMSRecord data)
    {
        initComponents();
        
        this.data = data;
        this.lbltwo.setText(data.getLatitude());
        this.lblthree.setText(data.getLongitude());
        this.lblfour.setText(data.getAltitude());

        this.setVisible(true);
        
    }
    
    public VCMSRecord getData()
    {
        return data;
    }
    
    /** 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.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        vcmsPlotView = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        lblone = new javax.swing.JLabel();
        lbltwo = new javax.swing.JLabel();
        lblthree = new javax.swing.JLabel();
        lblfour = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jToolBar1 = new javax.swing.JToolBar();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setBackground(new java.awt.Color(204, 204, 255));

        javax.swing.GroupLayout vcmsPlotViewLayout = new javax.swing.GroupLayout(vcmsPlotView);
        vcmsPlotView.setLayout(vcmsPlotViewLayout);
        vcmsPlotViewLayout.setHorizontalGroup(
            vcmsPlotViewLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 160, Short.MAX_VALUE)
        );
        vcmsPlotViewLayout.setVerticalGroup(
            vcmsPlotViewLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 71, Short.MAX_VALUE)
        );

        jLabel1.setText("Area:");

        jLabel2.setText("Latitude:");

        jLabel3.setText("Longitude:");

        jLabel4.setText("Altitude:");

        lblone.setText("jLabel5");

        lbltwo.setText("jLabel5");

        lblthree.setText("jLabel5");

        lblfour.setText("jLabel5");

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

        jButton1.setText("Next");

        jButton2.setText("Prev");

        jToolBar1.setRollover(true);

        jButton3.setText("Min");
        jButton3.setAutoscrolls(true);
        jButton3.setFocusable(false);
        jButton3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton3.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton3);

        jButton4.setText("Max");
        jButton4.setFocusable(false);
        jButton4.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton4.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton4);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(vcmsPlotView, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel4))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(lbltwo)
                                        .addComponent(lblthree)
                                        .addComponent(lblone))
                                    .addComponent(lblfour))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 110, Short.MAX_VALUE)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton1)
                            .addComponent(jButton2)))
                    .addComponent(jToolBar1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 507, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel1)
                                .addComponent(lblone))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel2)
                                .addComponent(lbltwo))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel3)
                                .addComponent(lblthree))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel4)
                                .addComponent(lblfour)))
                        .addComponent(vcmsPlotView, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2))
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
    }// </editor-fold>

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {

        
    }

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {

        
    }
    
    
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JLabel lblfour;
    private javax.swing.JLabel lblone;
    private javax.swing.JLabel lblthree;
    private javax.swing.JLabel lbltwo;
    private javax.swing.JPanel vcmsPlotView;
    // End of variables declaration
    
}
Edited by: HrPeanut on Jun 20, 2008 10:06 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 18 2008
Added on Jun 20 2008
3 comments
514 views