package periman;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
@SuppressWarnings("serial")
public class ConfidenceDialog extends JDialog implements ActionListener{
private final String NO_RADIO_BUTTON_SELECTED = "none";
String confidenceLevel = NO_RADIO_BUTTON_SELECTED;
JButton okBtn;
public ConfidenceDialog(JFrame parent, int scale, String msg, String leftCaption, String rightCaption) {
super(parent, true);
setLocation(450, 450);
createGui(scale, msg, leftCaption, rightCaption);
pack();
setVisible( true );
}
private void createGui(int scale, String msg, String leftCaption, String rightCaption) {
GridBagLayout layout = new GridBagLayout();
setLayout( layout );
// ***** scale header
GridBagConstraints headerConstraints = new GridBagConstraints();
headerConstraints.gridx = 0;
headerConstraints.gridy = 0;
headerConstraints.anchor = GridBagConstraints.CENTER;
headerConstraints.gridwidth = GridBagConstraints.REMAINDER;
headerConstraints.fill = GridBagConstraints.HORIZONTAL;
JLabel headerLbl = new JLabel( msg );
add( headerLbl, headerConstraints );
// ***** second line first element
GridBagConstraints leftConstraint = new GridBagConstraints();
leftConstraint.gridx = 0;
leftConstraint.gridy = 1;
JLabel leftJLbl = new JLabel( leftCaption );
add(leftJLbl, leftConstraint);
// **** second line second element - the panel with all the radio buttons and the labels on the sides.
GridBagConstraints cbPanelConstraint = new GridBagConstraints();
cbPanelConstraint.gridx = 1;
cbPanelConstraint.gridy = 1;
cbPanelConstraint.anchor = GridBagConstraints.CENTER;
cbPanelConstraint.weightx = 1.0;
JPanel cbPanel = new JPanel();
add( cbPanel, cbPanelConstraint );
ButtonGroup group = new ButtonGroup();
for(int i = 0 ; i<scale; i++)
{
JRadioButton radioBtn = new JRadioButton();
group.add( radioBtn );
radioBtn.setActionCommand( ""+(i+1) );
radioBtn.addActionListener( this );
cbPanel.add( radioBtn );
}
// ***** second line third element - the right label
GridBagConstraints rightConstraint = new GridBagConstraints();
rightConstraint.gridx = 2;
rightConstraint.gridy = 1;
JLabel rightJlbl = new JLabel( rightCaption );
add( rightJlbl, rightConstraint );
// ****** 3 row
GridBagConstraints okBtnConstraint = new GridBagConstraints();
okBtnConstraint.gridx = 1;
okBtnConstraint.gridy = 2;
okBtnConstraint.anchor = GridBagConstraints.CENTER;
//okBtnConstraint.gridwidth = GridBagConstraints.REMAINDER;
okBtn = new JButton("OK");
okBtn.setEnabled( false );
okBtn.addActionListener(this);
okBtn.setActionCommand("OK");
add(okBtn, okBtnConstraint);
}
public String getConfidenceLevel() {
return confidenceLevel;
}
@Override
public void actionPerformed(ActionEvent e) {
if( e.getActionCommand().equals( "OK" ) )
{
setVisible( false );
}
else
{
okBtn.setEnabled( true );
confidenceLevel = e.getActionCommand();
}
}
}