GUI for the Framework
1. Make sure you have added "WindowBuider Pro "
2. Click on "Source "
// Use JDialog instead of JFrame for thread to pause until an event
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.AbstractButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.SpringLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.Box;
import javax.swing.JCheckBox;
import java.awt.Choice;
import javax.swing.JSpinner;
import javax.swing.JSeparator;
import java.awt.Panel;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.ButtonGroup;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
public class GUI2 extends JFrame {
private JTextField textField;
private JTextField password_field;
private JTextField email_field;
private JTextField data_field;
private JTextField textField_4;
private JTextField textField_5;
private final ButtonGroup buttonGroup = new ButtonGroup();
public String sUsername ,sPassword,sEmail,sTest_path,sRange_type="none",sPath;
public int iFrom,iTo;
public JFileChooser Filechoose;//=new JFileChooser();
public boolean bResume;
public File file;
PrintWriter writer;
/**
* @wbp.nonvisual location=72,219
*/
private final ButtonGroup buttonGroup_1 = new ButtonGroup();
/**
* Launch the application.
*/
public static void main(String[] args) {
// EventQueue.invokeLater(new Runnable() {
// public void run() {
// try {
GUI2 frame = new GUI2();
frame.setVisible(true);
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// });
}
/**
* Create the frame.
*/
public GUI2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 517, 475);
SpringLayout springLayout = new SpringLayout();
getContentPane().setLayout(springLayout);
JLabel lblUsername = new JLabel("Username");
springLayout.putConstraint(SpringLayout.NORTH, lblUsername, 27, SpringLayout.NORTH, getContentPane());
springLayout.putConstraint(SpringLayout.WEST, lblUsername, 10, SpringLayout.WEST, getContentPane());
getContentPane().add(lblUsername);
textField = new JTextField();
textField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(textField.getText());
}
});
springLayout.putConstraint(SpringLayout.NORTH, textField, -3, SpringLayout.NORTH, lblUsername);
springLayout.putConstraint(SpringLayout.WEST, textField, 6, SpringLayout.EAST, lblUsername);
getContentPane().add(textField);
textField.setColumns(10);
JLabel lblPassword = new JLabel("Password");
springLayout.putConstraint(SpringLayout.WEST, lblPassword, 0, SpringLayout.WEST, lblUsername);
getContentPane().add(lblPassword);
password_field = new JTextField();
password_field.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(password_field.getText());
}
});
springLayout.putConstraint(SpringLayout.SOUTH, lblPassword, 0, SpringLayout.SOUTH, password_field);
springLayout.putConstraint(SpringLayout.NORTH, password_field, 19, SpringLayout.SOUTH, textField);
springLayout.putConstraint(SpringLayout.EAST, password_field, 0, SpringLayout.EAST, textField);
getContentPane().add(password_field);
password_field.setColumns(10);
email_field = new JTextField();
springLayout.putConstraint(SpringLayout.NORTH, email_field, 19, SpringLayout.SOUTH, password_field);
email_field.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(email_field.getText());
}
});
getContentPane().add(email_field);
email_field.setColumns(10);
JLabel lblEmail = new JLabel("Email");
springLayout.putConstraint(SpringLayout.WEST, email_field, 6, SpringLayout.EAST, lblEmail);
springLayout.putConstraint(SpringLayout.SOUTH, lblEmail, 0, SpringLayout.SOUTH, email_field);
springLayout.putConstraint(SpringLayout.EAST, lblEmail, 0, SpringLayout.EAST, lblUsername);
getContentPane().add(lblEmail);
data_field = new JTextField();
springLayout.putConstraint(SpringLayout.WEST, data_field, 0, SpringLayout.WEST, textField);
springLayout.putConstraint(SpringLayout.NORTH, data_field, 143, SpringLayout.NORTH, getContentPane());
getContentPane().add(data_field);
data_field.setColumns(10);
JLabel lblDataPath = new JLabel("Data Path");
springLayout.putConstraint(SpringLayout.NORTH, lblDataPath, 24, SpringLayout.SOUTH, lblEmail);
springLayout.putConstraint(SpringLayout.WEST, lblDataPath, 0, SpringLayout.WEST, lblUsername);
getContentPane().add(lblDataPath);
final JRadioButton rdbtnAllTestCase = new JRadioButton("All Test Case");
rdbtnAllTestCase.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
if(rdbtnAllTestCase.isEnabled())
sRange_type="all";
}
});
buttonGroup.add(rdbtnAllTestCase);
springLayout.putConstraint(SpringLayout.WEST, rdbtnAllTestCase, 64, SpringLayout.WEST, getContentPane());
getContentPane().add(rdbtnAllTestCase);
final JRadioButton rdbtnNewRadioButton = new JRadioButton("Range");
rdbtnNewRadioButton.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
if (rdbtnNewRadioButton.isSelected()==true)
{
textField_4.setVisible(true);
textField_5.setVisible(true);
sRange_type="range";
}else if(rdbtnNewRadioButton.isSelected()==false)
{
textField_4.setVisible(false);
textField_5.setVisible(false);
}
}
});
buttonGroup.add(rdbtnNewRadioButton);
springLayout.putConstraint(SpringLayout.NORTH, rdbtnNewRadioButton, 235, SpringLayout.NORTH, getContentPane());
springLayout.putConstraint(SpringLayout.SOUTH, rdbtnAllTestCase, -2, SpringLayout.NORTH, rdbtnNewRadioButton);
springLayout.putConstraint(SpringLayout.WEST, rdbtnNewRadioButton, 64, SpringLayout.WEST, getContentPane());
getContentPane().add(rdbtnNewRadioButton);
final JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("Selective");
rdbtnNewRadioButton_1.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
if(rdbtnNewRadioButton_1.isSelected()==true)
sRange_type="selective";
}
});
buttonGroup.add(rdbtnNewRadioButton_1);
springLayout.putConstraint(SpringLayout.NORTH, rdbtnNewRadioButton_1, 6, SpringLayout.SOUTH, rdbtnNewRadioButton);
springLayout.putConstraint(SpringLayout.WEST, rdbtnNewRadioButton_1, 64, SpringLayout.WEST, getContentPane());
getContentPane().add(rdbtnNewRadioButton_1);
rdbtnAllTestCase.setSelected(true);
final JComboBox comboBox = new JComboBox();
comboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sRange_type="path";
}
});
JButton btnNewButton = new JButton("OK");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//String sUsername ,sPassword,sEmail,sTest_path;
sUsername=textField.getText().toString();
sPassword=password_field.getText().toString();
sEmail=email_field.getText().toString();
sTest_path=data_field.getText().toString();
//try{
if (sRange_type.equalsIgnoreCase("range"))
{
try{
iFrom=Integer.parseInt(textField_4.getText());
iTo=Integer.parseInt(textField_5.getText());
}catch(Throwable t)
{
JOptionPane.showMessageDialog(null, "Please enter valid integer values");
}
}else if (sRange_type.equalsIgnoreCase("path"))
{
sPath=(String) comboBox.getSelectedItem();
System.out.println(sTest_path+"\\"+sPath);
}
else if (sRange_type.equalsIgnoreCase("none") )
{
JOptionPane.showMessageDialog(null, "Please choose a value in the radio button");
}
if (sUsername.isEmpty()==false && sPassword.isEmpty()==false && sEmail.matches("(.*)@(.*)\\.(.*)")==true && sTest_path.isEmpty()==false && sRange_type.equalsIgnoreCase("none")==false )
{
System.out.println(sUsername+"\n"+sPassword+"\n"+sEmail+"\n"+sTest_path+"\n"+"Range="+sRange_type+"\n----Creating xml");
try {
writer = new PrintWriter("D:\\1.xml", "UTF-8");
writer.println("<username>"+sUsername+"</username>");
writer.println("<password>"+sPassword+"</password>");
writer.println("<email>"+sEmail+"</email>");
writer.println("<testdatapath>"+sTest_path+"\\"+sPath+"</testdatapath>");
writer.println("<selection>"+sRange_type+"</selection>");
writer.println(" <from>"+iFrom+" </from>");
writer.println("<to>"+iTo+"</to>");
writer.println("<resume>"+bResume+"</resume>");
writer.close();
System.out.println("-------------SUCCESS-----------");
} catch (Throwable t){
System.out.println(t.getCause()+"\n Error logging xml");
}
}
else
JOptionPane.showMessageDialog(null, "Please provide valid data ");
}
});
springLayout.putConstraint(SpringLayout.SOUTH, btnNewButton, -49, SpringLayout.SOUTH, getContentPane());
springLayout.putConstraint(SpringLayout.EAST, btnNewButton, 0, SpringLayout.EAST, rdbtnNewRadioButton);
getContentPane().add(btnNewButton);
JButton btnCancel = new JButton("Cancel");
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
springLayout.putConstraint(SpringLayout.NORTH, btnCancel, 0, SpringLayout.NORTH, btnNewButton);
springLayout.putConstraint(SpringLayout.WEST, btnCancel, 60, SpringLayout.EAST, btnNewButton);
getContentPane().add(btnCancel);
JButton btnHelp = new JButton("Help");
btnHelp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "---- Created by MARLABS (Reg) --------- ");
}
});
springLayout.putConstraint(SpringLayout.WEST, btnHelp, 56, SpringLayout.EAST, btnCancel);
springLayout.putConstraint(SpringLayout.SOUTH, btnHelp, 0, SpringLayout.SOUTH, btnNewButton);
getContentPane().add(btnHelp);
final JCheckBox chckbxNewCheckBox = new JCheckBox("Resume");
chckbxNewCheckBox.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
if(chckbxNewCheckBox.isEnabled())
bResume=true;
else
bResume=false;
}
});
springLayout.putConstraint(SpringLayout.SOUTH, chckbxNewCheckBox, -27, SpringLayout.NORTH, btnNewButton);
springLayout.putConstraint(SpringLayout.EAST, chckbxNewCheckBox, 0, SpringLayout.EAST, rdbtnNewRadioButton_1);
getContentPane().add(chckbxNewCheckBox);
textField_4 = new JTextField();
springLayout.putConstraint(SpringLayout.NORTH, textField_4, 1, SpringLayout.NORTH, rdbtnNewRadioButton);
springLayout.putConstraint(SpringLayout.WEST, textField_4, 79, SpringLayout.EAST, rdbtnNewRadioButton_1);
springLayout.putConstraint(SpringLayout.EAST, textField_4, 14, SpringLayout.EAST, btnCancel);
getContentPane().add(textField_4);
textField_4.setColumns(10);
textField_4.setVisible(false);
textField_5 = new JTextField();
springLayout.putConstraint(SpringLayout.NORTH, textField_5, 1, SpringLayout.NORTH, rdbtnNewRadioButton);
springLayout.putConstraint(SpringLayout.EAST, textField_5, -120, SpringLayout.EAST, getContentPane());
getContentPane().add(textField_5);
textField_5.setColumns(10);
textField_5.setVisible(false);
JLabel lblNewLabel_2 = new JLabel("To");
springLayout.putConstraint(SpringLayout.EAST, lblNewLabel_2, -173, SpringLayout.EAST, getContentPane());
springLayout.putConstraint(SpringLayout.WEST, textField_5, 5, SpringLayout.EAST, lblNewLabel_2);
springLayout.putConstraint(SpringLayout.NORTH, lblNewLabel_2, 4, SpringLayout.NORTH, rdbtnNewRadioButton);
getContentPane().add(lblNewLabel_2);
JLabel lblFrom = new JLabel("From");
springLayout.putConstraint(SpringLayout.NORTH, lblFrom, 4, SpringLayout.NORTH, rdbtnNewRadioButton);
springLayout.putConstraint(SpringLayout.EAST, lblFrom, -6, SpringLayout.WEST, textField_4);
getContentPane().add(lblFrom);
final JRadioButton rdbtnTestCasePath = new JRadioButton("Test Case Path");
springLayout.putConstraint(SpringLayout.NORTH, rdbtnTestCasePath, 19, SpringLayout.SOUTH, data_field);
JButton btnBrowse = new JButton("Browse");
springLayout.putConstraint(SpringLayout.NORTH, btnBrowse, 142, SpringLayout.NORTH, getContentPane());
springLayout.putConstraint(SpringLayout.EAST, data_field, -6, SpringLayout.WEST, btnBrowse);
springLayout.putConstraint(SpringLayout.EAST, btnBrowse, 0, SpringLayout.EAST, btnCancel);
btnBrowse.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
Filechoose=new JFileChooser();
Filechoose.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int retval=Filechoose.showOpenDialog(GUI2.this);
if (retval == JFileChooser.APPROVE_OPTION) {
//... The user selected a file, get it, use it.
sRange_type="path";
file = Filechoose.getSelectedFile();
data_field.setText(file.getPath());
rdbtnTestCasePath.setVisible(true);
//System.out.println(file.getPath());
}
}
});
getContentPane().add(btnBrowse);
springLayout.putConstraint(SpringLayout.NORTH, comboBox, 18, SpringLayout.SOUTH, btnBrowse);
springLayout.putConstraint(SpringLayout.WEST, comboBox, 13, SpringLayout.WEST, btnCancel);
springLayout.putConstraint(SpringLayout.EAST, comboBox, 124, SpringLayout.EAST, btnHelp);
comboBox.setVisible(false);
rdbtnTestCasePath.setVisible(false);
springLayout.putConstraint(SpringLayout.WEST, rdbtnTestCasePath, 63, SpringLayout.WEST, getContentPane());
rdbtnTestCasePath.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
if(rdbtnTestCasePath.isSelected())
{
sRange_type="path";
comboBox.removeAllItems();
comboBox.setVisible(true);
File[] listOfFiles = file.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isDirectory()) {
comboBox.addItem(listOfFiles[i].getName());//(listOfFiles[i].getName());
}
}
}
else
{
comboBox.setVisible(false);
comboBox.removeAllItems();
}
}
});
buttonGroup.add(rdbtnTestCasePath);
getContentPane().add(rdbtnTestCasePath);
getContentPane().add(comboBox);
}
}
No comments:
Post a Comment