Results 1 to 2 of 2
- 12-14-2011, 11:05 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 1
- Rep Power
- 0
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:
Unfortunately, the panel is in the old position, as defined by NetBeans generated method initComponents: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 }

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..gif)
I appreciate any help. Thanks!
- 12-16-2011, 07:03 PM #2
Re: Help me understand painting methods
A constructor is not a method.I want to modify its location from the constructor method.
Then drop the visual designer and learn to code the GUI yourself.I'd like to understand what's happening.
Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)
Omit the second subtopic listed. It'll only hamper the learning process.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Grid Painting?
By iCon09 in forum New To JavaReplies: 2Last Post: 02-26-2011, 07:04 AM -
Painting in SWT
By jionnet in forum SWT / JFaceReplies: 9Last Post: 09-24-2010, 06:52 AM -
Painting
By xael in forum New To JavaReplies: 6Last Post: 09-06-2010, 05:10 AM -
painting problem
By hannes in forum New To JavaReplies: 3Last Post: 01-17-2010, 11:44 AM -
Trying to understand accessor and mutator methods
By DC200 in forum New To JavaReplies: 6Last Post: 12-02-2008, 11:15 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks