Spikerman
Spikerman

Reputation: 63

JTextAreas are invisible in JDialog, I have added setVisible function, but it still doesn't work

I hope to show my JDialog when click the button, but when I click it, the dialog does show, but I can't see any other component on my Dialog since I have added four textfied on my dialog and added the setVisible function already

class Class2_3 {
public static void main(String[]args){

    MyFrame myFrame=new MyFrame();
    myFrame.setVisible(true);
}
}

class MyDialog extends JDialog{

Container container =this.getContentPane();
JPanel jPanel=new JPanel();

public MyDialog(Component relativeTo)
{
    super();
    this.IniDialog();

    this.setLocationRelativeTo(relativeTo);
    this.setSize(300,300);
    //this.setDefaultCloseOperation(EXIT_ON_CLOSE);

    setModal(true);
    //this.setVisible(true);
}


public void  IniDialog()
{
    JTextField textField1=new JTextField(10);
    textField1.setMaximumSize(textField1.getPreferredSize());
    Box hBox1=Box.createHorizontalBox();

    JTextField textField2=new JTextField(10);
    textField2.setMaximumSize(textField2.getPreferredSize());
    Box hBox2=Box.createHorizontalBox();

    JTextField textField3=new JTextField(10);
    textField3.setMaximumSize(textField3.getPreferredSize());
    Box hBox3=Box.createHorizontalBox();

    JTextField textField4=new JTextField(10);
    textField4.setMaximumSize(textField4.getPreferredSize());
    Box hBox4=Box.createHorizontalBox();

    Box vBox=Box.createVerticalBox();
    vBox.add(Box.createVerticalStrut(10));
    vBox.add(hBox1);
    vBox.add(Box.createVerticalStrut(10));
    vBox.add(hBox2);
    vBox.add(Box.createVerticalStrut(10));
    vBox.add(hBox3);
    vBox.add(Box.createVerticalStrut(10));
    vBox.add(hBox4);
    vBox.add(Box.createVerticalStrut(10));

    jPanel.add(vBox);
    container.add(jPanel);
}
}

class MyFrame extends JFrame implements ActionListener{

MyDialog dialog;
//Container contentPane=this.getContentPane();

public MyFrame()
{
    //initialization
    this.setTitle("Spike");
    this.setSize(400, 300);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);

    //Component adding
    JButton openButton=new JButton("Open");
    //openButton.setSize(50, 50);
    openButton.addActionListener(this);
    //contentPane.add(openButton);

    JLabel label1=new JLabel("编号:");
    JTextField textField1=new JTextField(10);
    textField1.setMaximumSize(textField1.getPreferredSize());
    Box hBox1=Box.createHorizontalBox();
    hBox1.add(label1);
    hBox1.add(textField1);

    JLabel label2=new JLabel("名称:");
    JTextField textField2=new JTextField(10);
    textField2.setMaximumSize(textField2.getPreferredSize());
    Box hBox2=Box.createHorizontalBox();
    hBox2.add(label2);hBox2.add(textField2);

    JLabel label3=new JLabel("单价:");
    JTextField textField3=new JTextField(10);
    textField3.setMaximumSize(textField3.getPreferredSize());
    Box hBox3=Box.createHorizontalBox();
    hBox3.add(label3);hBox3.add(textField3);

    JLabel label4=new JLabel("数量:");
    JTextField textField4=new JTextField(10);
    textField4.setMaximumSize(textField4.getPreferredSize());
    Box hBox4=Box.createHorizontalBox();
    hBox4.add(label4);hBox4.add(textField4);

    Box vBox=Box.createVerticalBox();

    vBox.add(Box.createVerticalStrut(10));
    vBox.add(hBox1);
    vBox.add(Box.createVerticalStrut(10));
    vBox.add(hBox2);
    vBox.add(Box.createVerticalStrut(10));
    vBox.add(hBox3);
    vBox.add(Box.createVerticalStrut(10));
    vBox.add(hBox4);
    vBox.add(Box.createVerticalStrut(10));
    vBox.add(openButton);

    this.add(vBox,BorderLayout.CENTER);
}

@Override
public void actionPerformed(ActionEvent e) {
    if(dialog==null){
        //dialog=(MyDialog) new JDialog(this,true);
        dialog=new MyDialog(this);
        dialog.setVisible(true);
    }
}
}

Upvotes: 1

Views: 52

Answers (1)

Eng.Fouad
Eng.Fouad

Reputation: 117589

You forgot to add the text fields to the boxes in IniDialog() :)

hBox1.add(textField1);
hBox2.add(textField2);
hBox3.add(textField3);
hBox4.add(textField4);

Upvotes: 3

Related Questions