Hi All,
I have a component "JPResourceBox" which extends JPanel
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
public class JPResourceBox extends javax.swing.JPanel {
/** Creates new form JPResourceBox */
public JPResourceBox(JPWorkspacePanel parent) {
initComponents();
}
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
setLayout(new java.awt.BorderLayout());
setBorder(javax.swing.BorderFactory.createEtchedBorder());
jLabel1.setText("jLabel1");
add(jLabel1, java.awt.BorderLayout.CENTER);
}
private javax.swing.JLabel jLabel1;
public void setText(String buttonText){
this.jLabel1.setText(buttonText);
}
}
I am adding instances of JPResourceBox to a JFrame programatically on
a button click
public class NewJFrame extends javax.swing.JFrame {
private javax.swing.JButton jButton1;
public NewJFrame() {
initComponents();
}
private void initComponents() {
jButton1 = new javax.swing.JButton();
getContentPane().setLayout(null);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent
evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1);
jButton1.setBounds(160, 210, 75, 23);
pack();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent
evt) {
JPResourceBox jp = new JPResourceBox(null);
jp.setBounds(50,50,50,50);
add(jp);
repaint();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
}
When i click the button, the JPResourceBox instance is getting added
to the JFrame and is visible. But the text in the JLabel inside
JPResourceBox instance is not visible. If the JFrame is resized, the
text in JLablel is getting displayed.
Kindly advice on how the text in JLabel can be made to be displayed
when JPResourceBox instance is added to the JFrame.
Thanks and Regards
Chanchal
Roedy Green - 25 Sep 2007 08:57 GMT
On Tue, 25 Sep 2007 00:34:50 -0700, Chanchal
<chanchal.jacob@gmail.com> wrote, quoted or indirectly quoted someone
who said :
>When i click the button, the JPResourceBox instance is getting added
>to the JFrame and is visible. But the text in the JLabel inside
>JPResourceBox instance is not visible. If the JFrame is resized, the
>text in JLablel is getting displayed.
After you add a Component, you must do a validate() to let the
components jostle about and let the layout manager determine their new
sizes and positions.
This does not happen automatically, to avoid recalculating after every
add in a continuous stream of adds. It would just cause annoying
flicker.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Roedy Green - 25 Sep 2007 08:58 GMT
On Tue, 25 Sep 2007 07:57:30 GMT, Roedy Green
<see_website@mindprod.com.invalid> wrote, quoted or indirectly quoted
someone who said :
>After you add a Component, you must do a validate() to let the
>components jostle about and let the layout manager determine their new
[quoted text clipped - 3 lines]
>add in a continuous stream of adds. It would just cause annoying
>flicker.
see http://mindprod.com/jgloss/validate.html
http://mindprod.com/jgloss/repaint.html

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Andrew Thompson - 25 Sep 2007 09:53 GMT
>Hi All,
>
>I have a component "JPResourceBox" which extends JPanel
What about JPWorkspacePanel? I had to refactor that to a JPanel
before I could get this code to work, and then I saw no 'flicker'.
OTOH, I think the root cause of that problem (as well as
potentially others you have not yet seen) is code like..
...
> jButton1.setBounds(160, 210, 75, 23);
...
> jp.setBounds(50,50,50,50);
Using these kind of conbstructs, you can expect problems.
Learn how to use layouts, and I am betting the problem
will vanish.
<http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html>
And as an aside, please consider posting SSCCE's, to
help others to help you. An SSCCE would either have
removed the reference to JPWorkspacePanel, or added
the code, and demoted JPResourceBox to 'default' access.
To make SSCCE's short (the first 'S') it is best to add
'package imports'* at the top of the code, then drop
use of the fully qualified name of each widget used.
* That makes for a good SSCCE, not 'good production
code' - the two are different things.

Signature
Andrew Thompson
http://www.athompson.info/andrew/