I have implemented the addActionlistener for the Add Contact button in this GUI setup (please try running this code in your computer to know what i am talking about) and when i click on Add contact, the new frame pops up, i enter the details in the New contact frame, and click on save, now i want those details to be displayed in the table in the Address book frame when i click on save. Please look at btnSave action listener for what happens when i click on Save. Please help.

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

import javax.swing.*;
import javax.swing.table.*;

import java.awt.event.ActionEvent;
import java.text.*;
import java.awt.Dimension;


class Addressbk extends JFrame implements ActionListener
{
	FlowLayout leftLayout;
	JFrame winNew;
	
	private static JPanel pTitle;
	private static JPanel pName;
	private static JPanel pAddress;
	private static JPanel pHPhone;
	private static JPanel pMPhone;
	private static JPanel pButtons;

	private static JLabel lblTitle;
	private static JLabel lblName;
	private static JLabel lblAddress;
	private static JLabel lbHPhone;
	private static JLabel lbMPhone;

	private static JTextField txtName;
	private static JTextField txtAddress;
	private static JTextField txtHPhone;
	private static JTextField txtMPhone;
	
	private static JButton btnAdd;
	private static JButton btnDel;
	private static JButton btnImp;
	private static JButton btnExp;
	private static JButton btnSave;
	
	ArrayList<Person> people;
	DefaultTableModel table_model;
	JTable jt;
	
	public Addressbk()
	{
		toolbar();
		table_display();
	
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle("Address Book");
		validate();
		setSize(800, 600);
		setLocationRelativeTo(null);
		setVisible(true);
		
	}
	
