简体   繁体   中英

JDialog window don't Show

I have a JFrame that has an addButton.

When user clicked Button, AddDialog window must show, but I can't see it And Can not Add row

Here's My code:

public class AddDialogS extends JDialog{
BookInformation bookinform=new BookInformation();

public AddDialogS(JFrame owner){
    super(owner,"Add New Book!", true);
    JButton OkButton=new JButton("Ok");
   final JTextField nameTF=new JTextField(10);
   final JTextField dateTF=new JTextField(10);
   final JTextField idTF=new JTextField(10);

    OkButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            bookinform.setBookName(nameTF.getText());
            bookinform.setBookDate(dateTF.getText());
            bookinform.setBookID(idTF.getText());
            AddDialogS.this.dispose();
        }
    });

    JPanel panel=new JPanel(new FlowLayout());
    panel.add(OkButton);
    panel.add(nameTF);
    panel.add(dateTF);
    panel.add(idTF);
}

public BookInformation getBookInfos(){
    return bookinform;
}
}

And me main Class:

public class MainS extends JFrame{

   final AllBooks allBooks=new AllBooks();
   final JTable Btable=new JTable(allBooks);

   public MainS(){
       JButton AddBookButton=new JButton("Add New Book");
       AddBookButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) { 

           AddDialogS adddialog=new AddDialogS(MainS.this);
           BookInformation B_info=adddialog.getBookInfos();
           if(B_info != null){
               allBooks.AddRow(B_info);
           }
        }
    });

    JPanel Bpanel=new JPanel();
    Bpanel.setLayout(new FlowLayout());
    JScrollPane sp=new JScrollPane(Btable);
    Bpanel.add(sp);
    Bpanel.add(AddBookButton);
    this.add(Bpanel);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setBounds(300, 60, 550, 550);
    this.setVisible(true);
   }

   public static void main(String[] args){
       new MainS();
   }
}

It wolud Add a new line to jtable.

You should add dialog.setVisible(true); in the actionPerformed() method of your main class:

public class MainS extends JFrame{
...
    public MainS(){
        AddDialogS adddialog=new AddDialogS(MainS.this);
        adddialog.setVisible(true);

        BookInformation B_info=adddialog.getBookInfos();
        ...

Just creating object of JDialog is not going to display it. You must set it visible using setVisible(true) .

Also I cant see the size set to your dialog box. You can use setSize(width, height) for this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM