Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Help me understand painting methods

  1. #1
    marcelo is offline Member
    Join Date
    Dec 2011
    Posts
    1
    Rep Power
    0

    Lightbulb Help me understand painting methods

    First of all, sorry if this is a dumb question. I'm quite new to Swing and I really appreciate some light on my issue.

    I use NetBeans as my programming IDE and came up with a simple JFrame window example. I have a JPanel in which I want to modify its location from the constructor method. Let me show my code:

    Java Code:
    package myexample;
    
    import java.awt.Point;
    
    public class Example extends javax.swing.JFrame {
    
        public Example() {
            initComponents();
            pnlTitle.setLocation(new Point((int) pnlTitle.getLocation().getX(), (int) pnlTitle.getLocation().getY() + 100));
        }
    
        @SuppressWarnings("unchecked")
        private void initComponents() {
    
            pnlTitle = new javax.swing.JPanel();
            lblText = new javax.swing.JLabel();
            btnTest = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            lblText.setText("My label");
    
            javax.swing.GroupLayout pnlTitleLayout = new javax.swing.GroupLayout(pnlTitle);
            pnlTitle.setLayout(pnlTitleLayout);
            pnlTitleLayout.setHorizontalGroup(
                pnlTitleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(pnlTitleLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(lblText)
                    .addContainerGap(331, Short.MAX_VALUE))
            );
            pnlTitleLayout.setVerticalGroup(
                pnlTitleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(pnlTitleLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(lblText)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
    
            btnTest.setText("Test");
            btnTest.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnTestActionPerformed(evt);
                }
            });
    
            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(pnlTitle, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnTest, javax.swing.GroupLayout.Alignment.TRAILING))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(pnlTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 219, Short.MAX_VALUE)
                    .addComponent(btnTest)
                    .addContainerGap())
            );
    
            pack();
        }                  
    
        private void btnTestActionPerformed(java.awt.event.ActionEvent evt) {                                        
            pnlTitle.setLocation(new Point((int) pnlTitle.getLocation().getX(), (int) pnlTitle.getLocation().getY() + 100));
        }                                       
    
        public static void main(String args[]) {
    
            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(Example.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(Example.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(Example.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(Example.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
    
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                public void run() {
                    new Example().setVisible(true);
                }
            });
        }
                            
        private javax.swing.JButton btnTest;
        private javax.swing.JLabel lblText;
        private javax.swing.JPanel pnlTitle;
        // End of variables declaration                   
    }
    Unfortunately, the panel is in the old position, as defined by NetBeans generated method initComponents:

    Help me understand painting methods-mylabel.png

    If I click in the button, the panel is correctly put in the place I want.

    I'm pretty sure it's something related to a painting method, but I really can't figure out what it is. In this particular code, I really need to set the JPanel position again after its default position from the initComponents method. But I could not figure out which method I should invoke. I tried repaint, revalidate and a thousand more, but no luck. Besides, I'd like to understand what's happening.

    I appreciate any help. Thanks!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,252
    Rep Power
    19

    Default Re: Help me understand painting methods

    I want to modify its location from the constructor method.
    A constructor is not a method.

    I'd like to understand what's happening.
    Then drop the visual designer and learn to code the GUI yourself.
    Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)
    Omit the second subtopic listed. It'll only hamper the learning process.

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Grid Painting?
    By iCon09 in forum New To Java
    Replies: 2
    Last Post: 02-26-2011, 07:04 AM
  2. Painting in SWT
    By jionnet in forum SWT / JFace
    Replies: 9
    Last Post: 09-24-2010, 06:52 AM
  3. Painting
    By xael in forum New To Java
    Replies: 6
    Last Post: 09-06-2010, 05:10 AM
  4. painting problem
    By hannes in forum New To Java
    Replies: 3
    Last Post: 01-17-2010, 11:44 AM
  5. Replies: 6
    Last Post: 12-02-2008, 11:15 PM

Posting Permissions

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