Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Java

Reply
 
LinkBack Thread Tools Display Modes
Old 06-10-2008, 02:48 PM   #1 (permalink)
Rotkiv
Code Monkey
 
Rotkiv's Avatar
 
Join Date: Apr 2004
Location: Silicon Valley, CA
Posts: 64
Rotkiv is on a distinguished road
Send a message via AIM to Rotkiv Send a message via MSN to Rotkiv
adding stuff to JTable without constructor

I'm trying to use a JTable to display a list of files and some information about them. actually at this point the fact that they're files doesn't matter that much.
I'm trying to display information via a JTable. however the only way i know how to get data to the JTable is through the constructor. So how would i append rows to or preferably completely replace the information in a JTable using a change listener?

here's where i've been stumped. I have many other classes which i hope are not relevant to this issue, unless my code should work... which i doubt. the JARmanager is another class in my program which manages a list of files and the getTable method returns a vector of object[]'s which contain a single string. the system.out line is for testing and the vector is being populated correctly.
Code:
import java.awt.BorderLayout;
import javax.swing.table.DefaultTableModel;
import javax.swing.event.*; //ChangeEvent, ChangeListener,
import javax.swing.*; //JPanel, JScrollPane, JTable, DefaultListSelectionModel,
import java.util.Vector;

public class TableView extends JPanel implements ChangeListener {
	Vector<String> columnNames = new Vector<String>();
	Vector data;
	JTable table;
	JScrollPane scroll;
	JARmanager jman;
	
	public TableView(JARmanager man) {
		columnNames.add("Name");
		columnNames.add("Type");
		data = man.getTable();
		jman = man;
		
		table  = new JTable(data, columnNames);
		table.setFillsViewportHeight(true);
		table.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
		table.setColumnSelectionAllowed(false);
		scroll = new JScrollPane(table);
		this.setLayout(new BorderLayout());
		this.add(scroll, BorderLayout.CENTER);
	}
	
	public void stateChanged(ChangeEvent e) {
		data = jman.getTable();
		System.out.println(data);
	}
}
does anyone know what to do?
does anyone understand my question?
Rotkiv is offline   Reply With Quote
Old 06-10-2008, 08:11 PM   #2 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,175
Belisarius is on a distinguished road
I'm not really familiar with Swing - been a while since I did anything with it. Sun has a pretty good guide to JTables though - see here. In short, I think you want "fire" methods - fireTableCellUpdated, fireTableRowsUpdated, fireTableDataChanged, fireTableRowsInserted, fireTableRowsDeleted, and fireTableStructureChanged.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 06-11-2008, 09:43 AM   #3 (permalink)
Rotkiv
Code Monkey
 
Rotkiv's Avatar
 
Join Date: Apr 2004
Location: Silicon Valley, CA
Posts: 64
Rotkiv is on a distinguished road
Send a message via AIM to Rotkiv Send a message via MSN to Rotkiv
thanks, I'd been using that page regularly to learn how to use jtables but i don't know why i didn't see this part: How to Use Tables (The Java™ Tutorials > Creating a GUI with JFC/Swing > Using Swing Components)
i looked all through that page yesterday but i don't know what the hell i was looking for that i didn't see the big orange letters that say listening and changes.
Rotkiv is offline   Reply With Quote
Old 06-11-2008, 02:29 PM   #4 (permalink)
Rotkiv
Code Monkey
 
Rotkiv's Avatar
 
Join Date: Apr 2004
Location: Silicon Valley, CA
Posts: 64
Rotkiv is on a distinguished road
Send a message via AIM to Rotkiv Send a message via MSN to Rotkiv
well i've gotten it to add rows. It's pretty straightforward if you understand the MVC concept. I was trying to use JTable as just another component along with my own model. whatever, like i know what i'm talking about . anyways, here's the working code. it adds a whole Vector to the table.

Code:
public class TableView extends JPanel implements ChangeListener {
	DefaultTableModel model = new DefaultTableModel();
	JTable table;
	JScrollPane scroll;
	JARmanager jman;
	
	public TableView(JARmanager man) {
		model.addColumn("Name");
		model.addColumn("Type");
		jman = man;
		
		table  = new JTable(model);
		table.setFillsViewportHeight(true);
		table.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
		table.setColumnSelectionAllowed(false);
		scroll = new JScrollPane(table);
		this.setLayout(new BorderLayout());
		this.add(scroll, BorderLayout.CENTER);
	}
	
	public void stateChanged(ChangeEvent e) {
		Vector<Object[]> data = jman.getTable();
		for(Object[] o: data)
			model.insertRow(model.getRowCount(), o);
	}
}
Rotkiv is offline   Reply With Quote
Old 06-11-2008, 05:29 PM   #5 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,175
Belisarius is on a distinguished road
Cool, thanks for sharing your solution. Glad everything worked out for you.
__________________
GitS
Belisarius is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Default Constructors fp_unit Standard C, C++ 3 07-03-2005 10:37 AM


All times are GMT -8. The time now is 12:05 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting