On this assignment we have to add the function to add members to a JTable
The code is divided in 4 clases.
Screenshot:

Source CODE Main:
/*
* Pablo Suzarte
* Assignment 2
* JTable
* Interaction Design
* Malmö University
* contact me: pablosu@gmail.com
*/
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
@SuppressWarnings("serial")
public class Main extends JFrame {
private JPanel contentPane;
private JTable table;
private DefaultTableModel model;
private JMenuBar menuBar;
private JMenu mnNewMenu;
private JMenuItem mntmNew;
private JMenuItem mntmOpen;
private JMenuItem mntmClose;
private JMenu mnNewMenu_1;
private JMenuItem mntmNewMember;
private JMenuItem mntmFindMember;
private JTextField textField;
private JButton Search;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
// this add a new member on the top table
public void addNewMember(String name, String idnumber, String address, String phone, String email) {
model.insertRow(0, new Object[] {
name,
idnumber,
address,
phone,
email
});
}
/**
* Create the frame.
*/
public Main() {
setTitle("Friskis och Svettis");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 600);
menuBar = new JMenuBar();
setJMenuBar(menuBar);
mnNewMenu = new JMenu("File");
mnNewMenu.setFont(new Font("Segoe UI", Font.PLAIN, 16));
menuBar.add(mnNewMenu);
mntmNew = new JMenuItem("New");
mntmNew.setIcon(new ImageIcon(Main.class.getResource("/images/1326824571_filenew.png")));
mntmNew.setFont(new Font("Segoe UI", Font.PLAIN, 16));
mnNewMenu.add(mntmNew);
mntmOpen = new JMenuItem("Open");
mntmOpen.setIcon(new ImageIcon(Main.class.getResource("/images/1326824544_folder_horizontal_open.png")));
mntmOpen.setFont(new Font("Segoe UI", Font.PLAIN, 16));
mnNewMenu.add(mntmOpen);
mntmClose = new JMenuItem("Close");
mntmClose.setIcon(new ImageIcon(Main.class.getResource("/images/1326824461_button_cancel.png")));
mntmClose.addActionListener(new ActionListener() {
//Exit the Application via pressing the exit button on the drop menu
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
mntmClose.setFont(new Font("Segoe UI", Font.PLAIN, 16));
mnNewMenu.add(mntmClose);
Component horizontalStrut = Box.createHorizontalStrut(12);
menuBar.add(horizontalStrut);
mnNewMenu_1 = new JMenu("Members");
mnNewMenu_1.setFont(new Font("Segoe UI", Font.PLAIN, 16));
menuBar.add(mnNewMenu_1);
mntmNewMember = new JMenuItem("New Member");
mntmNewMember.setIcon(new ImageIcon(Main.class.getResource("/images/1326824652_business-contact.png")));
mntmNewMember.setFont(new Font("Segoe UI", Font.PLAIN, 16));
mntmNewMember.addActionListener(new ActionListener() {
//Open Add New Member button
public void actionPerformed(ActionEvent e) {
newMember addNewMember = new newMember(Main.this);
addNewMember.setVisible(true);
}
});
mnNewMenu_1.add(mntmNewMember);
mntmFindMember = new JMenuItem("Find Member");
mntmFindMember.setIcon(new ImageIcon(Main.class.getResource("/images/1326824635_search_16.png")));
mntmFindMember.addActionListener(new ActionListener() {
// open the search for a member from the drop menu "members"
public void actionPerformed(ActionEvent e) {
Find findContact = new Find();
findContact.setVisible(true);
}
});
mntmFindMember.setFont(new Font("Segoe UI", Font.PLAIN, 16));
mnNewMenu_1.add(mntmFindMember);
Component horizontalStrut_1 = Box.createHorizontalStrut(12);
menuBar.add(horizontalStrut_1);
JMenu mnHelp = new JMenu("Help");
mnHelp.setFont(new Font("Segoe UI", Font.PLAIN, 16));
menuBar.add(mnHelp);
JMenuItem mntmAbout = new JMenuItem("About");
mntmAbout.setIcon(new ImageIcon(Main.class.getResource("/images/1326838484_Information.png")));
mntmAbout.addActionListener(new ActionListener() {
//Open the about window
public void actionPerformed(ActionEvent arg0) {
About about = new About();
about.setVisible(true);
}
});
mntmAbout.setFont(new Font("Segoe UI", Font.PLAIN, 16));
mnHelp.add(mntmAbout);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 122, 774, 334);
table = new JTable();
model = new DefaultTableModel(
new Object[][] {},
new String[] {
"Name", "ID Number", "Address", "Phone", "E-mail"
}
);
table.setModel(model);
scrollPane.setViewportView(table);
JLabel label = new JLabel("");
label.setIcon(new ImageIcon(Main.class.getResource("/images/1326716854_physical_education.png")));
JLabel lblFriskisOchSvettis = new JLabel("Friskis och Svettis");
lblFriskisOchSvettis.setIcon(new ImageIcon(Main.class.getResource("/images/48x48-f-o-s.png")));
lblFriskisOchSvettis.setForeground(Color.BLACK);
lblFriskisOchSvettis.setFont(new Font("Tahoma", Font.PLAIN, 24));
textField = new JTextField();
textField.setColumns(10);
Search = new JButton("");
Search.setFont(new Font("Tahoma", Font.PLAIN, 16));
Search.setIcon(new ImageIcon(Main.class.getResource("/images/1326824635_search_16.png")));
Search.setSelectedIcon(new ImageIcon(Main.class.getResource("/images/1326824635_search_16.png")));
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(3)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, 249, GroupLayout.PREFERRED_SIZE))
.addComponent(lblFriskisOchSvettis, GroupLayout.PREFERRED_SIZE, 252, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(Search, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(label, GroupLayout.PREFERRED_SIZE, 200, GroupLayout.PREFERRED_SIZE))
.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 753, GroupLayout.PREFERRED_SIZE))
.addContainerGap(11, Short.MAX_VALUE))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addComponent(label, GroupLayout.PREFERRED_SIZE, 107, GroupLayout.PREFERRED_SIZE)
.addGroup(Alignment.LEADING, gl_contentPane.createSequentialGroup()
.addContainerGap()
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addComponent(Search)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(lblFriskisOchSvettis, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE)
.addGap(30)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(ComponentPlacement.RELATED, 10, Short.MAX_VALUE)))
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 403, GroupLayout.PREFERRED_SIZE))
);
contentPane.setLayout(gl_contentPane);
}
}
Source CODE NewMember:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.SystemColor;
import javax.swing.UIManager;
import javax.swing.ImageIcon;
import java.awt.Toolkit;
public class newMember extends JDialog {
private final JPanel contentPanel = new JPanel();
private JTextField textName;
private JTextField textIdnumber;
private JTextField textAddress;
private JTextField textPhone;
private JTextField textEmail;
//private main parent;
//private main myMain = parent;
public static void main(String[] args) {
try {
newMember dialog = new newMember(null);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public newMember(JFrame parent) { // Still don't get this parameter hwo works
setIconImage(Toolkit.getDefaultToolkit().getImage(newMember.class.getResource("/images/48x48-f-o-s.png")));
final Main minParent = (Main) parent; //Still don't get what is this
setBounds(100, 100, 558, 418);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setBackground(SystemColor.window);
panel.setBounds(0, 0, 542, 347);
contentPanel.add(panel);
textName = new JTextField();
textName.setColumns(10);
textName.setBounds(115, 38, 215, 20);
panel.add(textName);
textIdnumber = new JTextField();
textIdnumber.setColumns(10);
textIdnumber.setBounds(115, 69, 215, 20);
panel.add(textIdnumber);
textAddress = new JTextField();
textAddress.setColumns(10);
textAddress.setBounds(115, 100, 215, 20);
panel.add(textAddress);
textPhone = new JTextField();
textPhone.setColumns(10);
textPhone.setBounds(115, 131, 215, 20);
panel.add(textPhone);
textEmail = new JTextField();
textEmail.setColumns(10);
textEmail.setBounds(115, 162, 215, 20);
panel.add(textEmail);
JLabel lblName = new JLabel("Name : ");
lblName.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblName.setBounds(48, 33, 57, 20);
panel.add(lblName);
JLabel lblIdNumber = new JLabel("ID Number : ");
lblIdNumber.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblIdNumber.setBounds(10, 69, 95, 17);
panel.add(lblIdNumber);
JLabel lblAddress = new JLabel("Address : ");
lblAddress.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblAddress.setBounds(32, 99, 73, 19);
panel.add(lblAddress);
JLabel lblPhone = new JLabel("Phone : ");
lblPhone.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblPhone.setBounds(45, 131, 60, 17);
panel.add(lblPhone);
JLabel lblEmail = new JLabel("E-Mail : ");
lblEmail.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblEmail.setBounds(45, 160, 60, 20);
panel.add(lblEmail);
JButton btnNewButton = new JButton("add photo");
btnNewButton.setIcon(new ImageIcon(newMember.class.getResource("/images/1326991898_user_48.png")));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
btnNewButton.setBounds(355, 37, 154, 145);
panel.add(btnNewButton);
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton("Save");
okButton.setIcon(new ImageIcon(newMember.class.getResource("/images/1326826169_stock_save.png")));
okButton.setFont(new Font("Tahoma", Font.PLAIN, 16));
okButton.addActionListener(new ActionListener() {
// when you press the Save button this action save the data on the correct cell on the jTable
public void actionPerformed(ActionEvent e) {
minParent.addNewMember(textName.getText(), textIdnumber.getText(), textAddress.getText(), textPhone.getText(), textEmail.getText());
setVisible(false);
dispose();
}
});
okButton.setActionCommand("Save");
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
{
JButton cancelButton = new JButton("Cancel");
cancelButton.setIcon(new ImageIcon(newMember.class.getResource("/images/1326826193_Cancel.png")));
cancelButton.setFont(new Font("Tahoma", Font.PLAIN, 16));
cancelButton.addActionListener(new ActionListener() {
// when you press the Cancel Button just the New Member window closes and not the whole program
public void actionPerformed(ActionEvent e) {
setVisible(false);
dispose();
}
});
cancelButton.setActionCommand("Cancel");
buttonPane.add(cancelButton);
}
}
}
}
Source CODE Find:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import java.awt.Toolkit;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ImageIcon;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class Find extends JFrame {
private JPanel contentPane;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Find frame = new Find();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Find() {
setTitle("Find Member");
setIconImage(Toolkit.getDefaultToolkit().getImage(Find.class.getResource("/images/48x48-f-o-s.png")));
setBounds(100, 100, 513, 113);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
textField = new JTextField();
textField.setColumns(10);
JButton btnNewButton = new JButton("Search");
btnNewButton.setIcon(new ImageIcon(Find.class.getResource("/images/1326971810_xmag.png")));
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(101)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, 210, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(btnNewButton)
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap()
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
.addComponent(btnNewButton))
.addContainerGap(299, Short.MAX_VALUE))
);
contentPane.setLayout(gl_contentPane);
}
}
Source CODE About:
/*
* Pablo Suzarte
* Assignment 2
* JTable
* Interaction Design
* Malmö University
* contact me: pablosu@gmail.com
*/
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.Toolkit;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
@SuppressWarnings("serial")
public class About extends JDialog {
private final JPanel contentPanel = new JPanel();
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
About dialog = new About();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the dialog.
*/
public About() {
setTitle("About Develop by Pablo Suzarte");
setIconImage(Toolkit.getDefaultToolkit().getImage(About.class.getResource("/images/friskis_o_svettis_78548586_116130886.gif")));
setBounds(100, 100, 450, 300);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.EAST);
JLabel lblDevelopByPablo = new JLabel("Develop by Pablo Suzarte \u00A9 2011");
lblDevelopByPablo.setFont(new Font("Tahoma", Font.PLAIN, 16));
JLabel lblpablosu = new JLabel("@pablosu");
lblpablosu.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblpablosu.setIcon(new ImageIcon(About.class.getResource("/images/48x48_icontexto-inside-twitter.png")));
JLabel label = new JLabel("@pablosu");
label.setIcon(new ImageIcon(About.class.getResource("/images/1326989302_icontexto-inside-facebook.png")));
label.setFont(new Font("Tahoma", Font.PLAIN, 14));
JLabel label_1 = new JLabel("@pablosu");
label_1.setIcon(new ImageIcon(About.class.getResource("/images/1326991198_icontexto-inside-linkedin.png")));
label_1.setFont(new Font("Tahoma", Font.PLAIN, 14));
JLabel lblNewLabel = new JLabel("");
lblNewLabel.setIcon(new ImageIcon(About.class.getResource("/images/1326990419_human-folder-development.png")));
JLabel label_2 = new JLabel("@pablosu");
label_2.setIcon(new ImageIcon(About.class.getResource("/images/1326989330_icontexto-inside-google.png")));
label_2.setFont(new Font("Tahoma", Font.PLAIN, 14));
JLabel lblFiskisSvetis = new JLabel("Fiskis & Svettis Software Version 0.1");
lblFiskisSvetis.setFont(new Font("Tahoma", Font.PLAIN, 23));
GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
gl_contentPanel.setHorizontalGroup(
gl_contentPanel.createParallelGroup(Alignment.TRAILING)
.addGroup(Alignment.LEADING, gl_contentPanel.createSequentialGroup()
.addGap(41)
.addGroup(gl_contentPanel.createParallelGroup(Alignment.TRAILING)
.addComponent(lblDevelopByPablo)
.addGroup(gl_contentPanel.createSequentialGroup()
.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPanel.createSequentialGroup()
.addComponent(lblpablosu)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(label, GroupLayout.PREFERRED_SIZE, 112, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_contentPanel.createSequentialGroup()
.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 112, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addComponent(label_2, GroupLayout.PREFERRED_SIZE, 112, GroupLayout.PREFERRED_SIZE)))
.addGap(18)
.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 119, GroupLayout.PREFERRED_SIZE))
.addGroup(Alignment.LEADING, gl_contentPanel.createSequentialGroup()
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(lblFiskisSvetis, GroupLayout.PREFERRED_SIZE, 375, GroupLayout.PREFERRED_SIZE)))
.addContainerGap(28, Short.MAX_VALUE))
);
gl_contentPanel.setVerticalGroup(
gl_contentPanel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPanel.createSequentialGroup()
.addContainerGap()
.addComponent(lblFiskisSvetis, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
.addGap(25)
.addGroup(gl_contentPanel.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_contentPanel.createSequentialGroup()
.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
.addComponent(label, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE)
.addComponent(lblpablosu))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
.addComponent(label_2, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE)
.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE)))
.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 102, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED, 53, Short.MAX_VALUE)
.addComponent(lblDevelopByPablo))
);
contentPanel.setLayout(gl_contentPanel);
}
}
Like this:
Like Loading...
Posted in Programming
|