Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-10-2009, 12:36 AM   PM User | #1
Steph
New to the CF scene

 
Join Date: Oct 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Steph is an unknown quantity at this point
Question issues with tables and swing

Hi. I'm having trouble getting the GUI to work. I have a class MatchesTable.java which passes all the JUnit tests we were given and which has the following code:

Code:
package au.edu.uq.csse2002.smartnotes;

import javax.swing.table.*;

public class MatchesTable extends AbstractTableModel {

	private Matches matches;

	public MatchesTable(Matches matches) {
		this.matches = matches;
	}

	public void setMatches(Matches matches) {
		this.matches = matches;
	}

	public Matches getMatches() {
		return matches;
	}

	public int getColumnCount() {
		return 2;
	}

	public int getRowCount() {
		return matches.size();
	}

	public Object getValueAt(int row, int col) {
		if (col == 0) {
			return (int)((matches.match(row).similarity())*100);
		}
		else {
			return matches.match(row).record().text();
		}
	}

	public String getColumnName(int col) {
		if (col == 0) {
			return "Relevance";
		}
		else {
			return "Record";
		}
	}
}
and then SmartNotes.java which contains the GUI functionality:

Code:
package au.edu.uq.csse2002.smartnotes;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;

import javax.swing.*;
import javax.swing.border.Border;

public class SmartNotes extends JFrame{

	AbstractRecordReader recordReader;
	static JPanel jPanel = new JPanel(new FlowLayout());

	Matches matches;

	JTextField searchInput = new JTextField("");
	JButton searchButton = new JButton("Search");
	JTable matchesTable;
	JTextArea resultsDisplay = new JTextArea();
	JScrollPane pane = new JScrollPane(); 

	public SmartNotes(final int max) {
		matches = new Matches(3);//change back to max later
		matches.add(new Match(new Record("testing..."), 0.1));
		matches.add(new Match(new Record("swing"), 0.8));
		matches.add(new Match(new Record("is"), 0.7));
		matches.add(new Match(new Record("annoying"), 0.66));
		searchInput.setColumns(25);
		jPanel.add(searchInput);
		jPanel.add(searchButton);
		pane.add(new JSplitPane());
		matchesTable = new JTable(new MatchesTable(matches));
		matchesTable.setSize(400, 400);
		matchesTable.setFillsViewportHeight(true);
		matchesTable.getColumnName(0);
		matchesTable.getColumnName(1);

		resultsDisplay.setColumns(30);
		resultsDisplay.setText("test");
		jPanel.add(matchesTable);
		jPanel.add(resultsDisplay);
		getRootPane().setDefaultButton(searchButton);
		searchButton.addActionListener(new ActionListener(){ 
			public void actionPerformed(ActionEvent e){ 
				recordReader.find(searchInput.getText(), max);
				matchesTable = new JTable(new MatchesTable(matches));
			}
		});
		jPanel.setVisible(true);
	}
	public static void main(String args[]) {
		SmartNotes smartNotes = new SmartNotes(10);
		JFrame frame = new JFrame();
		frame.getContentPane().add(jPanel);
		frame.setSize(400, 500);
		frame.setVisible(true);
	}
}
I realise that SmartNotes.java isn't quite correct, but I had to add some data to the table before it would display anything.

And it looks like this when I run SmartNotes.java:



My main issue is that the table doesn't have column headers, so when the table is empty nothing shows.

Thanks so much for any help you can give me :)
Steph is offline   Reply With Quote
Old 10-16-2009, 10:35 PM   PM User | #2
jolly_nikki
New Coder

 
Join Date: Aug 2009
Posts: 94
Thanks: 0
Thanked 16 Times in 16 Posts
jolly_nikki is on a distinguished road
Could you post Matches.java and Match.java. This would help to debug the issue
jolly_nikki is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 01:57 AM.


Advertisement
Log in to turn off these ads.