🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #11288

Re: JTable Columns

#11288
From: Steve Horsley
Date: Wed, 16 Jun 1999 00:00
119 lines
3797 bytes
Your model needs to know the data has changed as well, because the
JTable queries the model for the new info. In this case, my Vector
is the database.

Look at the following working example...

//=======================================
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

class TableTest
{
	Vector tableContents;
	JTextField tf1, tf2;
	MyTableModel myTableModel;

	public static void main(String[] args) {
		TableTest tt = new TableTest();
	}

	TableTest() {	// constructor
		tableContents = new Vector();

		// construct the GUI

		JFrame frame = new JFrame("TableTest");
        	frame.addWindowListener(new WindowAdapter() {
            	public void windowClosing(WindowEvent e) {
                		System.exit(0);
            	}
		});
		Container contentPane = frame.getContentPane();
		JPanel panel = new JPanel() {
			public Insets getInsets() {
				return new Insets(10,10,10,10);
		}};
		panel.setLayout(new GridLayout(3, 2, 10, 10));
		JLabel lCol1 = new JLabel("Col 1 text : ", SwingConstants.RIGHT);
		JLabel lCol2 = new JLabel("Col 2 text : ", SwingConstants.RIGHT);
		tf1 = new JTextField();
		tf2 = new JTextField();
		JButton bAdd = new JButton("Add to table");
		bAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bAddPressed();
		}});

		panel.add(lCol1);
		panel.add(tf1);
		panel.add(lCol2);
		panel.add(tf2);
		panel.add(bAdd);

		// make the table
		myTableModel = new MyTableModel();
		JTable table = new JTable(myTableModel);
		JScrollPane sp = new JScrollPane(table);

		contentPane.add(panel, BorderLayout.NORTH);
		contentPane.add(sp, BorderLayout.CENTER);

		frame.pack();
		frame.setVisible(true);
	}

	void bAddPressed()
	{
		Entry newEntry = new Entry();
		newEntry.s1 = tf1.getText();
		newEntry.s2 = tf2.getText();
		int x = tableContents.size();
		tableContents.add(newEntry);
		myTableModel.fireTableRowsInserted(x, x);
	}

	class MyTableModel extends AbstractTableModel
	{
		String[] colNames = {"Col1", "Col2"};
		
		public int getRowCount() {
			return(tableContents.size()); 
		}
		public int getColumnCount(){ 
			return(2); 
		}
		public Object getValueAt(int row, int column) {
			if(column == 0) {
				return ((Entry) tableContents.elementAt(row)).s1;
			}
			if(column == 1) {
				return ((Entry) tableContents.elementAt(row)).s2;
			}
			return null;
		}
		public String getColumnName(int col) {
			return colNames[col];
		}
	}


	class Entry {
		String s1, s2;
	}
}
//=======================================

Quach Hoa Thanh wrote:
> 
> Hi Steve, thank you again for your help.
> 
> Have you successfully inserted a row into JTable at run-time like the way we add a
> record to the database. I tried your code and many other way I can think of, but so
> far failed in every attempt. I know of one way is to change the model attaching to
> JTable which contains different data.
> 
> Thanh
Technical Details
Message-ID: <376767D8.AA5D766C@cwcom.co.uk>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <3763E235.80724A4@ns.sympatico.ca> <3764C6B6.8F6FF6EE@cwcom.co.uk> <37657039.C7B77F6F@ns.sympatico.ca> <3765839E.AEB2697@ns.sympatico.ca> <37661363.BC903E98@cwcom.co.uk> <3766FF5D.81899083@ns.sympatico.ca>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,eb4b6527d333ff79
X-Google-Attributes: gid111d45,public
From: Steve Horsley <steve.horsley@cwcom.co.uk>
Subject: Re: JTable Columns
Date: 1999/06/16
Message-ID: <376767D8.AA5D766C@cwcom.co.uk>
X-Deja-AN: 490183000
Content-Transfer-Encoding: 7bit
References: <3763E235.80724A4@ns.sympatico.ca> <3764C6B6.8F6FF6EE@cwcom.co.uk> <37657039.C7B77F6F@ns.sympatico.ca> <3765839E.AEB2697@ns.sympatico.ca> <37661363.BC903E98@cwcom.co.uk> <3766FF5D.81899083@ns.sympatico.ca>
X-Accept-Language: en
Content-Type: text/plain; charset=us-ascii
Organization: Cable & Wireless Communications
Mime-Version: 1.0
Newsgroups: borland.public.jbuilder.java.swing