Re: How to open a JPanel similar to a JDialog
 
etantonio wrote:
Good Morning,
I'm creating a gui, from the menuBar I've a menuitem that I need that
opens a new panel,
not just a dialog panel because in this panel I've to insert some
calibration values for my program,
while a Jdialog can just insert a value
Not true, a single JDialog can be used to view/set/edit many values. See 
below.
.
How I can open this new panel really similar to a jdialog but
different in the number of parameters that I could change.
Thanks
Put the JPanel in a JDialog.
------------------------8<-------------------------------
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
/**
  *
  * @author RedGrittyBrick
  *
  */
public class MultiValueDialog1 {
     private JFrame frame;
     public static void main(String[] args) {
         SwingUtilities.invokeLater(new Runnable() {
             @Override
             public void run() {
                 new MultiValueDialog1().createAndShowGUI();
             }
         });
     }
     JLabel label = new JLabel("Please log in");
     void createAndShowGUI() {
         JMenuItem item = new JMenuItem("Login");
         item.addActionListener(new Login());
         JMenu menu = new JMenu("User");
         menu.add(item);
         JMenuBar bar = new JMenuBar();
         bar.add(menu);
         JPanel p = new JPanel();
         p.add(label);
         frame = new JFrame("MultiValueDialog1");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setJMenuBar(bar);
         frame.add(p);
         frame.pack();
         frame.setLocationRelativeTo(null);
         frame.setVisible(true);
     }
     class Login implements ActionListener {
         @Override
         public void actionPerformed(ActionEvent e) {
             LoginPanel panel = new LoginPanel();
             JOptionPane.showConfirmDialog(frame, panel, "Login",
                     JOptionPane.OK_CANCEL_OPTION);
             label.setText("Welcome " +
                     panel.getCredentials().toString());
             frame.pack();
         }
     }
     class LoginPanel extends JPanel {
         private JTextField firstNameField = new JTextField(20),
                 lastNameField = new JTextField(20);
         LoginPanel() {
             setLayout(new GridBagLayout());
             GridBagConstraints c = new GridBagConstraints();
             add(new JLabel("First Name: "), c);
             c.gridwidth = GridBagConstraints.REMAINDER;
             add(firstNameField, c);
             c.gridwidth = 1;
             add(new JLabel("Last Name: "), c);
             c.gridwidth = GridBagConstraints.REMAINDER;
             add(lastNameField, c);
         }
         Credentials getCredentials() {
             return new Credentials(firstNameField.getText(),
                     lastNameField.getText());
         }
     }
     class Credentials {
         String firstname, lastname;
         Credentials(String name, String password) {
             this.firstname = name;
             this.lastname = password;
         }
         public String toString() {
             return firstname + " " + lastname;
         }
     }
}
------------------------8<-------------------------------
-- 
RGB