Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding components to panel doesn't work.

Status
Not open for further replies.

adicohen

Programmer
Oct 7, 2004
12
IL
The following code is compilng and runing with no errors.
It should add X textfiled to a panel( which is empty), where X is enterd by user
no component is added to the panel ( nothing that I can see - I change the color so it will be visible for sure).

What should I change?

Thanks,
Adi

private void jComboBoxSizeActionPerformed(java.awt.event.ActionEvent evt) {
if ("comboBoxChanged".equals(evt.getActionCommand())) {
String numStr=(String)jComboBoxSize.getSelectedItem();
int num = Integer.parseInt(numStr);
if (num > 0 )
PrintWordCell(num);
}
}

private void PrintWordCell( int num) {
MaskFormatter formatter=null;
try{
formatter = new MaskFormatter("?");
GridLayout gl=new java.awt.GridLayout(1,num);
jPanelWord.setLayout(gl);
WordToFind=new javax.swing.JTextField[num];
for ( int i=0; i<num;i++) {
WordToFind=new javax.swing.JTextField();
WordToFind.setBackground(new java.awt.Color(255,0,255));
jPanelWord.add(WordToFind);
}
CardLayout cl = (CardLayout)(Screens.getLayout());
cl.show(Screens,"CreateCross");
}
catch (java.text.ParseException x){
x.printStackTrace();
}
}
 
hm.
Not enough code, to be sure, what's happening.

btw: more a j2se (Java (Sun))-question than j2ee.

May it be, that you replace Panels/ Layout, instead of adding new components to them?

seeking a job as java-programmer in Berlin:
 
I'm changing layout just to make sure i'm using GridLayout
the following line add compenent to panel , isn't it?
jPanelWord.add(WordToFind);


 
If the panel is visible when you add the JTextField components, then you must call validate() for that panel in order to display components added after you displayed the panel. The folowing code displayes 5 JTextFrame components(I've modified your code a little)
Code:
import javax.swing.*;
import java.awt.*;
class test extends JFrame
{
 JPanel jPanelWord = new JPanel();
javax.swing.JTextField[]  WordToFind;
 test(){
        //MaskFormatter formatter=null;
        //try{
          //  formatter = new MaskFormatter("?");
            setSize(400,400);
            setVisible(true);
            jPanelWord.setSize(400,400);
            jPanelWord.setBackground(Color.RED);
            getContentPane().add(jPanelWord);
            GridLayout gl=new java.awt.GridLayout(1,5);
            jPanelWord.setLayout(gl);
            WordToFind=new javax.swing.JTextField[5];
            for ( int i=0; i<5;i++) {
                WordToFind[i]=new javax.swing.JTextField();
                WordToFind[i].setBackground(new java.awt.Color(255,0,255));
                jPanelWord.add(WordToFind[i]);
            }
             jPanelWord.validate();
            //CardLayout cl = (CardLayout)(Screens.getLayout());
            //cl.show(Screens,"CreateCross");
        }
        //catch (java.text.ParseException x){
            //x.printStackTrace();
        //}
   
    
public static void main(String args[])
{
	
	test g = new test();
	
}

}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top