Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By Tolls

Thread: weird problems with 2 jPanel (resize, hide, and show)

  1. #1
    integer is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Unhappy weird problems with 2 jPanel (resize, hide, and show)

    I have 2 pieces JPanel (jPanel1 and jPanel2) and i want to make : when btn1 clicked then jPnl1 l is active then it will change the size, position, and visibility in the frame, and jPnl2 will disappear, change its position and size. its same with btn2.

    This is tmy code

    Java Code:
    import javax.swing.JPanel;
    
    public class NewJFrame extends javax.swing.JFrame {
        int[] jPnl1Init =  {160, 50, 100, 40}; int[] jPnl1Active =  {260,49,990,430};
        int[] jPnl2Init =  {260, 50, 100, 40}; int[] jPnl2Active =  {260,50,980,310};
    
        private void panelState(JPanel panel, int[] state, boolean visible){
            panel.setBounds(state[0], state[1], state[2], state[3]);
            panel.validate();
            panel.repaint();
            panel.setVisible(visible);
        }  
    
        public NewJFrame() {
            initComponents();
        }
    
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            jPanel1 = new javax.swing.JPanel();
            btn1 = new javax.swing.JButton();
            btn2 = new javax.swing.JButton();
            jPnl1 = new javax.swing.JPanel();
            jPnl2 = new javax.swing.JPanel();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jPanel1.setBackground(new java.awt.Color(102, 255, 102));
            jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
    
            btn1.setText("jButton1");
            btn1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btn1ActionPerformed(evt);
                }
            });
            jPanel1.add(btn1, new org.netbeans.lib.awtextra.AbsoluteConstraints(544, 0, -1, -1));
    
            btn2.setText("jButton2");
            btn2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btn2ActionPerformed(evt);
                }
            });
            jPanel1.add(btn2, new org.netbeans.lib.awtextra.AbsoluteConstraints(643, 0, -1, -1));
    
            org.jdesktop.layout.GroupLayout jPnl1Layout = new org.jdesktop.layout.GroupLayout(jPnl1);
            jPnl1.setLayout(jPnl1Layout);
            jPnl1Layout.setHorizontalGroup(
                jPnl1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(0, 208, Short.MAX_VALUE)
            );
            jPnl1Layout.setVerticalGroup(
                jPnl1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(0, 100, Short.MAX_VALUE)
            );
    
            jPanel1.add(jPnl1, new org.netbeans.lib.awtextra.AbsoluteConstraints(159, 98, -1, -1));
    
            org.jdesktop.layout.GroupLayout jPnl2Layout = new org.jdesktop.layout.GroupLayout(jPnl2);
            jPnl2.setLayout(jPnl2Layout);
            jPnl2Layout.setHorizontalGroup(
                jPnl2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(0, 100, Short.MAX_VALUE)
            );
            jPnl2Layout.setVerticalGroup(
                jPnl2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(0, 100, Short.MAX_VALUE)
            );
    
            jPanel1.add(jPnl2, new org.netbeans.lib.awtextra.AbsoluteConstraints(497, 64, -1, -1));
    
            getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
    
            pack();
        }// </editor-fold>
    
        private void btn1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            panelState(jPnl1, jPnl1Init, false);
            panelState(jPnl2, jPnl2Active, true); 
        }                                        
    
        private void btn2ActionPerformed(java.awt.event.ActionEvent evt) {
            panelState(jPnl2, jPnl2Init, false);
            panelState(jPnl1, jPnl1Active, true);      
        }
    
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                public void run() {
                    new NewJFrame().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify
        private javax.swing.JButton btn1;
        private javax.swing.JButton btn2;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JPanel jPnl1;
        private javax.swing.JPanel jPnl2;
        // End of variables declaration
    }
    When me click bnt1 them jpnl2 will hide, resize, and change position, jPnl1 will appear, and change position but not resize, i have to click btn1 one more time to make jPnl1 to resize... its same with btn2.

    Any ideas on how I might go about setting the jPanel's

    im using netbeans 7.0 and JDK 1.6

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    integer is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: weird problems with 2 jPanel (resize, hide, and show)

    I have never used a layout other than the absolute layout and free design, but i trying it.. thx Fubarable

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: weird problems with 2 jPanel (resize, hide, and show)

    Case in point, don't use the GUI builder...it really doesn't help when you're learning.

    It also does one of my pet peeves, which is extend JFrame.
    Fubarable likes this.

  5. #5
    integer is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: weird problems with 2 jPanel (resize, hide, and show)

    Well im tryng, but still love the power of GUI Builder, haha

  6. #6
    integer is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Unhappy another weird problems(resize, hide, and show)

    first.. sorry im still using GUI builder

    at first code, the jScrollpane will resize correctly as the writen code
    Java Code:
    import java.awt.Dimension;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    
    public class NewJFrame extends javax.swing.JFrame {
        public NewJFrame() {
            initComponents();
        }
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            btn1 = new javax.swing.JButton();
            btn2 = new javax.swing.JButton();
            jPanel1 = new javax.swing.JPanel();
            jsp = new javax.swing.JScrollPane();
            tbl = new javax.swing.JTable();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            btn1.setText("jButton1");
            btn1.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    btn1MouseClicked(evt);
                }
            });
    
            btn2.setText("jButton2");
            btn2.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    btn2MouseClicked(evt);
                }
            });
    
            tbl.setModel(new javax.swing.table.DefaultTableModel(
                new Object [][] {
                    {null, null, null, null},
                    {null, null, null, null},
                    {null, null, null, null},
                    {null, null, null, null}
                },
                new String [] {
                    "Title 1", "Title 2", "Title 3", "Title 4"
                }
            ));
            jsp.setViewportView(tbl);
    
            org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .add(jsp, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 375, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(138, Short.MAX_VALUE))
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(jPanel1Layout.createSequentialGroup()
                    .add(jsp, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 275, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(12, Short.MAX_VALUE))
            );
    
            org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(layout.createSequentialGroup()
                            .add(300, 300, 300)
                            .add(btn1)
                            .add(18, 18, 18)
                            .add(btn2))
                        .add(layout.createSequentialGroup()
                            .addContainerGap()
                            .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                    .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 33, Short.MAX_VALUE)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(btn1)
                        .add(btn2))
                    .addContainerGap())
            );
    
            pack();
        }// </editor-fold>                        
    
        private void btn1MouseClicked(java.awt.event.MouseEvent evt) {                                  
            jsp.setSize(100, 100);
            jsp.validate();
            jsp.repaint();
        }                                 
    
        private void btn2MouseClicked(java.awt.event.MouseEvent evt) {                                  
            jsp.setSize(200, 200);
            jsp.validate();
            jsp.repaint();
        }                                 
    
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                public void run() {
                    new NewJFrame().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify                     
        private javax.swing.JButton btn1;
        private javax.swing.JButton btn2;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JScrollPane jsp;
        private javax.swing.JTable tbl;
        // End of variables declaration                   
    }
    code 2 : at 1st time, i have to click btn1 or btn2 twice to make the jScrollpane resize after appear.
    Java Code:
    import java.awt.Dimension;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    
    public class NewJFrame extends javax.swing.JFrame {
        public NewJFrame() {
            initComponents();
            jsp.setVisible(false);
        }
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            btn1 = new javax.swing.JButton();
            btn2 = new javax.swing.JButton();
            jPanel1 = new javax.swing.JPanel();
            jsp = new javax.swing.JScrollPane();
            tbl = new javax.swing.JTable();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            btn1.setText("jButton1");
            btn1.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    btn1MouseClicked(evt);
                }
            });
    
            btn2.setText("jButton2");
            btn2.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    btn2MouseClicked(evt);
                }
            });
    
            tbl.setModel(new javax.swing.table.DefaultTableModel(
                new Object [][] {
                    {null, null, null, null},
                    {null, null, null, null},
                    {null, null, null, null},
                    {null, null, null, null}
                },
                new String [] {
                    "Title 1", "Title 2", "Title 3", "Title 4"
                }
            ));
            jsp.setViewportView(tbl);
    
            org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .add(jsp, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 375, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(138, Short.MAX_VALUE))
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(jPanel1Layout.createSequentialGroup()
                    .add(jsp, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 275, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(12, Short.MAX_VALUE))
            );
    
            org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(layout.createSequentialGroup()
                            .add(300, 300, 300)
                            .add(btn1)
                            .add(18, 18, 18)
                            .add(btn2))
                        .add(layout.createSequentialGroup()
                            .addContainerGap()
                            .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                    .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 33, Short.MAX_VALUE)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(btn1)
                        .add(btn2))
                    .addContainerGap())
            );
    
            pack();
        }// </editor-fold>                        
    
        private void btn1MouseClicked(java.awt.event.MouseEvent evt) {                                  
            jsp.setVisible(true);
            jsp.setSize(100, 100);
            jsp.validate();
            jsp.repaint();
        }                                 
    
        private void btn2MouseClicked(java.awt.event.MouseEvent evt) {                                  
            jsp.setVisible(true);
            jsp.setSize(200, 200);
            jsp.validate();
            jsp.repaint();
        }                                 
    
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                public void run() {
                    new NewJFrame().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify                     
        private javax.swing.JButton btn1;
        private javax.swing.JButton btn2;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JScrollPane jsp;
        private javax.swing.JTable tbl;
        // End of variables declaration                   
    }
    Please help me how to resolve this problem, me feel java is too hard, even for resize, hide, and show

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: weird problems with 2 jPanel (resize, hide, and show)

    You're using NetBeans' default layout, the GroupLayout. I'd choose a more user friendly layout (or nested layouts). The tutorials can help you with this.

  8. #8
    integer is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: weird problems with 2 jPanel (resize, hide, and show)

    Fubarable... thx for your advice to using card layout to swap jpanels, its really work for my 1st problem.
    now the second problem... i think its not the layout problem. I have 5 tables models, than I make 5 pieces of the object table so I create an object table can change the size. Perhaps with some of my projects that have been uploaded, could further help me to explain the problems I face at this time.
    Free File Hosting & Video Downloads, Free File Sharing, Online Friends Network - Ziddu

    I've tried searching the internet trying to solve my problem these days but I still have this problem. Pls help me...

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: weird problems with 2 jPanel (resize, hide, and show)

    If it's too much code to post here, it's too much code to expect free help with.

    You do not learn Swing by using a visual tool that produces code well beyond your present level of understanding. If you're not willing to bite the bullet and go through the tutorials as already advised, maybe it's better you give up now.

    I've tried searching the internet
    Have you tried going through the tutorials on the Oracle site? or were you searching for something you might be able to copy without having to understand it?

    db

  10. #10
    integer is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: weird problems with 2 jPanel (resize, hide, and show)

    DarrylBurke, ok its my bad.. to much code even for this simple problem.
    and yes i've read tutorials from oracle, and downloaded it. My bad, did not read all of the tutorials.

    maybe i'll give it up.. but not now.

    Thx.. for ur advice.

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: weird problems with 2 jPanel (resize, hide, and show)


  12. #12
    integer is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: weird problems with 2 jPanel (resize, hide, and show)

    Thx for the url, but i'm sure for my 2nd problem is not the layout.
    again... thx in advance :)

Similar Threads

  1. FrameView doesn't show new JPanel until I resize
    By metazone in forum SWT / JFace
    Replies: 3
    Last Post: 01-05-2012, 03:52 PM
  2. Replies: 10
    Last Post: 07-03-2011, 01:07 PM
  3. Show/Hide Textfields in Struts 2 form
    By aruna.hcu in forum Web Frameworks
    Replies: 3
    Last Post: 07-08-2010, 03:36 PM
  4. Show/Hide Label with JToggleButton
    By ntagrafix in forum AWT / Swing
    Replies: 4
    Last Post: 11-04-2009, 02:19 AM
  5. Resize shell on show/hide controls.
    By spacetoha in forum SWT / JFace
    Replies: 1
    Last Post: 03-24-2009, 12:20 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •