JTable DND requires two clicks (fixed but broken in 1.6)

I'm using a JTable as a source of a drag and drop operation.
I can easily get it to work, however, when I want to start a drag from an unselected row I have to first click on the row to select it and then mouse-down on the row and then I can drag it.
I found a workaround and it was working flawlessly. I was caching the mouse-down and technically firing it twice. Although it seemed like a really cheap trick it worked, but now in 1.6 it doesn't.
Any ideas?

Alright, here's the code. I've tried a bunch of things, but if you can get a single-click drag working on this example, then it'll work in my stuff as well.
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.dnd.*;
import javax.swing.*;
import javax.swing.table.*;
public class JTableDNDExample extends JTable
  // the data for the table...
  private String[][] data =
      { "Cell 1-1", "Cell 2-1", "Cell 3-1" },
      { "Cell 1-2", "Cell 2-2", "Cell 3-2" },
      { "Cell 1-3", "Cell 2-3", "Cell 3-3" },
      { "Cell 1-4", "Cell 2-4", "Cell 3-4" },
      { "Cell 1-5", "Cell 2-5", "Cell 3-5" },
  // the columns for the table...
  private String[] columns = { "Column 1", "Column 2", "Column 3" };
  public JTableDNDExample()
    // enable the drag...
    this.setDragEnabled(true);
    // create the table model with the default data...
    DefaultTableModel model = new DefaultTableModel(data, columns);
    this.setModel(model);
    // get the selection model...
    ListSelectionModel selectionModel = this.getSelectionModel();
    selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    // set the selection possibilities...
    this.setCellSelectionEnabled(false);
    this.setRowSelectionAllowed(true);
    this.setColumnSelectionAllowed(false);
    // create the trasnfer handler for the table...
    TransferHandler th = new TransferHandler()
      public int getSourceActions(JComponent comp)
        // return the copy or move action...
        return DnDConstants.ACTION_COPY_OR_MOVE;
      protected Transferable createTransferable(JComponent comp)
           int index = getSelectedRow();
           if(index >= 0 && index < data.length)
             // get the value at the index...
             String value = data[index][0];
             // get the string transferable for the string...
             StringSelection transferable = new StringSelection(value);
             // return the rtansferable...
          return transferable;
        // no element to transfer, return null...
        return null;
      public boolean canImport(JComponent comp, DataFlavor[] flavors)
        // cannot import, return false...
        return false;
    // se tthe transfer handler...
    this.setTransferHandler(th);
  public static void main(String[] args)
    JFrame frame = new JFrame("JTable DND Example");
    frame.getContentPane().setLayout(new GridBagLayout());
    JTableDNDExample table = new JTableDNDExample();
    JScrollPane scrollPane = new JScrollPane(table);
    frame.add(scrollPane, new GridBagConstraints(1, 1, 1, 1, 1.0, 1.0,
      GridBagConstraints.CENTER, GridBagConstraints.BOTH,
      new Insets(6, 6, 6, 6), 0, 0));
    frame.setSize(500, 500);
    frame.setVisible(true);
}

