Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1806
  • Last Modified:

Problem with JTable?

Hi,

I want to do the filter in my JTable, so i tried to google something and finally, i found the code below can do this:

//package testClass;

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.util.regex.*;

public class FilterTable {
      public static void main(String args[]) {
            Runnable runner = new Runnable() {
                  public void run() {
                        JFrame frame = new JFrame("Sorting JTable");
                        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        Object rows[][] = { { "AMZN", "Amazon", 41.28 },
                                    { "EBAY", "eBay", 41.57 },
                                    { "GOOG", "Google", 388.33 },
                                    { "MSFT", "Microsoft", 26.56 },
                                    { "NOK", "Nokia Corp", 17.13 },
                                    { "ORCL", "Oracle Corp.", 12.52 },
                                    { "SUNW", "Sun Microsystems", 3.86 },
                                    { "TWX", "Time Warner", 17.66 },
                                    { "VOD", "Vodafone Group", 26.02 },
                                    { "YHOO", "Yahoo!", 37.69 } };
                        Object columns[] = { "Symbol", "Name", "Price" };
                        TableModel model = new DefaultTableModel(rows, columns) {
                              public Class getColumnClass(int column) {
                                    Class returnValue;
                                    if ((column >= 0) && (column < getColumnCount())) {
                                          returnValue = getValueAt(0, column).getClass();
                                    } else {
                                          returnValue = Object.class;
                                    }
                                    return returnValue;
                              }
                        };
                        JTable table = new JTable(model);
                            //Notice this line below:            
                                final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(
                                    model);
                        table.setRowSorter(sorter);
                        JScrollPane pane = new JScrollPane(table);
                        frame.add(pane, BorderLayout.CENTER);
                        JPanel panel = new JPanel(new BorderLayout());
                        JLabel label = new JLabel("Filter");
                        panel.add(label, BorderLayout.WEST);
                        final JTextField filterText = new JTextField("SUN");
                        panel.add(filterText, BorderLayout.CENTER);
                        frame.add(panel, BorderLayout.NORTH);
                        JButton button = new JButton("Filter");
                        button.addActionListener(new ActionListener() {
                              public void actionPerformed(ActionEvent e) {
                                    String text = filterText.getText();
                                    if (text.length() == 0) {
                                          sorter.setRowFilter(null);
                                    } else {
                                          try {
                                                sorter
                                                            .setRowFilter(RowFilter
                                                                        .regexFilter(text));
                                          } catch (PatternSyntaxException pse) {
                                                System.err.println("Bad regex pattern");
                                          }
                                    }
                              }
                        });
                        frame.add(button, BorderLayout.SOUTH);
                        frame.setSize(300, 250);
                        frame.setVisible(true);
                  }
            };
            EventQueue.invokeLater(runner);
      }
}


Well, i think the first thing i need to do is create a TableRowSorter , then i added this line to my code:

TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);

but, i got this error "The type RowSorter is not generic; it cannot be parameterized with arguments <TableModel>" instantly :(

Here is my code:

//package view;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.table.TableRowSorter;
import javax.swing.JTable;
import javax.swing.table.TableModel;
import java.awt.*;
import java.awt.event.*;
import java.util.regex.*;


import controller.*;

public class  StudentList extends JPanel implements ActionListener{

      private static final long serialVersionUID = 1L;
      JButton btUpdate = new JButton("Update");
      JButton btDelete = new JButton("Delete");
      

      ExportReport Export ;
      
      Database dbStu;
      String colNames[] = {"Student ID","Name","Gender","Date of birth","Class ID","Address","Phone","Email","Delete"};
      MyTableModel tableModel;
      
      
      
      JTable table;
      JScrollPane scrollPane;
      
      JPanel header = new JPanel();
      JPanel body = new JPanel();
      JPanel footer = new JPanel();
      
      
      
      
      //Search Component
      JPanel search = new JPanel();
      JPanel left = new JPanel();
      JPanel right = new JPanel();
      JPanel tmp;
      
      
      JRadioButton radClass = new JRadioButton();
      JRadioButton radAll = new JRadioButton("All");
      JComboBox comb = new JComboBox();
      
      JButton btFilter = new JButton("Filter");
      ButtonGroup bg = new ButtonGroup();
      
      JTextField tfSearch = new JTextField("Enter student name");
      JButton btSearch = new JButton("Search");
      
      
      
      StudentList()
      {
            setLayout(new BorderLayout());
            body.setLayout(new GridLayout(1,1));
            dbStu = new Database();
            
            table = new JTable(this.getTableModel());
            table.setAutoCreateRowSorter(true);
            table.getTableHeader().setToolTipText("Click to specify sorting; Control-Click to specify secondary sorting");
            RowSorter<TableModel> sorter = new RowSorter<TableModel>((this.getTableModel());
            table.setRowSorter(sorter);
            
            
            
            scrollPane = new JScrollPane(table);
            table.setPreferredScrollableViewportSize(new Dimension(500, 70));
            table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            table.setCellSelectionEnabled(true);
            body.add(scrollPane);
            
            //Search function
            left.setBorder(new TitledBorder("Filter"));
            left.setLayout(new BorderLayout());
            
            btFilter.addActionListener(this);
            btSearch.addActionListener(this);
            
            
            bg.add(radAll);
            bg.add(radClass);
            tmp = new JPanel();
            tmp.setLayout(new FlowLayout());
            tmp.add(radClass);
            tmp.add(fil.getFilterClass(comb));
            tmp.add(radAll);
            left.add(tmp,BorderLayout.CENTER);
            
            tmp = new JPanel();
            tmp.add(btFilter);
            left.add(tmp,BorderLayout.SOUTH);
            
            right.setBorder(new TitledBorder("Search"));
            right.setLayout(new GridLayout(2,1));
            right.add(tfSearch);
            tmp = new JPanel();
            tmp.add(btSearch);
            right.add(tmp);
            
            search.setBorder(new TitledBorder(""));
            search.setLayout(new GridLayout(1,2));
            search.add(left);
            
            search.add(right);
            //End search
            
            Export = new ExportReport(table);
            
            footer.setBorder(new TitledBorder("Control"));
            btUpdate.addActionListener(this);
            btDelete.addActionListener(this);
            footer.add(btUpdate);
            footer.add(btDelete);
            footer.add(Export.getExport());
            add(body,BorderLayout.CENTER);
            add(footer,BorderLayout.SOUTH);
            add(search,BorderLayout.NORTH);
            
            StudentManager.stuFlag = 0;
            StudentManager.stuCond = null;
            
      }
      
      public void actionPerformed(ActionEvent e)
      {
            if(e.getSource() == btUpdate)
            {
            
                  
            }
            if(e.getSource() == btDelete)
            {


            }
            if(e.getSource() == btSearch)
            {
            
                  
            }
            if(e.getSource() == btFilter)
            {
                  
            }
            
      }
      
      public MyTableModel getTableModel()
      {
            //return new  MyTableModel();
      }
      
      public JPanel getStuLst()
      {
            return new StudentList();
      }



}

i very wonder why with the same code, in the same JRE, but one give error, and one now ?
Please help me!

0
cassiej81
Asked:
cassiej81
  • 7
  • 5
1 Solution
 
cassiej81Author Commented:
Edited the last line:
i very wonder why with the same code, in the same JRE, but one give error, and one not?
Please help me!
0
 
objectsCommented:
TableRowSorter<TableModel> sorter = new TableRowSorter<MyTableModel>(table.getModel());
0
 
cassiej81Author Commented:
Thank objects, but i still got this error when using your code :(

"The type TableRowSorter is not generic; it cannot be parameterized with arguments <TableModel>"

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
objectsCommented:
sorry, missed one

TableRowSorter<MyTableModel> sorter = new TableRowSorter<MyTableModel>(table.getModel());
0
 
cassiej81Author Commented:
i already tried it, but got same error :(
0
 
objectsCommented:
ho can u get same error if you're not using a TableModel anywhgere?

0
 
cassiej81Author Commented:
Can u explain with more detail?
0
 
objectsCommented:
...with arguments <TableModel>"

theres no mention of a TableModel

though try:

TableRowSorter<MyTableModel> sorter = new TableRowSorter<MyTableModel>((MyTableModel)(table.getModel()));
0
 
cassiej81Author Commented:
oops, it's could not solve, got this error again
"The type TableRowSorter is not generic; it cannot be parameterized with arguments <TableModel>"

Why in the first code, with same declare it work fine?

final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>( model);
 table.setRowSorter(sorter);

???

Hope you can help!
0
 
objectsCommented:
i'm confused, whats working and whats not?
0
 
cassiej81Author Commented:
Ah-ha, i realize that  error only occur when i put that code: TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>( model);
 table.setRowSorter(sorter);
on my current project!

I try to create another new project  ( i using Eclipse), then add it on a test class, it's work fine, so i think the problem is something on my current project. I try to figure out now, hope can get the advice from you
0
 
cassiej81Author Commented:
Haha, i found the problem, it's because i using JRE 1.6 but the compiler still is 1.4.2

Thanks objects for your time on my stupid problem.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now