PDA

View Full Version : issues with tables and swing


Steph
10-10-2009, 12:36 AM
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:


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:


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:

http://www.mediafire.com/imgbnc.php/45569560dc28810a0e316fd5c2df21d96g.jpg

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 :)

jolly_nikki
10-16-2009, 10:35 PM
Could you post Matches.java and Match.java. This would help to debug the issue