1

I want to have the following function: by clicking the header of a column, I can select the column and hide the other columns. At the same time, I want to show semantic zooming information for the selected column.

Now I am just registering a mouse listener to selected header but getting the following exception:

[relevant exception info]

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 50, Size: 50
at java.util.ArrayList.rangeCheck(ArrayList.java:604)
at java.util.ArrayList.get(ArrayList.java:382)
at TableModelSwitch.getValueAt(Unknown Source)
atjavax.swing.table.TableRowSorter$TableRowSorterModelWrapper.getValueAt(TableRowSorter.java:269)
at javax.swing.table.TableRowSorter$TableRowSorterModelWrapper.getStringValueAt(TableRowSorter.java:285)
at javax.swing.DefaultRowSorter.compare(DefaultRowSorter.java:952)
at javax.swing.DefaultRowSorter.access$100(DefaultRowSorter.java:112)
at javax.swing.DefaultRowSorter$Row.compareTo(DefaultRowSorter.java:1376)
at javax.swing.DefaultRowSorter$Row.compareTo(DefaultRowSorter.java:1366)

I am wondering why the clicking event in JTable will related to getValueAt() method in TableModel class. For all the examples of teaching adding mouse listener for header clicking, no one mentions this part as well. Could anyone help me figure out the problem? The register part code is as follows:

  public MyJTable() {
    super();
    this.addMouseListener(new MouseAdapter() {
       public void mouseClicked(MouseEvent e) {
           JTableHeader header = getTableHeader();
           if (header.equals(e.getSource())){
                if (e.getClickCount()==2){
                   int index = convertColumnIndexToModel(columnAtPoint(e.getPoint()));
                   if (index >= 0) {
                      System.out.println("Clicked on column " + index);

                    }
                } 
            } else { //clicking event when clicking any cell of this Table
      }
    });

I had thought maybe because the header==null, there is no chance to get into the first if condition. However, after checking, header!=null and I had override getColumnName in myAbstractTableModel.So now I am running out of ideas...

[New edit]

Now I try to add listener for header, but the error is the same. I found that when I double click the header, the exception is the same as above, and also shows the selected header's information as I coded. I guess the exception is because the table's header has sorting listener. I tried to setAutoCreateRowSorter(false); and also summaryHeader.setReorderingAllowed(false); but it still doesn't work. The relevant new code is as follows:

  public JTableSummary() {
    super();
    setAutoCreateRowSorter(false);
    JTableHeader summaryHeader = this.getTableHeader();
    summaryHeader.setReorderingAllowed(false);
    summaryHeader.addMouseListener(new MouseAdapter(){
           @Override
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount()==2){
                   int index = convertColumnIndexToModel(columnAtPoint(e.getPoint()));
                   if (index >= 0) {
                      System.out.println("Clicked on column " + index);
                    }
                //} 
            }
    });

I suppose the current problem is how to effectively disable header sorting listener(I can see sorting icon - a triangle appeared upward/downward when I click the header) and to make my own listener workable?

Thanks in advance for your help!

AnneS
  • 1,493
  • 3
  • 14
  • 19
  • 3
    Netbeans isnt the problem, i think you've not compiled the program with debug information enabled. Go into the project properties and change the compiler properties. You've also attached your mouse listener to the table itself and not the table header. This means that the table will want to re-render the table cell so it appears highlighted – MadProgrammer Sep 07 '13 at 20:57
  • 2
    [How can I put a control in the JTableHeader of a JTable?](http://stackoverflow.com/questions/7137786/how-can-i-put-a-control-in-the-jtableheader-of-a-jtable) shows how to add a `MouseListener` to the header; the accepted answer offers valuable caveats. – trashgod Sep 07 '13 at 22:45
  • @MadProgrammer Thanks for your suggestion! Now I found new cause for the problem. Could you please take a look at the new edit? – AnneS Sep 08 '13 at 17:39

0 Answers0