	//---------------------------------------GUI methods------------------------------------------
	public void toolbar()
	{
		//--------------File Menu----------------
		JMenuBar mb = new JMenuBar();
		setJMenuBar (mb);
		JMenu fm = new JMenu("File");
		mb.add(fm);
		JMenuItem ei = new JMenuItem("Exit");
		mb.add(ei);
		ei.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent AE)
			{
				System.exit(0);
			}
		});
		fm.add(ei);
		//----------------End File Menu-----------
		
		JToolBar jtb=new JToolBar();
        jtb.setLayout(new GridLayout(5,1));
        add(jtb, BorderLayout.WEST);

        //--------------Buttons-------------------
        btnAdd = new JButton("Add Contact");
        jtb.add(btnAdd);
        btnAdd.addActionListener(this);
        
        btnDel = new JButton("Delete Contact");
        jtb.add(btnDel);
		btnImp = new JButton("Import Contacts");
		jtb.add(btnImp);
		btnExp = new JButton("Export Contacts");
		jtb.add(btnExp);
		//---------------End Buttons--------------
	
	}
	
	public void actionPerformed(ActionEvent e) 
	{
		if(e.getSource() == btnAdd)
			addContact();
		else if(e.getSource() == btnSave)
		{
			people.add(new Person(txtName.getText(),Integer.parseInt(txtHPhone.getText()),Integer.parseInt(txtMPhone.getText()),txtAddress.getText()));
			
		}
	}
	
	public void addContact()
	{
		final int WIDTH = 375;
		final int HEIGHT = 325;
	
		winNew = new JFrame("Add New Contact");
	
		leftLayout = new FlowLayout(FlowLayout.LEFT);
	
		pTitle = new JPanel();
		pTitle.setLayout(new FlowLayout(FlowLayout.CENTER));
		lblTitle = new JLabel("New Contact");
		pTitle.add(lblTitle);
		winNew.add(pTitle);
	
		winNew.setLayout(new GridLayout(6,1));
		winNew.setSize(WIDTH, HEIGHT);
		winNew.setBackground(Color.BLUE);
	
		pName = new JPanel();
		pName.setLayout(leftLayout);
		lblName = new JLabel("Name: ");
		txtName = new JTextField(20);
		winNew.add(pName);
		pName.add(lblName);
		pName.add(txtName);
	
		pAddress = new JPanel();
		pAddress.setLayout(leftLayout);
		lblAddress = new JLabel("Address: ");
		txtAddress = new JTextField(30);
		winNew.add(pAddress);
		pAddress.add(lblAddress);
		pAddress.add(txtAddress);
	
		pHPhone = new JPanel();
		pHPhone.setLayout(leftLayout);
		lbHPhone = new JLabel("Home Phone Number: ");
		txtHPhone = new JTextField(12);
		winNew.add(pHPhone);
		pHPhone.add(lbHPhone);
		pHPhone.add(txtHPhone);
		
		pMPhone = new JPanel();
		pMPhone.setLayout(leftLayout);
		lbMPhone = new JLabel("Mobile Phone Number: ");
		txtMPhone = new JTextField(12);
		winNew.add(pMPhone);
		pMPhone.add(lbMPhone);
		pMPhone.add(txtMPhone);
	
	
		pButtons = new JPanel();
	
		btnSave = new JButton("Save");
		pButtons.add(btnSave);
		btnSave.addActionListener(this);
	
		winNew.add(pButtons);
	
		winNew.setVisible(true);
	}
	
	public void table_display()
	{
		setSize(new Dimension(600, 600));
		setLayout(new FlowLayout());
		
		people = new ArrayList<Person>();
		
		people.add(0,new Person("Hello1",3,4,"Yo3"));
		people.add(1,new Person("Hello2",5,6,"Yo1"));

		
		Object[][] data = new Object[people.size()][4];
		
		for (int i = 0; i < people.size(); i++) {
			data[i][0] = people.get(i).name;
			data[i][1] = people.get(i).Hphone;
			data[i][2] = people.get(i).Mphone;
			data[i][3] = people.get(i).address;
		}
		
		table_model = new DefaultTableModel(data, new String[]{"Name", "Home Phone", "Mobile Phone", "Address"});
		jt = new JTable(table_model);
		add(new JScrollPane(jt, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS));
		
		TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(table_model);
		sorter.setComparator(1, new Comparator<Integer>() {
			 public int compare(Integer o1, Integer o2)
			 {
				 return o1.compareTo(o2);
			 }
				 
			 public boolean equals(Object obj)
			 {
				 return obj.equals(this);
			 }
		});
		jt.setRowSorter(sorter); 
		
		setVisible(true);
		validate();
	}
	
	
	
	//-----------------------------------End GUI methods------------------------------------------
	
	
        class Person
        {
            String name;
            int Hphone;
            int Mphone;
            String address;
            
            public Person(String n, int hp, int mp, String add)
            {
            	name = n;
            	Hphone = hp;
            	Mphone = mp;
            	address = add;            	
            }
        }
        



	class Editable_Table_Model extends AbstractTableModel
	{
		private String [] columnNames ={"Name", "Home phone", "Mobile phone", "Home address"};
		String tmp_col1 = "", tmp_col2 = "";
		public int getColumnCount()
		{
			return columnNames.length;
		}
		public int getRowCount()
		{
			return people.size();
		}
		public Object getValueAt(int row, int col)
		{
			/*
			 * String values[][] = new String{{"row 1 column1","row 1 column 2"},  {"row2 - column1", "row2 - column 2"}};
			 * public Object getValueAt(int row, int col){
			 * return values[row][col];
			 * }
			 * getValueAt(0,1)
			 * Row 1 - Column 2
			 */
			if (row == people.size()) {
				if (col == 0)
					return tmp_col1;
				else
					return tmp_col2;
			}	
			Person p = people.get(row);
				//if(col ==0)
				//	return p.name;
		//	else
			//	return p.Haddress;
					return p;
		}
		
		
		
		public String getColumnName(int c)
	{
			return columnNames[c];
		//return new String[]{"Name", "Home phone", "Mobile phone", "home address", "E-mail Address"}[c];
	}
		public boolean isCellEditable(int row, int col)
		{
			return true;
		}
	
	}
	
	
	
	public static void main (String [] args)
	{
		new Addressbk();
	}
}

Recommended Answers

All 2 Replies

I didn't tried that....

1/ remove all static declarations for JComponents
2/ probably you need to put JButtons to JPanel instead of JToolbar, or put Menu to JFrame
3/ change popup JFrame to JDialog, isn't need to create everytimes new one, built that on start with setVisible(false) and if you need to show InputDialog with setVisible(true) plus reset value for JTestFields before visible
4/ very contraproductive is parsing int (phoneNo), change that to JFormattedJTextField with InputMask
5/ you never add new row to JTable
6/ your TableComparator become from Java 1.3, remove that, just add RowSorter to JTable
7/ your TableModel doesn't works and isn't wrote correct way, remove that, because you never will be add new row, you never will be need extends Jtable with TableModel for those code, if yes then with usage of DeafaultTableModel

Please help me with some code, as it is hard for me to understand what you're trying to say.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.