• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to create a popup JTable

 
joe encalada
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all. I would like to know if there is way to create a popup JFrame  which contains a JTable which I can use to enter some data. This JTable popups when I press a JButton.

Thanks.
 
Paul Clapham
Sheriff
Posts: 21548
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, there is. Although... is using a JFrame to contain your JTable a fixed requirement? I ask because you'd often use a JDialog to do that sort of thing.
 
joe encalada
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, It is not a fixed requirement. But I would have to try it and see what happens. Can you show me some code o Web page  to initialize and use JTable inside JDialog?

Thank you Paul.
 
Rob Camick
Ranch Hand
Posts: 2668
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding a JTable to a JDialog is the same as adding a JTable to a JFrame (which is the same as adding any component to a JFrame..

 
joe encalada
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Rob. But how to display the JDialog as a popup When a JButton has been pressed ?
 
Rob Camick
Ranch Hand
Posts: 2668
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The same way you display a JFrame. You create the dialog add components to it and then use setVisible(...).
 
joe encalada
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Rob. I'll do what you suggested.
 
Maneesh Godbole
Saloon Keeper
Posts: 11308
16
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:The same way you display a JFrame. You create the dialog add components to it and then use setVisible(...).


A more convenient way would be to use one of the many JOptionPane.showXXXDialog
All these accept an Object message. This means you can also pass your JTable, JTable wrapped inside a JScrollPane or even a complex JPanel with multiple children as the message.
Check out http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html
 
joe encalada
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob you where right. Everything works ok. Here is part of my code.





But now I facing a problem.I need the only one  popup is present and not other can be present until I close the first one or if I press the save button to close it. I guess is matter of use another listener with the save button but I dont know how to do it. Can you suggest me something.

Maneesh Godbole I'm reading above your suggestion, I'll try it.

 
Rob Camick
Ranch Hand
Posts: 2668
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need the only one  popup is present and not other can be present until I close the first one


This is called a "modal" dialog. So you need to set the property of your dialog and you need to set your JFrame as the parent of the dialog.

In your listener you add the following:
 
joe encalada
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm gonna do it. Thank you Rob.
 
joe encalada
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everything works as espected but only a maybe silly question. Why when I set modal in JDialog the default JDialog size is lost. Here is the command inserted in row 6.

Thanks.
 
Campbell Ritchie
Sheriff
Posts: 51332
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't like to see anything about== e.getSource() in an add action listener method. Add that Listener to a specific button and nowhere else, and that equality test becomes unnecessary. I would also suggest you move most of that code into a different method. See following post.
 
Campbell Ritchie
Sheriff
Posts: 51332
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's change yout code:- Start by moving the code into a method by itself.Now, we no longer need the test about getSource(), so we can lose that. (And change the indentation.)You now no longer need the action event parameter:-I shall ognore the other listener in line 8. So let's leave that method unchanged, and look at the button. This is what you would have written in Java7, but in Java8 you can miss much of it out.Now, I shall flatten that code to a single line and strike through the bits you can omit in Java8:-
openDialogButton.addActionListener( new ActionListener() { @Override /* optional in Java6+, not permissible in Java1.1-5 */ public void actionPerformed(ActionEvent evt) { openDialogActionPerformed(); } } );
openDialogButton.addActionListener( new ActionListener() { @Override /* optional in Java6+, not permissible in Java1.1-5 */ public void actionPerformed(ActionEvent evt) { openDialogActionPerformed(); } } );
Of course, there is a bit missing. The arrow token, which I think is pronounced “goes to”.
openDialogButton.addActionListener(evt openDialogActionPerformed() );
openDialogButton.addActionListener(evt -> openDialogActionPerformed());
So we end up withDon't know about setting modality altering the size of the dialogue window. If you are using pack(), I thought you have to call setPreferredSize() on enough Components to fill the width and height of the display. I also thought Dialogs were modal by default, but you will have to check that in the documentation, because I am by no means sure about that.
 
Campbell Ritchie
Sheriff
Posts: 51332
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you add things to a JPanel, that uses flow layout by default. You would do well to use a different layout.
 
Rob Camick
Ranch Hand
Posts: 2668
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would normally do the pack() before the setVisible().
 
joe encalada
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It did the trick. Thank you Rob.
 
Campbell Ritchie
Sheriff
Posts: 51332
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:You would normally do the pack() before the setVisible().
Damn! I never noticed that bit.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic