Dragging columns in JTable
I am dragging a JTable column with my mouse. If I release the column, will an event be fired indicating that the column was dragged AND is now being released??
The mouseReleased(MouseEvent e) in MouseListener() will just inform me of the release of the mouse button, it will not inform me whether this release of the mouse button has induced the release of a dragged column.
The columnMoved(TableColumnModelEvent e) in TableColumnModelListener() will give me the coordinates of a column WHILE it is being dragged. It will not inform me if a column gets released.
If no specific event exist to indicate the release of a dragged column, I guess I'll try to obtain the same result by combining both the mouseReleased method and the columnMoved method.
Thanks in advance.
Olivier
try attaching MouseMotionListener as well as MouseListener to the table header. when mouse drag starts, make a boolean variable, say dragged true, and then in mouseReleased() method, check if dragged is true or not. if true, it'll meet your both the requirements.
add this code to your code.
private boolean dragged = false;
private void addDragListener(JTable tableObj) {
final JTable table = tableObj;
MouseMotionAdapter motionListener = new MouseMotionAdapter() {
public void mouseDragged(MouseEvent me) {
dragged = true;
TableColumnModel columnModel = table.getColumnModel();
int viewColumn = columnModel.getColumnIndexAtX(me.getX());
int column = table.convertColumnIndexToModel(viewColumn);
System.out.println("column " + column +" dragged");
MouseAdapter mouseListener = new MouseAdapter() {
public void mouseReleased(MouseEvent me) {
if(dragged) {
System.out.println("column released after dragging");
dragged = false;
JTableHeader th = table.getTableHeader();
th.addMouseMotionListener(motionListener);
th.addMouseListener(mouseListener);
hope this helps,
Similar Messages
-
Hi,
How can I protect the JTable for the user don�t press the mouse for drag the column position???
thanks!!!!Try this:
JTable objTable = new jTable();
objTable.getTableHeader().setReorderingAllowed(false); -
how to drag columns in a JTable ?
JTable myTable = new JTable(mymodel,myheaders);
JTableHeader head =myTable.getTableHeader();
//// this function will solve your problem
head.setReorderingAllowed(true); -
Hiding a column in jtable made from DefaultTableModel.
I have made my jtable from DefaultTableModel.
I want to keep one column in the jtable as hidden storing some data containing neccessary information like the "path of the file"
which need not be shown to the user.
Please tell me how I can hide one column in the Jtable.
please provide siome link or code for the same.
Tia,
Sarwadayanandabv wrote:
[http://search.sun.com/search/onesearch/index.jsp?qt=hide+column%2B+JTable&rfsubcat=&col=developer-forums]
My thought exactly.
db -
How to set different renderers to different cells in same column of JTable?
Hello Friends,
I need your help again...
Does any body knows, how to set different renderer's for different cells of same column in JTable..??
For ex.
Col1 Col2 Col3 Col4
A A A A
A A A B
A A A C
A A A D
Where A B C D would be different Renderers. I want set exactly same ,,, ie. one column with different renderer at different cell positions..
Right now i m setting renderer using statement bellow :
table.getColumnModel().getColumn(int).setCellRenderer(rederer_Instance);But with this, effect in the last renderer is applicable whole column....
Can any body help me out ?????????
please refer this thread for similar kind of discussion...
http://forums.sun.com/thread.jspa?forumID=57&threadID=571445Thanks
SuyogPlease refer to the first reply of [this thread|http://forums.sun.com/thread.jspa?forumID=57&threadID=571445] for the answer. If you have a specific problem implementing it, post you code with a specific question.
-
Add and remove columns from JTable
Help me please!
A try to remove column from JTable. It's removed, but when I try to add column in table, then I get all old (removed early) columns + new column....
I completely confused with it.....
Here is my code for remove column:
class DelC implements ActionListener
public void actionPerformed (ActionEvent e )
int [] HowManyColDelete = table.getSelectedColumns();
if (HowManyColDelete.length !=0)
TableColumnModel tableCModel = table.getColumnModel();
for (int i = HowManyColDelete.length-1; i>-1; i--)
table.getColumnModel().removeColumn (tableCModel.getColumn (HowManyColDelete [ i ]));
else
JOptionPane.showMessageDialog(JOptionPane.getRootFrame(), "Column is not selected!");
}It's little ex for me, I just try understand clearly how it's work (table models i mean). Here is code. All action with tables take place through menu items.
My brain is boiled, I've try a lot of variants of code, but did't get right result :((
It's code represent problem, which I've describe above. If you'll try remove column and then add it again, it will be ma-a-a-any colunms...
I understand, that my code just hide columns, not delete from table model....
But now I have not any decision of my problem...
Thanks a lot for any help. :)
import javax.swing.*;
import java.awt.*;
import javax.swing.table.*;
import java.awt.event.*;
import javax.swing.table.DefaultTableModel;
class JTableF extends JFrame
Object [] [] data = new Object [0] [2];
JTable table;
DefaultTableModel model;
String [] columnNames = {"1", "2"};
TableColumnModel cm;
JTableF()
super("Table features");
setDefaultLookAndFeelDecorated( true );
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
JMenuBar MBar = new JMenuBar();
JMenu [] menus = {new JMenu("A"), new JMenu("B")};
JMenuItem [] menu1 = {new JMenuItem("Add row"), new JMenuItem("Delete row", 'D'), new JMenuItem("Add column"), new JMenuItem("Delete column")};
menu1 [ 0 ].addActionListener(new AddL());
menu1 [ 1 ].addActionListener(new DelL());
menu1 [ 2 ].addActionListener(new AddC());
menu1 [ 3 ].addActionListener(new DelC());
for (int i=0; i<menu1.length; i++)
menus [ 0 ].add( menu1 [ i ]);
for (int i=0; i<menus.length; i++)
MBar.add(menus );
JPanel panel = new JPanel ();
model = new DefaultTableModel( data, columnNames );
table = new JTable (model);
cm = table.getColumnModel();
panel.add (new JScrollPane(table));
JButton b = new JButton ("Add row button");
b.addActionListener(new AddL());
panel.add (b);
setJMenuBar (MBar);
getContentPane().add(panel);
pack();
setLocationRelativeTo (null);
setVisible (true);
class DelC implements ActionListener
public void actionPerformed (ActionEvent e )
int [] HowManyColDelete = table.getSelectedColumns();
if (HowManyColDelete.length !=0)
TableColumnModel tableCModel = table.getColumnModel();
for (int i = HowManyColDelete.length-1; i>-1; i--)
int vizibleCol = table.convertColumnIndexToView(HowManyColDelete [ i ]);
tableCModel.removeColumn (tableCModel.getColumn (vizibleCol));
//cm = tableCModel;
else
JOptionPane.showMessageDialog(JOptionPane.getRootFrame(), "Column is not selected!");
class AddC implements ActionListener
public void actionPerformed (ActionEvent e)
//table.setColumnModel(cm);
Object NewColumnName = new String();
NewColumnName = JOptionPane.showInputDialog ("Input new column name", "Here");
int i = model.getRowCount();
int j = model.getColumnCount();
Object [] newData = new Object [ i ];
model.addColumn ( NewColumnName, newData);
class AddL implements ActionListener
public void actionPerformed (ActionEvent e)
int i = model.getColumnCount();
Object [] Row = new Object [ i ];
model.addRow ( Row );
class DelL implements ActionListener
public void actionPerformed (ActionEvent e)
int [] HowManyRowsDelete = table.getSelectedRows();
if (HowManyRowsDelete.length !=0)
for (int k = HowManyRowsDelete.length-1; k>-1; k--)
model.removeRow (HowManyRowsDelete[k]);
else
JOptionPane.showMessageDialog(JOptionPane.getRootFrame(), "Row is not selected!");
public static void main (String [] args)
javax.swing.SwingUtilities.invokeLater(new Runnable()
public void run()
JTableF inst = new JTableF(); -
Keyboard Access For a Column in JTable with default editor as JComboBox
I want to get Key board access for a column in JTable.
The user should be able to select from a drop down list for the column with default
editor set as JComboBox.
Presently,it works fine with mouse,also I am able to focus it with Keyboard using
ALT+Up keys,but how to make drop down list appear.
Plz help,it's urgent.
Thanks in AdvanceHi,
In addition to setting DO_SUM = 'X' you need to specify function in H_FTYPE field. It should be set to 'AVG' in your case.
ls_fielcat-do_sum = 'X'.
ls_fieldcat-h_ftype = 'AVG. -
Serial number column in Jtable
Apologies for this distraction.
I read at this forum about a year ago how to add a s/n column in Jtable such that all the nos are automatically updated/adjusted when a row is added/deleted. the count starts from the top down serially and unaffected by sorting.
pls help!thanks a million BaltimoreJohn and especially camickr whose solutions to problems have been most helpful. applying the referenced post, i worked around the problem by resetting the values of the column having the serial number, based on table rowCount every time there is an operation on it(deleting, addition etc)
-
Problem with Hiding Columns in JTable
Hi.
Who knows how to hide column in JTable.
I try to get column:
MyTab.getColumnModel().getColumn(i)But there is no methods like SetVisible(false)
What to DO???http://www.google.com/search?hl=en&q=%2Bhide+%2Bcolumn+%2Bjtable&btnG=Google+Search
-
Programmatically Sorting columns in JTable
Hi,
How can I programmatically sort a particular column in JTable without having to click on column header.
Thanks.to sort the desired column w/o click on header
int columnIndexToSort = 1;
RowSorter.SortKey sortKey = new RowSorter.SortKey(columnIndexToSort,SortOrder.ASCENDING);
ArrayList alist = new ArrayList(1);
alist.add(sortKey);
((DefaultRowSorter)table.getRowSorter()).setSortable(columnIndexToSort , true);
((DefaultRowSorter)table.getRowSorter()).setSortKeys(alist);
((DefaultRowSorter)table.getRowSorter()).sort();expecting dukestars.. -
Hai Friends,
i need suggestions for the following,
How to freeze the columns in swing JTable.?????????????
I need to freeze 3 columns in JTable... how it can be done?
eg: its like the option used in excel for freezing the panes. like that, i need to freeze the column in JTable. so that, it wont be scrolled while scrolling the records, it should also be done in colmns of jtable for horizontal scroll n row for vertical scroll. is this can be done in JTable. Can u pls help me out in this...?
Regards,
Ciya.Did you actually try the code which was pointed out to you when you [url http://forum.java.sun.com/thread.jspa?threadID=701198]asked this question before? It does just what you want...
-
I wont to hide a elected column in JTable.
how it to doTableColumnModel tcm = table.getColumnModel();
TableColumn column = tcm.getColumn( modelIndex );
tcm.removeColumn( column );
Next time, search the forum first. There are at least a dozen posts asking this question. -
Show/hide a column in JTable
I want to show/hide columns in JTable
How is this possible
Note that columns that are hidden may be showed later
thanks in advance
RenjithI tried out for this:TableColumn col = jTable.getColumnModel().getColumn( colnum );
if( col != null )
col.setPreferredWidth( 0 );
col.setMaxWidth( 0 );
}But the column seems to have minimum width which you can't even change with col.setMinWidth( 0 ); -
Dear all,
I want to hide a column in JTable object, I already tried resize it to 0 but the effect is not good, I also cannot remove the column, since I need this column for other process.
So, any other good idea besides resizes and remove?
Thank you very much!I have tried that too, but the column isn't hide, the size is not actually come to 0,...That's because JTable has a default minimum width of 15.... if you want to hide the column, you must set the minwidth as well. For example:
myTable.getColumnModel().getColumn(column_to_hide).setMinWidth(0);
myTable.getColumnModel().getColumn(column_to_hide).setPreferredWidth(0);;o)
V.V. -
To setup scrollbars for individual columns in JTable
Hi,
I need to setup individual scrollbars for the
columns in JTable.
Can anybody help?
Thanks in advance.
Rgds,
sitaHi,
I need to setup individual scrollbars for the
columns in JTable.If i understand you correctly, you want to have a scrollbar for every column. If so, you will need a new instance of JTable and JScrollPane for every column, but that's a lot of work if you want to do it fancy.
The good news, is you can reuse the table model.
You could do something like that.
for (int i= 0; i < model.getColumnCount(); i++) {
JTable table = new JTable();
table.setAutoCreateColumnsFromModel(false);
//container
someContainer.add(new ScrollPane(table), null/* or some layout constrains*/);
TableColumn tc = new TableColumn(i, 120);
tc.setHeaderValue(model.getColumnName(i));
table.addColumn(tc);
Can anybody help?
Thanks in advance.
Rgds,
sita
Maybe you are looking for
-
Right item category not getting copied while copying sales order
Hi, We have a scenario where we are coping an existing order document and maintaining a new order document. The problem is that when we are copying the existing order, we ll not get the same item categories in the copied order as available in the ori
-
Why does numbers tell me document is invalid and it won't open a spreadsheet?
-
How to perform Error Handling in this Bdc Code
Hi, I had created this BDC for the tcode MB1B and i want to know how to perform the ERROR HANDLING in it .. Plzz provide me guidelines for doing it . here's d code:- report ZBDC_MB1B no standard page heading line-size 255. include bdcrecx1. da
-
I need to reload Adobe Photoshop CS4 Extended.
I have the key but not the original download. I have had a M/Board die & need to reload everything with a new OS. I do not have the download file saved.
-
my iphone automatically locked and it does not open again,but if u call the sim that was inserted you will hear that it was active,so please help me how to open my iphone 4,no display appeared if you touch the switch button,