Similar Messages

  • Dropdown widget requires two clicks?

    I'm just becoming familiar with the dropdown widget, and have noticed that it requires two clicks in order to display the list. The first click doesn't do anything, and the second displays the choices. Is there some way around this? I looked in the properties but didn't see anything related to this issue. Any help is appreciated. Thanks!

    I reported this behaviour, don't have a workaround. Can only recommend that you report it as well.

  • JOptionPane fired by FocusListener requires two clicks to close

    I have a similar problem as the person posting this ( http://forums.java.sun.com/thread.jsp?forum=54&thread=109797 ): my JOptionPane fired by a focus listener needs to have its OK button clicked twice before the dialog closes. Any ideas?

    check out this thread
    http://forum.java.sun.com/thread.jsp?forum=54&thread=109797

  • VERY VERY angry at Apple - Address Book problems, easy to fix, but broken.

    Hello
    I have lost the addresses in my address book. I had my entire user folder backed up onto a separate hard drive. In that folder, there is a Library > Application Support > AddressBook directory.
    In that directory is a "AddressBook.data" file and an "AddressBook.data.beforesave" file. I am unable to get these into my Address book to replace my lost addresses.
    If I drag the "AddressBook" folder into my new application support directory and replace the existing one, then when I open address book, it is empty.
    Apple, how hard is it to just base the address book file on a text document that actually works in this case? A simple tab delimited document, or another simple type of document would work fine. Or, you could just write the functionality into Address Book to read these files directly. The info is there, I can open them in BBedit and see the data!!!
    I just cannot believe that this is a problem. I am so disappointed in Apple for not making this simpler. It is very un-like Apple to do this, and even more un-like Apple to not fix it.

    I just had Mail crash while Address Book was open, that subsequently caused a couple of other Mac apps to crash as well: Safari, ICal. After running Disk Utility to repair permissions, I restarted and went back to Mail, and my entire address book is GONE. ***??????????? I'm so irritated right now, I've been a Mac user for 15 years and have never had something like this happen.

  • Why does the Back button require two mouse clicks?

    I've just upgraded to ver. 9 and now the Back button requires two mous clicks/

    I might not have used the word "Normal".  "Typical" maybe.  I suppose "Normal" is acceptable in the context of "SNAFU".
    Wait until you make an edit on one of your own posts to fix a stupid typo, then later look to find the typo back and you can no longer edit it.  And what's up with that messed-up Javascript that shows up at the top of some quoted messages?
    These forums are barely usable, but a bright spot is the color-managed photo attachment capability (i.e., you can upload an image with a non-sRGB color profile and the server will convert it).  I suppose if anything Adobe could have been expected to get that part right.
    -Noel

  • Two clicks required to get results from Search Input box on toolbar.

    I'm running version 8.02.208.
    I'm new at Webhelp and was hoping somebody might be able to give me some pointers.
    If I open my published webhelp on the Internet and try to search using the Search Input box on the toolbar the first time, two clicks are required in order to get search results.
    This problem only appears the first time, all other searches from the Search Input box on the toolbar work properly with only one click required.
    In addition, the Search tool opened from the Search button always works fine first and every time, but I did see the extra space that I've read about.
    This is what I don't understand...the problem I mentioned with the 2 clicks required on the toolbar, never happens when I Generate and View the results. Meaning that I am looking at the output but have not published it yet. It works fine first and every time.
    Why would the problem show up one way and not the other? Does anyone know what could be different?
    I just keep hoping that there is something that I can do to get rid of the problem. I keep getting Problem Reports opened and assigned to me and complaints from customer service from customers. They think the search isn't working and I keep having to explain.....arghhhh!
    Thanks!
    Deb

    Hi Sheryl and Peter,
    I have also tried the options as you have suggested without success. Everything works fine when the WebHelp is published to a local or network drive but when it is published to the server the issue occurs.
    I would point out too, that I find that the problem with the space only occurs on the intial attempt to search using the Search Box. Any subsequent attempts without closing the browser appear to work fine.
    Chris

  • I replaced the hard drive to a 30gig on my Ipod 4th gen. click wheel but it is still not recognized on my computer or in itunes......I get an error message with a folder and "!"....what can I do to fix??

    I replaced the hard drive to a 30gig on my Ipod 4th gen. click wheel but it is still not recognized on my computer or in itunes......I get an error message with a folder and "!"....what can I do to fix??

    Have you recharge the battery for at least 2 hours, or just leave it connected for 4 hours?
    (When you replaced the Hardisk, you should also replace the battery.)
    Some other iPod SSD sites, says that you may have to format the CF in FAT32, as Mac dont read NTFS drive so if Windows is the 1st PC that your CF is connected, it may be formatted to NTFS.
    You can try erasing the CF using Disk Utilty, so that Mac can recognise it. (but not Windows)
    Have a nice day!

  • I want to transfer an audiobook and a movie from my MacBook Pro to my iPhone 4S, and can't figure out how.  I went to "Help" and it said after connecting the two devices I should "Click Apps," but I can't find anything labeled "Apps" to click.  Help?!!

    I want to transfer an audiobook and a movie from my MacBook Pro to my iPhone 4S, and can't figure out how.  I went to "Help" and it said after connecting the two devices I should "Click Apps," but I can't find anything labeled "Apps" to click.  Help?!!

    If you have iTunes 11 on your Mac then you can enable the left-hand sidebar via option-command-S, that might make it easier to navigate.
    Connect the phone to your Mac and select it on that left-hand sidebar, and on the right-hand side of the screen there should be a series of tabs which you can use to select what to sync to the phone. If it's an audiobook then it should appear on the Books tab for selecting/syncing, and films will be on the Movies tab. When you've got the audiobook on your phone you should get an Audiobooks option in the Music app (you might need to tap the '...' tab at the bottom of the Music app to see it).

  • HT5704 Hi, Two recent updates for my iPhone have caused crashes (down the white lead via iTunes).I have "fixed" but not sure of the cause?

    Hi,
    Two recent updates for my iPhone have caused crashes (down the white lead) via iTunes.
    I have "fixed" but I don't know the cause?
    Each time the update does n't complete.
    The download does n't happen.
    The computer & internet connection are good.
    It is as if the Apple server does n't complete?
    The phone stays in recovery mode, until you reset and rebuild in iTunes.
    All software is up to date, but I can't find the cause
    ANY IDEAS?
    Regards,
    P.
    <Email Edited by Host>

    No error messages.
    It just fails to complete the download, so recovery mode is the next step.
    This can be over a period of eight hours or more!
    I have so many (original) Apple USB leads that I would n't know if I have been using the same one (if it was faulty).
    Does this sound like a "common" problem?

  • Two clicks required for Safari window which is not currently the focus

    This is really starting to annoy me - I have a 17" powerbook so I can display two webpages side by side. I click on a link in one page and... ...nothing happens. If this page was not the current focus, my first click just changes the focus - i then have to click again to go to the link. Many times i sit there waiting for the display to update, only to find that it is not going to because it requires another click... Is there any way to make Safari take action on the first click on a window not currently in focus?

    I don't know of any way to allow this.
    Mail works the same way.
    using mail & safari windows side by side does the same, as does anything(?) that involves switching applications.
    It's just the links in safari that require one click to focus then one to activate - running two safari windows & clicking something in the toolbar or bookmarks bar on the 'other' window works just fine. As does clicking a 'button' within the page (like 'post message' on these boards,for example)
    I can only suggest letting Apple know what you think at http://www.apple.com/macosx/feedback/

  • Can apple fix my broken ipodtouch 4g if you can hear the sound but the screen doesnt show

    can apple fix my broken ipodtouch 4g if you can hear the sound but the screen doesnt show

    They will not fix it. What they do is replace rather then fix.
    After Apple has examined it, they will determine if it is covered by warranty or not. If it is not, you will have to pay for the replacement.
    Allan

  • After downloading the iOS 7, it asked me if I agree to the terms and conditions and I keep clicking agree but it stays on that page and I can't even use my phone now. How do I fix this problem?

    After downloading the iOS 7, it asked me if I agree to the terms and conditions and I keep clicking agree but it stays on that page and I can't even use my phone now. How do I fix this problem?

    I have the same problem. frozen on the Terms and Conditions page. Any help, anybody?

  • Fix multiple broken aliases after sync

    I've synchronised my iMac with my new MacBook. I have quite a lot of aliases set up on the iMac, however, when they're transferred across, they retain their source path (i.e. when on the MacBook, clicking on the alias doesn't work because it's looking for a file on the iMac).
    It's easy enough to fix the broken links individually, but I wondered if there was a way for this to be done en masse, automatically?
    Can't be that hard - all the details of where to find the files are there, just with "MacBook" instead of "iMac".
    Any advice gratefully received
    Giles
    ps I've never used scripts, so if that's the answer, please be gentle with me!

    Ok, to answer my own question...
    If you get this problem after synchronising two machines, simply repair the disk permissions on the target machine and the aliases should start working again. Worked for me anyway!
    :¬)
    G

  • JTable JCombox popup on click in table cell

    What is the recommended way of making a JComboBox that is in a JTable cell popup when the user clicks once anywhere in the cell?
    The popup is actually a calendar that works fine if the user clicks on the spot where the button shows up in the cell but sets the focus in the text box of the JComboBox is the click is anywhere else in the cell and the popup doesn't sho in that case. To get to the popup requires a click on the button.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Test extends JFrame {
      public Test() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container content = getContentPane();
        String[] head = {"One","Two","Three"};
        String[][] data = {{"R1-C1","R1-C2","R1-C3"},
                           {"R2-C1","R2-C2","R2-C3"},
                           {"R3-C1","R3-C2","R3-C3"}};
        JTable jt = new JTable(data, head);
        content.add(new JScrollPane(jt), BorderLayout.CENTER);
        JComboBox jcb = new JComboBox(head);
        jcb.addFocusListener(new FocusAdapter() {
          public void focusGained(FocusEvent fe) {
            ((JComboBox)fe.getSource()).showPopup();
        jt.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(jcb));
        setSize(200, 200);
        setVisible(true);
      public static void main(String[] args) { new Test(); }
    }

  • JTable header : text = two-rows, onClick action = sorting

    Hi guys.
    I want to create a JTable where the user can have the data sorted by clicking upon a column's header. The code below shows the table I describe (you can click upon a column and sorting is performed).
    import javax.swing.JFrame;
    import java.awt.HeadlessException;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.util.Vector;
    import javax.swing.table.DefaultTableModel;
    //--------- TableSorter ----------
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import java.util.List;
    import javax.swing.*;
    import javax.swing.event.TableModelEvent;
    import javax.swing.event.TableModelListener;
    import javax.swing.table.*;
    //---------- Table Height - Column Width
    import javax.swing.table.TableColumn;
    import java.awt.FontMetrics;
    import javax.swing.JTable;
    import java.awt.Dimension;
    * <p>Title: </p>
    * <p>Description: </p>
    * <p>Copyright: Copyright (c) 2006</p>
    * <p>Company: </p>
    * @author not attributable
    * @version 1.0
    public class Test extends JFrame {
      JPanel jPanel1 = new JPanel();
      JPanel jPanel2 = new JPanel();
      JButton jButton1 = new JButton();
      FlowLayout flowLayout1 = new FlowLayout();
      BorderLayout borderLayout1 = new BorderLayout();
      BorderLayout borderLayout2 = new BorderLayout();
      //------------- MY VARIABLES -------------
      String[] columnNames_objectArr = new String[] {
          "Col1",
          "Column 2",
          "Column 3",
          "C4",
          "Col5",
          "Col 6",
      Vector columnNames_vector = new Vector();
      private Vector data_vector = new Vector();
      private Object[][] data_objectArr;
      public TableHeight_ColumnWidth tcw;
      private int totalTableWidth = 0, totalTableHeight = 0;
      private JComboBox jcmbxData = new JComboBox(
          new String[] {"Not Configured", "Switch", "Modem"});
      private int maxVisibleRows = 51;
      TableSorter model;
      private JTable table;
      JScrollPane jScrollPane1;
      public Test(String[] args) throws HeadlessException {
        try {
          jbInit();
          setupGUI();
          launchGUI();
        catch(Exception e) {
          e.printStackTrace();
      private void jbtnClose_actionPerformed(ActionEvent e) {
        dispose();
      private void launchGUI() {
        pack();
        setVisible(true);
        setResizable(false);
      private void setupGUI() {
        columnNames_vector.addElement("Col1");
        columnNames_vector.addElement("Column 2");
        columnNames_vector.addElement("Column 3");
        columnNames_vector.addElement("C4");
        columnNames_vector.addElement("Col5");
        columnNames_vector.addElement("Col 6");
        Vector row_data1 = new Vector();
        Vector row_data2 = new Vector();
        Vector row_data3 = new Vector();
        Vector row_data4 = new Vector();
        Vector row_data5 = new Vector();
        Vector row_data6 = new Vector();
        row_data1.add("Mary");
        row_data1.add("Campioneato");
        row_data1.add("Snowboarding");
        row_data1.add(new Integer(578987899));
        row_data1.add(new Boolean(false));
        row_data1.add("Not Configured");
        row_data2.add("Alison");
        row_data2.add("Huml");
        row_data2.add("Rowing");
        row_data2.add(new Integer(3));
        row_data2.add(new Boolean(true));
        row_data2.add("Switch");
        row_data3.add("Ka");
        row_data3.add("Walrath");
        row_data3.add("Knitting");
        row_data3.add(new Integer(2));
        row_data3.add(new Boolean(false));
        row_data3.add("Modem");
        row_data4.add("Sharon");
        row_data4.add("Zakhouras");
        row_data4.add("Speed reading");
        row_data4.add(new Integer(20));
        row_data4.add(new Boolean(true));
        row_data4.add("Switch");
        row_data5.add("Philip");
        row_data5.add("Milner");
        row_data5.add("Pool");
        row_data5.add(new Integer(10));
        row_data5.add(new Boolean(false));
        row_data5.add("Not Configured");
        data_vector.add(row_data1);
        data_vector.add(row_data2);
        data_vector.add(row_data3);
        data_vector.add(row_data4);
        data_vector.add(row_data5);
        model = new TableSorter(new SortTableModel(data_vector, columnNames_vector));
        table = new JTable(model);
        tcw = new TableHeight_ColumnWidth(model, table);
        jScrollPane1 = new JScrollPane(table);
        model.setTableHeader(table.getTableHeader());
        jPanel1.add(jScrollPane1, BorderLayout.CENTER);
        //Add a JComboBox as a cellEditor...
        DefaultCellEditor dce = new DefaultCellEditor(jcmbxData);
        table.getColumnModel().getColumn(5).setCellEditor(dce);
        //..... add the IPJPanel as cellEditor.....
        tcw.fixTableLook(maxVisibleRows);
      private void jbInit() throws Exception {
        this.getContentPane().setLayout(borderLayout2);
        jPanel1.setLayout(borderLayout1);
        jPanel2.setLayout(flowLayout1);
        jButton1.setText("Close");
        jPanel1.setBorder(BorderFactory.createRaisedBevelBorder());
        this.getContentPane().add(jPanel1, BorderLayout.CENTER);
        this.getContentPane().add(jPanel2, BorderLayout.SOUTH);
        jPanel2.add(jButton1, null);
        jButton1.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            jbtnClose_actionPerformed(e);
      public static void main( String[] args ) {
        new Test(args);
      public boolean getScrollableTracksViewportHeight() {
        Component parent = getParent();
        if (parent instanceof JViewport) {
          return parent.getHeight() > getPreferredSize().height;
        return false;
      //------------------ MY TABLE MODEL ------------------
      public class SortTableModel extends DefaultTableModel {
        private boolean DEBUG = false;
        public SortTableModel(Object[][] data, String[] columnNames) {
          super(data, columnNames);
        public SortTableModel(Vector data, Vector columnNames) {
          super(data, columnNames);
      //------------------ TABLE SORTER -----------------------
      public class TableSorter extends AbstractTableModel {
          protected TableModel tableModel;
          public static final int DESCENDING = -1;
          public static final int NOT_SORTED = 0;
          public static final int ASCENDING = 1;
          private Directive EMPTY_DIRECTIVE = new Directive(-1, NOT_SORTED);
          public final Comparator COMPARABLE_COMAPRATOR = new Comparator() {
              public int compare(Object o1, Object o2) {
                  return ((Comparable) o1).compareTo(o2);
          public final Comparator LEXICAL_COMPARATOR = new Comparator() {
              public int compare(Object o1, Object o2) {
                  return o1.toString().compareTo(o2.toString());
          private Row[] viewToModel;
          private int[] modelToView;
          private JTableHeader tableHeader;
          private MouseListener mouseListener;
          private TableModelListener tableModelListener;
          private Map columnComparators = new HashMap();
          private List sortingColumns = new ArrayList();
          public TableSorter() {
              this.mouseListener = new MouseHandler();
              this.tableModelListener = new TableModelHandler();
          public TableSorter(TableModel tableModel) {
              this();
              setTableModel(tableModel);
          public TableSorter(TableModel tableModel, JTableHeader tableHeader) {
              this();
              setTableHeader(tableHeader);
              setTableModel(tableModel);
          private void clearSortingState() {
              viewToModel = null;
              modelToView = null;
          public TableModel getTableModel() {
              return tableModel;
          public void setTableModel(TableModel tableModel) {
              if (this.tableModel != null) {
                  this.tableModel.removeTableModelListener(tableModelListener);
              this.tableModel = tableModel;
              if (this.tableModel != null) {
                  this.tableModel.addTableModelListener(tableModelListener);
              clearSortingState();
              fireTableStructureChanged();
          public JTableHeader getTableHeader() {
              return tableHeader;
          public void setTableHeader(JTableHeader tableHeader) {
              if (this.tableHeader != null) {
                  this.tableHeader.removeMouseListener(mouseListener);
                  TableCellRenderer defaultRenderer = this.tableHeader.getDefaultRenderer();
                  if (defaultRenderer instanceof SortableHeaderRenderer) {
                      this.tableHeader.setDefaultRenderer(((SortableHeaderRenderer) defaultRenderer).tableCellRenderer);
              this.tableHeader = tableHeader;
              if (this.tableHeader != null) {
                  this.tableHeader.addMouseListener(mouseListener);
                  this.tableHeader.setDefaultRenderer(
                          new SortableHeaderRenderer(this.tableHeader.getDefaultRenderer()));
          public boolean isSorting() {
              return sortingColumns.size() != 0;
          private Directive getDirective(int column) {
              for (int i = 0; i < sortingColumns.size(); i++) {
                  Directive directive = (Directive)sortingColumns.get(i);
                  if (directive.column == column) {
                      return directive;
              return EMPTY_DIRECTIVE;
          public int getSortingStatus(int column) {
              return getDirective(column).direction;
          private void sortingStatusChanged() {
              clearSortingState();
              fireTableDataChanged();
              if (tableHeader != null) {
                  tableHeader.repaint();
          public void setSortingStatus(int column, int status) {
              Directive directive = getDirective(column);
              if (directive != EMPTY_DIRECTIVE) {
                  sortingColumns.remove(directive);
              if (status != NOT_SORTED) {
                  sortingColumns.add(new Directive(column, status));
              sortingStatusChanged();
          protected Icon getHeaderRendererIcon(int column, int size) {
              Directive directive = getDirective(column);
              if (directive == EMPTY_DIRECTIVE) {
                  return null;
              return new Arrow(directive.direction == DESCENDING, size, sortingColumns.indexOf(directive));
          private void cancelSorting() {
              sortingColumns.clear();
              sortingStatusChanged();
          public void setColumnComparator(Class type, Comparator comparator) {
              if (comparator == null) {
                  columnComparators.remove(type);
              } else {
                  columnComparators.put(type, comparator);
          protected Comparator getComparator(int column) {
              Class columnType = tableModel.getColumnClass(column);
              Comparator comparator = (Comparator) columnComparators.get(columnType);
              if (comparator != null) {
                  return comparator;
              if (Comparable.class.isAssignableFrom(columnType)) {
                  return COMPARABLE_COMAPRATOR;
              return LEXICAL_COMPARATOR;
          private Row[] getViewToModel() {
              if (viewToModel == null) {
                  int tableModelRowCount = tableModel.getRowCount();
                  viewToModel = new Row[tableModelRowCount];
                  for (int row = 0; row < tableModelRowCount; row++) {
                      viewToModel[row] = new Row(row);
                  if (isSorting()) {
                      Arrays.sort(viewToModel);
              return viewToModel;
          public int modelIndex(int viewIndex) {
              return getViewToModel()[viewIndex].modelIndex;
          private int[] getModelToView() {
              if (modelToView == null) {
                  int n = getViewToModel().length;
                  modelToView = new int[n];
                  for (int i = 0; i < n; i++) {
                      modelToView[modelIndex(i)] = i;
              return modelToView;
          // TableModel interface methods
          public int getRowCount() {
              return (tableModel == null) ? 0 : tableModel.getRowCount();
          public int getColumnCount() {
              return (tableModel == null) ? 0 : tableModel.getColumnCount();
          public String getColumnName(int column) {
              return tableModel.getColumnName(column);
          public Class getColumnClass(int column) {
              return tableModel.getColumnClass(column);
          public boolean isCellEditable(int row, int column) {
              return tableModel.isCellEditable(modelIndex(row), column);
          public Object getValueAt(int row, int column) {
              return tableModel.getValueAt(modelIndex(row), column);
          public void setValueAt(Object aValue, int row, int column) {
              tableModel.setValueAt(aValue, modelIndex(row), column);
          // Helper classes
          private class Row implements Comparable {
              private int modelIndex;
              public Row(int index) {
                  this.modelIndex = index;
              public int compareTo(Object o) {
                  int row1 = modelIndex;
                  int row2 = ((Row) o).modelIndex;
                  for (Iterator it = sortingColumns.iterator(); it.hasNext();) {
                      Directive directive = (Directive) it.next();
                      int column = directive.column;
                      Object o1 = tableModel.getValueAt(row1, column);
                      Object o2 = tableModel.getValueAt(row2, column);
                      int comparison = 0;
                      // Define null less than everything, except null.
                      if (o1 == null && o2 == null) {
                          comparison = 0;
                      } else if (o1 == null) {
                          comparison = -1;
                      } else if (o2 == null) {
                          comparison = 1;
                      } else {
                          comparison = getComparator(column).compare(o1, o2);
                      if (comparison != 0) {
                          return directive.direction == DESCENDING ? -comparison : comparison;
                  return 0;
          private class TableModelHandler implements TableModelListener {
              public void tableChanged(TableModelEvent e) {
                  // If we're not sorting by anything, just pass the event along.
                  if (!isSorting()) {
                      clearSortingState();
                      fireTableChanged(e);
                      return;
                  // If the table structure has changed, cancel the sorting; the
                  // sorting columns may have been either moved or deleted from
                  // the model.
                  if (e.getFirstRow() == TableModelEvent.HEADER_ROW) {
                      cancelSorting();
                      fireTableChanged(e);
                      return;
                  // We can map a cell event through to the view without widening
                  // when the following conditions apply:
                  // a) all the changes are on one row (e.getFirstRow() == e.getLastRow()) and,
                  // b) all the changes are in one column (column != TableModelEvent.ALL_COLUMNS) and,
                  // c) we are not sorting on that column (getSortingStatus(column) == NOT_SORTED) and,
                  // d) a reverse lookup will not trigger a sort (modelToView != null)
                  // Note: INSERT and DELETE events fail this test as they have column == ALL_COLUMNS.
                  // The last check, for (modelToView != null) is to see if modelToView
                  // is already allocated. If we don't do this check; sorting can become
                  // a performance bottleneck for applications where cells
                  // change rapidly in different parts of the table. If cells
                  // change alternately in the sorting column and then outside of
                  // it this class can end up re-sorting on alternate cell updates -
                  // which can be a performance problem for large tables. The last
                  // clause avoids this problem.
                  int column = e.getColumn();
                  if (e.getFirstRow() == e.getLastRow()
                          && column != TableModelEvent.ALL_COLUMNS
                          && getSortingStatus(column) == NOT_SORTED
                          && modelToView != null) {
                      int viewIndex = getModelToView()[e.getFirstRow()];
                      fireTableChanged(new TableModelEvent(TableSorter.this,
                                                           viewIndex, viewIndex,
                                                           column, e.getType()));
                      return;
                  // Something has happened to the data that may have invalidated the row order.
                  clearSortingState();
                  fireTableDataChanged();
                  return;
          private class MouseHandler extends MouseAdapter {
              public void mouseClicked(MouseEvent e) {
                  JTableHeader h = (JTableHeader) e.getSource();
                  TableColumnModel columnModel = h.getColumnModel();
                  int viewColumn = columnModel.getColumnIndexAtX(e.getX());
                  int column = columnModel.getColumn(viewColumn).getModelIndex();
                  if (column != -1) {
                      int status = getSortingStatus(column);
                      if (!e.isControlDown()) {
                          cancelSorting();
                      // Cycle the sorting states through {NOT_SORTED, ASCENDING, DESCENDING} or
                      // {NOT_SORTED, DESCENDING, ASCENDING} depending on whether shift is pressed.
                      status = status + (e.isShiftDown() ? -1 : 1);
                      status = (status + 4) % 3 - 1; // signed mod, returning {-1, 0, 1}
                      setSortingStatus(column, status);
          private class Arrow implements Icon {
              private boolean descending;
              private int size;
              private int priority;
              public Arrow(boolean descending, int size, int priority) {
                  this.descending = descending;
                  this.size = size;
                  this.priority = priority;
              public void paintIcon(Component c, Graphics g, int x, int y) {
                  Color color = c == null ? Color.GRAY : c.getBackground();
                  // In a compound sort, make each succesive triangle 20%
                  // smaller than the previous one.
                  int dx = (int)(size/2*Math.pow(0.8, priority));
                  int dy = descending ? dx : -dx;
                  // Align icon (roughly) with font baseline.
                  y = y + 5*size/6 + (descending ? -dy : 0);
                  int shift = descending ? 1 : -1;
                  g.translate(x, y);
                  // Right diagonal.
                  g.setColor(color.darker());
                  g.drawLine(dx / 2, dy, 0, 0);
                  g.drawLine(dx / 2, dy + shift, 0, shift);
                  // Left diagonal.
                  g.setColor(color.brighter());
                  g.drawLine(dx / 2, dy, dx, 0);
                  g.drawLine(dx / 2, dy + shift, dx, shift);
                  // Horizontal line.
                  if (descending) {
                      g.setColor(color.darker().darker());
                  } else {
                      g.setColor(color.brighter().brighter());
                  g.drawLine(dx, 0, 0, 0);
                  g.setColor(color);
                  g.translate(-x, -y);
              public int getIconWidth() {
                  return size;
              public int getIconHeight() {
                  return size;
          private class SortableHeaderRenderer implements TableCellRenderer {
              private TableCellRenderer tableCellRenderer;
              public SortableHeaderRenderer(TableCellRenderer tableCellRenderer) {
                  this.tableCellRenderer = tableCellRenderer;
              public Component getTableCellRendererComponent(JTable table,
                                                             Object value,
                                                             boolean isSelected,
                                                             boolean hasFocus,
                                                             int row,
                                                             int column) {
                  Component c = tableCellRenderer.getTableCellRendererComponent(table,
                          value, isSelected, hasFocus, row, column);
                  if (c instanceof JLabel) {
                      JLabel l = (JLabel) c;
                      l.setHorizontalTextPosition(JLabel.LEFT);
                      int modelColumn = table.convertColumnIndexToModel(column);
                      l.setIcon(getHeaderRendererIcon(modelColumn, l.getFont().getSize()));
                  return c;
          private class Directive {
              private int column;
              private int direction;
              public Directive(int column, int direction) {
                  this.column = column;
                  this.direction = direction;
      //-------------- FIX TABLE'S HEIGHT & COLUMN WIDTH ---------
      public class TableHeight_ColumnWidth {
        private TableSorter sorter;
        private FontMetrics fm;
        private JTable table;
        private int numOfRows = 0, totalTableHeight = 0, totalTableWidth = 0;
         * Constructor --- it needs the model as well the JTable as parameters
         * @param sorter - the model
         * @param table - the JTable created based on the model
        public TableHeight_ColumnWidth(TableSorter sorter, JTable table) {
          this.sorter = sorter;
          this.table = table;
          this.fm = table.getFontMetrics(table.getFont());
          this.numOfRows = table.getRowCount();
         * Calculates the width of each column according to the String it contains
         * @param col = the desired column
         * @param fm = the FontMetrics of this column
         * @return - the width of the single column
        public int determineColumnWidth(TableColumn col, FontMetrics fm) {
          int headerWidth = fm.stringWidth((String)col.getHeaderValue());
          int columnNumber = col.getModelIndex();
          int max = headerWidth;
          int columnWidth = 0;
          String cell = "";
          Integer cell_int = new Integer(0);
          Short cell_short = new Short((short)0);
          for (int i = 0; i != sorter.getRowCount(); i++) {
            Object obj = (Object) sorter.getValueAt(i, columnNumber);
            if (obj instanceof String) {
              cell = (String)sorter.getValueAt(i, columnNumber);
            else if (obj instanceof Integer) {
              cell_int = (Integer)sorter.getValueAt(i, columnNumber);
              cell = String.valueOf(cell_int.intValue());
            else if (obj instanceof Short) {
              cell_short = (Short) sorter.getValueAt(i, columnNumber);
              cell = String.valueOf(cell_short.shortValue());
            columnWidth = fm.stringWidth(cell) + 5;
            if (columnWidth > max) {
              max = columnWidth;
          return max + 5;
         * Calculates the total width of the table according to the width of each
         * column.
         * @return - totalTableWidth
        private int fixColumnWidth () {
          int totalTableWidth = 0;
          TableColumn c = null;
          int cw = 0;
          for (int i = 0; i < table.getColumnCount(); i++) {
            c = table.getColumn(table.getColumnName(i));
            cw = this.determineColumnWidth(c, fm);
            c.setPreferredWidth(cw);
            totalTableWidth = totalTableWidth + cw;
            c.setMinWidth(cw);
          return totalTableWidth;
         * Calculates the height of the table according to the height of each row
         * multiplied by 51 (by default) or by the totalRowsCount.
         * @return - totalTableHeight
        private int fixTableHeight(int maxVisibleRows) {
          int rowHeight = table.getRowHeight();
          //Show maxVisibleRows rows maximum
          if (numOfRows > maxVisibleRows) {
            totalTableHeight = rowHeight * maxVisibleRows;
          else {
            totalTableHeight = rowHeight * numOfRows;
          return totalTableHeight;
         * Sets up the table according to the totalTableWidth and totalTableHeight
        public void fixTableLook(int maxVisibleRows) {
          table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
          totalTableWidth = this.fixColumnWidth();
          totalTableHeight = this.fixTableHeight(maxVisibleRows);
          table.setPreferredScrollableViewportSize(new Dimension(totalTableWidth, totalTableHeight));
    }Also, I want some column headers to display their text in two-rows. But by using the HTML technique look at the result in comparison with the previous table I had!
    import javax.swing.JFrame;
    import java.awt.HeadlessException;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.util.Vector;
    import javax.swing.table.DefaultTableModel;
    //--------- TableSorter ----------
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import java.util.List;
    import javax.swing.*;
    import javax.swing.event.TableModelEvent;
    import javax.swing.event.TableModelListener;
    import javax.swing.table.*;
    //---------- Table Height - Column Width
    import javax.swing.table.TableColumn;
    import java.awt.FontMetrics;
    import javax.swing.JTable;
    import java.awt.Dimension;
    * <p>Title: </p>
    * <p>Description: </p>
    * <p>Copyright: Copyright (c) 2006</p>
    * <p>Company: </p>
    * @author not attributable
    * @version 1.0
    public class Test extends JFrame {
      JPanel jPanel1 = new JPanel();
      JPanel jPanel2 = new JPanel();
      JButton jButton1 = new JButton();
      FlowLayout flowLayout1 = new FlowLayout();
      BorderLayout borderLayout1 = new BorderLayout();
      BorderLayout borderLayout2 = new BorderLayout();
      //------------- MY VARIABLES -------------
      String[] columnNames_objectArr = new String[] {
          "Col1",
          "Column 2",
          "Column 3",
          "C4",
          "Col5",
          "Col 6",
      Vector columnNames_vector = new Vector();
      private Vector data_vector = new Vector();
      private Object[][] data_objectArr;
      public TableHeight_ColumnWidth tcw;
      private int totalTableWidth = 0, totalTableHeight = 0;
      private JComboBox jcmbxData = new JComboBox(
          new String[] {"Not Configured", "Switch", "Modem"});
      private int maxVisibleRows = 51;
      TableSorter model;
      private JTable table;
      JScrollPane jScrollPane1;
      public Test(String[] args) throws HeadlessException {
        try {
          jbInit();
          setupGUI();
          launchGUI();
        catch(Exception e) {
          e.printStackTrace();
      private void jbtnClose_actionPerformed(ActionEvent e) {
        dispose();
      private void launchGUI() {
        pack();
        setVisible(true);
        setResizable(false);
      private void setupGUI() {
        columnNames_vector.addElement("<html>Col1</html>");
        columnNames_vector.addElement("<html>Column 2</html>");
        columnNames_vector.addElement("<html>Column 3<br>Second Row</html>");
        columnNames_vector.addElement("<html>C4</html>");
        columnNames_vector.addElement("<html>Col5</html>");
        columnNames_vector.addElement("<html>Col 6</html>");
        Vector row_data1 = new Vector();
        Vector row_data2 = new Vector();
        Vector row_data3 = new Vector();
        Vector row_data4 = new Vector();
        Vector row_data5 = new Vector();
        Vector row_data6 = new Vector();
        row_data1.add("Mary");
        row_data1.add("Campioneato");
        row_data1.add("Snowboarding");
        row_data1.add(new Integer(578987899));
        row_data1.add(new Boolean(false));
        row_data1.add("Not Configured");
        row_data2.add("Alison");
        row_data2.add("Huml");
        row_data2.add("Rowing");
        row_data2.add(new Integer(3));
        row_data2.add(new Boolean(true));
        row_data2.add("Switch");
        row_data3.add("Ka");
        row_data3.add("Walrath");
        row_data3.add("Knitting");
        row_data3.add(new Integer(2));
        row_data3.add(new Boolean(false));
        row_data3.add("Modem");
        row_data4.add("Sharon");
        row_data4.add("Zakhouras");
        row_data4.add("Speed reading");
        row_data4.add(new Integer(20));
        row_data4.add(new Boolean(true));
        row_data4.add("Switch");
        row_data5.add("Philip");
        row_data5.add("Milner");
        row_data5.add("Pool");
        row_data5.add(new Integer(10));
        row_data5.add(new Boolean(false));
        row_data5.add("Not Configured");
        data_vector.add(row_data1);
        data_vector.add(row_data2);
        data_vector.add(row_data3);
        data_vector.add(row_data4);
        data_vector.add(row_data5);
        model = new TableSorter(new SortTableModel(data_vector, columnNames_vector));
        table = new JTable(model);
        tcw = new TableHeight_ColumnWidth(model, table);
        jScrollPane1 = new JScrollPane(table);
        model.setTableHeader(table.getTableHeader());
        jPanel1.add(jScrollPane1, BorderLayout.CENTER);
        //Add a JComboBox as a cellEditor...
        DefaultCellEditor dce = new DefaultCellEditor(jcmbxData);
        table.getColumnModel().getColumn(5).setCellEditor(dce);
        //..... add the IPJPanel as cellEditor.....
        tcw.fixTableLook(maxVisibleRows);
      private void jbInit() throws Exception {
        this.getContentPane().setLayout(borderLayout2);
        jPanel1.setLayout(borderLayout1);
        jPanel2.setLayout(flowLayout1);
        jButton1.setText("Close");
        jPanel1.setBorder(BorderFactory.createRaisedBevelBorder());
        this.getContentPane().add(jPanel1, BorderLayout.CENTER);
        this.getContentPane().add(jPanel2, BorderLayout.SOUTH);
        jPanel2.add(jButton1, null);
        jButton1.addActionListener(new ActionListener() {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          

    First of all I found out that when I set the column's header like this:
    columnNames_vector.addElement("<html>Column 3<br>Second Row</html>"); //In the setupGUI() methodthe TableHeight_ColumnWidth.determineColumnWidth(TableColumn col, FontMetrics fm) method calculates the column's width by counting <html>, </html>, <br> characters as well. So I added a check to reject these characters and not count them for the column's width.
    As for the header's height:
    I found that when the first column's header is set to display two-lines, then the height of the rest columns headers is set to display two-lines as well. In other words, the height of the first column's header affects the height of the whole JTableHeader.
    I found that the BasicTableHeaderUI.getHeaderHeight() method is called, within which there are these comments:
              // If the header value is empty (== "") in the
              // first column (and this column is set up
              // to use the default renderer) we will
              // return zero from this routine and the header
              // will disappear altogether. Avoiding the calculation
              // of the preferred size is such a performance win for
              // most applications that we will continue to
              // use this cheaper calculation, handling these
              // issues as `edge cases'. Should I override a class and if so which one? I am so confused! If anyone has any idea about how to set the header's height according to the cell's height that is the maximum among all, please let me know.

Maybe you are looking for