JTable - Column swap disabling for selected columns

Hi,

In my JTable i want to disable the swapping of selected columns, table.getTableHeader().setReorderingAllowed(false);

this disables the whole table swapping.

Is there anyway to disable only selected column swapping?

Thanks in advance.

Yours,

Arun

[327 byte] By [MeManMea] at [2007-11-27 9:51:51]
# 1
try this: http://forum.java.sun.com/thread.jspa?threadID=601267&messageID=3242336
Yannixa at 2007-7-13 0:21:02 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thanks Yannix,

That link helped me a lot. Now one more problem.

By overriding the mouseDragged()

method in BasicTableHeaderUI.MouseInputHandler

i've made column 0,1 as fixed column. But if i drag column 2 and drop it in either 0 or 1st position the swapping is happening.

My guess is that i've to override the mouseReleased()

method to achive that. Any inputs or clues?

MeManMea at 2007-7-13 0:21:02 > top of Java-index,Desktop,Core GUI APIs...
# 3

I was able to disable the column swapping in all the ways, i.e. i've solved the problem that i've said in my previous post.

The following are the scenario'scovered, if the user wants to disable swapping of 1st and 2nd column of a JTable

1. Donot allow dragging of the 1st and 2nd column and

2. If the user drag-drop 3 or 4 th column in place of 1st or 2nd column, then undo the swap operation by re-swapping the columns.

Try the following example

import java.awt.Point;

import java.awt.event.MouseEvent;

import javax.swing.JFrame;

import javax.swing.JScrollPane;

import javax.swing.JTable;

import javax.swing.event.MouseInputListener;

import javax.swing.plaf.basic.BasicTableHeaderUI;

import javax.swing.table.JTableHeader;

import javax.swing.table.TableColumnModel;

public class FixedTableColumnsExample

{

FixedTableColumnsExample()

{

String[] columnNames = {"First","Last Name","Sport","# of Years","Vegetarian"};

Object[][] data = {

{"Mary", "Campione","Snowboarding", new Integer(5), new Boolean(false)},

{"Alison", "Huml","Rowing", new Integer(3), new Boolean(true)},

{"Kathy", "Walrath","Knitting", new Integer(2), new Boolean(false)},

{"Sharon", "Zakhour","Speed reading", new Integer(20), new Boolean(true)},

{"Philip", "Milne","Pool", new Integer(10), new Boolean(false)}

};

JTable table = new JTable(data, columnNames);

TableColumnModel columnModel = table.getColumnModel();

EditableHeader test = new EditableHeader(columnModel);

table.setTableHeader(test);

JFrame frame = new JFrame("Table Fixed Column Demo");

frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

JScrollPane scrollPane = new JScrollPane(table);

frame.getContentPane().add( scrollPane );

frame.setSize(400, 300);

frame.setVisible(true);

}

public static void main(String[] args)

{

new FixedTableColumnsExample();

}

}

class EditableHeader extends JTableHeader

{

public EditableHeader(TableColumnModel columnModel)

{

super(columnModel);

}

public void updateUI()

{

setUI(new EditableHeaderUI());

}

}

class EditableHeaderUI extends BasicTableHeaderUI

{

boolean is1and2Swapped = true;

protected MouseInputListener createMouseInputListener()

{

return new MouseInputHandler((EditableHeader) header);

}

class MouseInputHandler extends BasicTableHeaderUI.MouseInputHandler

{

protected EditableHeader header;

public MouseInputHandler(EditableHeader header)

{

this.header = header;

}

public void mouseDragged(MouseEvent e)

{

int draggedColumnIndex = ((EditableHeader)e.getSource()).getDraggedColumn().getModelIndex();

if(draggedColumnIndex != 0 && draggedColumnIndex != 1 )

{

super.mouseDragged(e);

is1and2Swapped = true;

}

else

{

is1and2Swapped = false;

}

}

public void mouseReleased(MouseEvent e)

{

int draggedColumnIndex = ((EditableHeader)e.getSource()).getDraggedColumn().getModelIndex();

Point p = e.getPoint();

TableColumnModel columnModel = header.getColumnModel();

if(p.x < 0)

{

p.x = 0;

}

int index = columnModel.getColumnIndexAtX(p.x);

super.mouseReleased(e);

if((index == 0 || index == 1) && is1and2Swapped)

{

header.getColumnModel().moveColumn(index,draggedColumnIndex);

}

}

}

}

Is there anyother better way to do the swap disabling? If so please suggest me.

This solution is having one disadvantage.

Consider the following scenario,

a) 1 and 2 are fixed columns.

b) Move column 4 to 3rd position

c) Move new 3rd column to 1st or 2nd column's position

d) the new 3rd column is moving to position 4(its original position when the model was created) instead of position 3.

This is because the TableColumModel is not updated properly. I'm working on that. Any Suggestions?

Thanks

Arun.

MeManMea at 2007-7-13 0:21:02 > top of Java-index,Desktop,Core GUI APIs...