Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

  • #2
    New Coder
    Join Date
    Aug 2009
    Posts
    94
    Thanks
    0
    Thanked 16 Times in 16 Posts
    Could you post Matches.java and Match.java. This would help to debug the issue


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •