Article View: borland.public.jbuilder.java.swing
Article #11307Re: JTable Columns
From: Quach Hoa Thanh
Date: Thu, 17 Jun 1999 00:00
Date: Thu, 17 Jun 1999 00:00
117 lines
4951 bytes
4951 bytes
Thanks again steve for the snippet of code. I really appreciate it.
I don't know where you are. If you happen to be in Halifax Nova Scotia, Canada, give me a
shout. I will drown you with beers.
Thanh
Steve Horsley wrote:
> 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;
> }
> }
> //=======================================
>
Technical Details
Message-ID:
<37696D06.DF82D27A@ns.sympatico.ca>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<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> <376767D8.AA5D766C@cwcom.co.uk>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,eb4b6527d333ff79 X-Google-Attributes: gid111d45,public From: Quach Hoa Thanh <thq.qua@ns.sympatico.ca> Subject: Re: JTable Columns Date: 1999/06/17 Message-ID: <37696D06.DF82D27A@ns.sympatico.ca> X-Deja-AN: 490827900 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> <376767D8.AA5D766C@cwcom.co.uk> Content-Type: text/plain; charset=us-ascii Organization: Self Employed Mime-Version: 1.0 Reply-To: thq.qua@ns.sympatico.ca Newsgroups: borland.public.jbuilder.java.swing