Wednesday, August 12, 2015

[Java Swing] DialogBox with Multiple Fields

Below are sample code on how to use dialog box as configuration window. It will require having multiple fields in dialog box. Below is the example on how to do it in Android.

Create a panel that will contain the panel that has the multiple fields.


//Create setup panel.
JPanel settingUITopPanel = new JPanel(new BorderLayout());
JPanel senderPanel = new JPanel();
senderPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("JMS SENDER"));
settingUITopPanel.add(senderPanel, BorderLayout.NORTH);
JPanel listenerPanel = new JPanel();
listenerPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("JMS LISTENER"));
settingUITopPanel.add(listenerPanel, BorderLayout.SOUTH);
GroupLayout layout = new GroupLayout(senderPanel);
senderPanel.setLayout(senderSetup(layout));
GroupLayout layout1 = new GroupLayout(listenerPanel);
listenerPanel.setLayout(listenerSetup(layout1));

//Displa dialog box with multiple fields.

int result = JOptionPane.showConfirmDialog(null,settingUITopPanel,"Setting",JOptionPane.OK_CANCEL_OPTION);

if(result == JOptionPane.OK_OPTION){

   //Get fields value here...
}


Method that will return sender setup layout.

private GroupLayout senderSetup(GroupLayout layout){
   JLabel l1 = new JLabel("MQ Host Name");
   JLabel l2 = new JLabel("MQ Port Number");
   JLabel l3 = new JLabel("MQ Manager");
   JLabel l4 = new JLabel("MQ Queue");
   JLabel l5 = new JLabel("MQ Channel");
   JLabel l6 = new JLabel("MQ Username");
   JLabel l7 = new JLabel("MQ Password");
   layout.setAutoCreateGaps(true);
   layout.setAutoCreateContainerGaps(true);
   GroupLayout.SequentialGroup hGroup = 
      layout.createSequentialGroup();
   hGroup.addGroup(layout.createParallelGroup().
   addComponent(l1).
   addComponent(l2).
   addComponent(l3).
   addComponent(l4).
   addComponent(l5).
   addComponent(l6).
   addComponent(l7));

   hGroup.addGroup(layout.createParallelGroup().

   addComponent(mqSenderHost).
   addComponent(mqSenderManager).
   addComponent(mqSenderQueue).
   addComponent(mqSenderChannel).
   addComponent(mqSenderUser).
   addComponent(mqSenderPassword)
            );
   layout.setHorizontalGroup(hGroup);

   GroupLayout.SequentialGroup vGroup = 
      layout.createSequentialGroup();
     vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
            addComponent(l1).addComponent(mqSenderHost));
  vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
            addComponent(l2).addComponent(mqSenderPort));
  vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
            addComponent(l3).addComponent(mqSenderManager));
  vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
            addComponent(l4).addComponent(mqSenderQueue));
  vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
            addComponent(l5).addComponent(mqSenderChannel));
  vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
            addComponent(l6).addComponent(mqSenderUser));
  vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
            addComponent(l7).addComponent(mqSenderPassword));
   
   layout.setVerticalGroup(vGroup);
   
   return layout;

}//End method



Method that will return listener setup layout.

private GroupLayout listenerSetup(GroupLayout layout){
   JLabel l1 = new JLabel("Enable JMS Reader");
   JLabel l2 = new JLabel("MQ Host Name"); 
   JLabel l3 = new JLabel("MQ Port Number");
   JLabel l4 = new JLabel("MQ Manager");
   JLabel l5 = new JLabel("MQ Queue");
   JLabel l6 = new JLabel("MQ Channel");
   JLabel l7 = new JLabel("MQ Username");
   JLabel l8 = new JLabel("MQ Password");
   listenerEnabledCheckbox.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent event) {
        if (!((JCheckBox) event.getSource()).isSelected()) {
        contentPane.remove(mainPanel);
        senderTa.setSize(40, 100);
        mainPanel = JPanelUtil.getJMSSenderOnlyPanel(senderTa);
        mainPanel.setBorder(BorderFactory.createMatteBorder(5, 5, 5, 5, Color.white));
        contentPane.add(mainPanel, BorderLayout.CENTER);
        frame.setSize(1000, 800);
        frame.setBounds(50, 50, 800, 600);
        frame.pack();
       
        listenBtn.setEnabled(false);
    suspendBtn.setEnabled(false);
   
        SetupUIUtil.enableListenerSetupFields(
        false,
        mqListenerHost,
        mqListenerPort,
        mqListenerManager,
        mqListenerQueue,
        mqListenerChannel,
        mqListenerUser,
        mqListenerPassword);
       
        if( jmsThread != null && jmsThread.isThreadAlive()){
            jmsThread.suspend();
            }
       
        } else {
        contentPane.remove(mainPanel);
        senderTa.setSize(20, 100);
        receiverTa.setSize(20, 100);
        mainPanel = JPanelUtil.getJMSSenderAndListenerPanel(senderTa, receiverTa);
        mainPanel.setBorder(BorderFactory.createMatteBorder(5, 5, 5, 5, Color.white));
        contentPane.add(mainPanel, BorderLayout.CENTER);
        frame.setSize(1000, 800);
        frame.setBounds(50, 50, 800, 600);
        frame.pack();
       
        listenBtn.setEnabled(true);
    suspendBtn.setEnabled(false);
   
        SetupUIUtil.enableListenerSetupFields(
        true,
        mqListenerHost,
        mqListenerPort,
        mqListenerManager,
        mqListenerQueue,
        mqListenerChannel,
        mqListenerUser,
        mqListenerPassword);
        }
    }
});
  layout.setAutoCreateGaps(true);
  layout.setAutoCreateContainerGaps(true);
  GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();

  hGroup.addGroup(layout.createParallelGroup().
            addComponent(l1).
            addComponent(l2).
            addComponent(l3).
            addComponent(l4).
            addComponent(l5).
            addComponent(l6).
            addComponent(l7).
            addComponent(l8));
  hGroup.addGroup(layout.createParallelGroup().
            addComponent(listenerEnabledCheckbox).
            addComponent(mqListenerHost).
            addComponent(mqListenerPort).
            addComponent(mqListenerManager).
            addComponent(mqListenerQueue).
            addComponent(mqListenerChannel).
            addComponent(mqListenerUser).
            addComponent(mqListenerPassword));
  layout.setHorizontalGroup(hGroup);
   
  GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();

  vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
            addComponent(l1).addComponent(listenerEnabledCheckbox));
  vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
            addComponent(l2).addComponent(mqListenerHost));
  vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
            addComponent(l3).addComponent(mqListenerPort));
  vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
            addComponent(l4).addComponent(mqListenerManager));
  vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
            addComponent(l5).addComponent(mqListenerQueue));
  vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
            addComponent(l6).addComponent(mqListenerChannel));
  vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
            addComponent(l7).addComponent(mqListenerUser));
  vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
            addComponent(l8).addComponent(mqListenerPassword));
   
   layout.setVerticalGroup(vGroup);
   
   return layout;
}//End method




No comments:

Post a Comment