import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Registration extends JFrame
{
JLabel jlblName1, jlblName2, jlblNRIC, jlblStudentID, jlblStaffID, jlblDepartment1, jlblDepartment2, jlblUsername1, jlblUsername2, jlblPassword1, jlblPassword11, jlblPassword2, jlblImg;
JPasswordField jtfPassword1, jtfPassword11, jtfPassword2;
JTextField jtfname1, jtfname2, jtfNRIC, jtfID1, jtfID2, jtfUsername1, jtfUsername2;
JButton jbtnUpload, jbtnRegister1, jbtnRegister2, jbtnCheck;
JComboBox jcbDepartment1, jcbDepartment2;
JTabbedPane jtpPanel;
JPanel jp1, jp2;
public Registration()
{
setSize(300,550);
setTitle("Registration");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jlblName1 = new JLabel("Name:");
jlblName2 = new JLabel("Name:");
jlblNRIC = new JLabel("NRIC:");
jlblStudentID = new JLabel("Student ID:");
jlblStaffID = new JLabel("Staff ID:");
jlblDepartment1 = new JLabel("Department:");
jlblDepartment2 = new JLabel("Department:");
jlblUsername1 = new JLabel("username:");
jlblUsername2 = new JLabel("username:");
jlblPassword1 = new JLabel("password:");
jlblPassword11 = new JLabel("Retype:");
jlblPassword2 = new JLabel("password:");
ImageIcon img = new ImageIcon("DogShirt.gif");
jlblImg = new JLabel(img);
jtfname1 = new JTextField(20);
jtfname2 = new JTextField(20);
jtfNRIC = new JTextField(20);
jtfID1 = new JTextField(15);
jtfID2 = new JTextField(15);
jtfUsername1 = new JTextField(8);
jtfUsername2 = new JTextField(8);
jtfPassword1 = new JPasswordField(8);jtfPassword1.setEchoChar('*');
jtfPassword11 = new JPasswordField(8);jtfPassword11.setEchoChar('*');
jtfPassword2 = new JPasswordField(8);jtfPassword2.setEchoChar('*');
jcbDepartment1 = new JComboBox();
jcbDepartment1.addItem("Engineering");
jcbDepartment1.addItem("Business");
jcbDepartment1.addItem("Applied Sciences");
jcbDepartment2 = new JComboBox();
jcbDepartment2.addItem("Engineering");
jcbDepartment2.addItem("Business");
jcbDepartment2.addItem("Applied Sciences");
jbtnUpload = new JButton("Upload Picture");
jbtnRegister1 = new JButton("Register");
jbtnRegister2 = new JButton("Register");
jbtnCheck = new JButton("Confirm");
jbtnUpload.addActionListener(new UploadListener());
JPanel jpFlow = new JPanel(new FlowLayout(FlowLayout.LEFT));jpFlow.add(jlblName1);jpFlow.add(jtfname1);
JPanel jpFlow1 = new JPanel(new FlowLayout(FlowLayout.LEFT));jpFlow1.add(jlblNRIC);jpFlow1.add(jtfNRIC);
JPanel jpFlow2 = new JPanel(new FlowLayout(FlowLayout.LEFT));jpFlow2.add(jlblStudentID);jpFlow2.add(jtfID1);
JPanel jpFlow3 = new JPanel(new FlowLayout(FlowLayout.LEFT));jpFlow3.add(jlblDepartment1);jpFlow3.add(jcbDepartment1);
JPanel jpFlow4 = new JPanel(new FlowLayout(FlowLayout.LEFT));jpFlow4.add(jlblUsername1);jpFlow4.add(jtfUsername1);
JPanel jpFlow5 = new JPanel(new FlowLayout(FlowLayout.LEFT));jpFlow5.add(jlblPassword1);jpFlow5.add(jtfPassword1);
JPanel jpFlow6 = new JPanel(new FlowLayout(FlowLayout.LEFT));jpFlow6.add(jlblPassword11);jpFlow6.add(jtfPassword11);jpFlow6.add(jbtnCheck);
JPanel jpGrid = new JPanel();
jpGrid.setLayout(new GridLayout(7,1,0,20));
jpGrid.add(jpFlow);
jpGrid.add(jpFlow1);
jpGrid.add(jpFlow2);
jpGrid.add(jpFlow3);
jpGrid.add(jpFlow4);
jpGrid.add(jpFlow5);
jpGrid.add(jpFlow6);
JPanel jpFlow7 = new JPanel(new FlowLayout());
jpFlow7.add(jlblImg);
JPanel jpFlow8 = new JPanel(new FlowLayout());
jpFlow8.add(jbtnUpload);
jpFlow8.add(jbtnRegister1);
jp1 = new JPanel(new BorderLayout());
jp1.add(jpGrid, "North");
jp1.add(jpFlow7, "Center");
jp1.add(jpFlow8, "South");
jp2 = new JPanel();
jp2.setLayout(new FlowLayout(FlowLayout.LEFT,10,15));
jp2.add(jlblName2);
jp2.add(jtfname2);
jp2.add(jlblStaffID);
jp2.add(jtfID2);
jp2.add(jlblDepartment2);
jp2.add(jcbDepartment2);
jp2.add(jlblUsername2);
jp2.add(jtfUsername2);
jp2.add(new JLabel());jp2.add(new JLabel());
jp2.add(new JLabel());jp2.add(new JLabel());
jp2.add(new JLabel());jp2.add(new JLabel());
jp2.add(jlblPassword2);
jp2.add(jtfPassword2);
jp2.add(new JLabel());jp2.add(new JLabel());
jp2.add(new JLabel());jp2.add(new JLabel());
jp2.add(new JLabel());jp2.add(new JLabel());
jp2.add(new JLabel());jp2.add(new JLabel());
jp2.add(new JLabel());jp2.add(new JLabel());
jp2.add(new JLabel());jp2.add(new JLabel());
jp2.add(new JLabel());jp2.add(new JLabel());
jp2.add(new JLabel());jp2.add(new JLabel());
jp2.add(new JLabel());jp2.add(new JLabel());jp2.add(new JLabel());
jp2.add(jbtnRegister2);
jtpPanel = new JTabbedPane();
jtpPanel.addTab("Student", null, jp1, "");
jtpPanel.addTab("Staff", null, jp2, "");
add(jtpPanel);
setVisible(true);
}
class UploadListener implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
FileChooser fc = new FileChooser();
fc.FileChooser();
}
}
public static void main(String[] args)
{
Registration r = new Registration();
}
}
And here is another class of FileChooser
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.io.*;
import javax.swing.filechooser.*;
public class FileChooser extends JFrame implements ActionListener
{
JLabel file;
JButton browse, cancel, ok;
JTextField field3;
JPanel jpBrowser1, jpBrowser2;
JFileChooser fc;
public void FileChooser() {
setTitle("Upload Picture");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
setSize(400,110);
fc = new JFileChooser();
file = new JLabel("File");
browse = new JButton("Browse");
cancel = new JButton("Cancel");
ok = new JButton("Ok");
field3 = new JTextField(20);
browse.addActionListener(this);
jpBrowser1 = new JPanel();
jpBrowser1.setLayout(new FlowLayout());
jpBrowser2 = new JPanel();
jpBrowser2.setLayout(new FlowLayout());
jpBrowser1.add(file);
jpBrowser1.add(field3);
jpBrowser1.add(browse);
jpBrowser2.add(cancel);
jpBrowser2.add(ok);
add(jpBrowser1, "North");
add(jpBrowser2, "East");
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == browse) {
int returnVal = fc.showOpenDialog(FileChooser.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
field3.setText(file.getName());
}
}
}
}
So my question is when you run the program, you could see that the gap between 'Name' and 'NRIC', 'NRIC' and 'Student ID', and so on, is so big, how can i actually decrease the gap?
I've tried to use the 'setLayout(new GridLayout(7,1,0,20)'
hoping that int 20 will adjust the gap. But it was to no avail.
Secondly, when I click on 'upload' button, a 'upload picture' window for us to choose a file later on will pop up.
My question here is when user clicks on the 'cancel' button, what method can I implement to close the 'upload picture' window and return to previous one?
Thirdly, when user is touching on 'upload picture' window, user can actually navigate around the 'registration' window.
How can I prevent such condition? Maybe like, freezing the 'registration' window until the 'upload picture' has been closed.
Thx for helping.

New Topic/Question
This topic is locked



MultiQuote




|