Select columns in jtable
Hello,
I have a JTable component with 4 columns. I only allow single row selection. If I select a row not all columns are selected. The one you clicked on does not have the selection color. Ho can you select all columns with the selection color ?
regards
Johan
Just guessing because I had a hard time wading though all your code. you probably have a custom renderer on that column. Your renderer needs to change the background color if the cell is selected.
Similar Messages
-
Focus stays on cell when I select column in JTable
Hi,
I am using a JTable and I have defined a function key cntrl-D to perform a function on a selected column. This works fine if I have not yet edited one of the cells in the column, but if I edit any cell and then click the column header and hit cntrl-D, then the column is deselected, the top cell enters edit mode, and my function is called with only one cell selected (always the top cell) instead of the whole column.
When I debug this, I find that my function is called first, and processKeyEvent in the cell editor is called later. The editor doesn't handle it and calls super().
I think that somehow the cell is keeping the focus even though I try to deselect it when the column is selected in my column header listener. Any suggestions welcome.
Here is the code I use to select the column in the column header listener. I have verified that it is called when I click the header:
public void mouseClicked(MouseEvent e)
table.requestFocusInWindow();
Point thePoint = e.getPoint();
int theColumnIndex = table.columnAtPoint(thePoint);
int theLastRowIndex = table.getRowCount() - 1;
if (theLastRowIndex > 0 && theColumnIndex > 0 && theColumnIndex < table.getColumnCount() )
if ( e.isShiftDown() && table.getSelectedRowCount() == table.getRowCount() )
table.editingStopped(null);
if ( table.isEditing() )
return;
if ( theColumnIndex <= mColumnStart )
table.setColumnSelectionInterval( theColumnIndex, mColumnStart);
else if ( theColumnIndex >= mColumnStart )
table.setColumnSelectionInterval( mColumnStart, theColumnIndex );
else if ( e.isControlDown() && table.getSelectedRowCount() == table.getRowCount() )
table.editingStopped(null);
if ( table.isEditing() )
return;
if (table.isColumnSelected(theColumnIndex))
table.removeColumnSelectionInterval(theColumnIndex, theColumnIndex);
else
table.addColumnSelectionInterval(theColumnIndex, theColumnIndex);
else
table.editingStopped(null);
if ( table.isEditing() )
return;
table.clearSelection();
table.setEditingRow(-1);
table.setEditingColumn(-1);
table.setRowSelectionInterval(0, theLastRowIndex);
table.setColumnSelectionInterval(theColumnIndex, theColumnIndex);
mColumnStart = theColumnIndex;
SampleManager.getInstance().setupMenus();I found the answer. This is a feature that worked in 1.3.1 but broke in 1.4.1. It turns out that there is a new feature in JTable that jumps into edit mode when a key event is detected, and that causes the column selection to be canceled. To inactivate this feature I put the following line into the constructor of my JTable subclass:
putClientProperty("JTable.autoStartsEdit", Boolean.FALSE); -
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,
ArunI 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. -
Hello,
i have a selection problem with JTable. I want to allow only single cell selection and additionally limit the selection to the first column.
I preffered the style from MS Outlook Express where you can select the email accounts to edit.
It is a table like this:
Account name | Type | ...
--------------|---------|---------------------
Hotmail | POP3 |
GMX | IMAP |The selection should be only avaibable at 'Hotmail' or 'GMX' - not at 'POP3', 'IMAP' or as complete row selection.
Please help me!
Thanks.
WarlockMaybe this will helpimport java.awt.*;
import javax.swing.*;
public class Test3 extends JFrame {
public Test3() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = getContentPane();
String[] head = {"One", "Two"};
String[][] data = {{"R1-C1", "R1-C2"}, {"R2-C1", "R2-C2"}};
JTable jt = new JTable(data, head);
jt.getColumnModel().setSelectionModel(new MyTableSelectionModel());
content.add(new JScrollPane(jt), BorderLayout.CENTER);
jt.setCellSelectionEnabled(true);
jt.setRowSelectionAllowed(false);
jt.setColumnSelectionAllowed(false);
setSize(300, 300);
setVisible(true);
public static void main(String[] arghs) { new Test3(); }
class MyTableSelectionModel extends DefaultListSelectionModel {
public void setSelectionInterval(int index0, int index1) {
super.setSelectionInterval(0, 0);
} -
How to get the current selected column and row
Hi,
A difficult one, how do i know which column (and row would also be nice) of a JTable is selected?
e.g.
I have a JButton which is called "Edit" when i select a cell in the JTable and click the button "Edit" a new window must be visible as a form where the user can edit the a part of a row.
Then the column which was selected in the JTable must be given (so i need to know current column) and then i want the TextField (the one needed to be edited) be active with requestFocus(). So it would be
pricetextfield.requestFocus();
Problem now is that i have to click every time in the window the JTextField which was selected in the JTable. I have chosen for this way of editing because my application is multi-user and it would be too difficult for me when everybody did editing directly (catch signals, reload data, etc.).
My question is how do I know the current column and the current row in a JTable?I'm not sure what your mean by the "current" row or column, but the following utility methods return
which row and column have focus within the JTable.
public static int getFocusRow(JTable table) {
return table.getSelectionModel().getLeadSelectionIndex();
public static int getFocusColumn(JTable table) {
return table.getColumnModel().getSelectionModel().getLeadSelectionIndex();
} -
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. -
Remove Cell Selection Border in JTable
Hi
I have multiple columns in JTable populated with data. When the row is selected, the row changes colour. It also places a border around the cell that is selected within the row.
Is there some way to remove this border?
Any help would be appreciated.
Thanks
GreggHi,
you need a different tableCellRenderer. Read:
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#custom
There are also code examples.
Phil -
Report Does Not Follow Select Column Order
Hi all,
Apex 2.2 on 10gXE
I always encounter this problem, the display column on report does not follow the select column sequence order in the "region source".
It is very tedious clicking the up/down arrow one-by-one in the "report attributes" especially if the display columns are plenty like 40 columns.
Is there a fix/patch or alternative tips for this bug?
Thanks a lot,
Edited by: 843228 on May 24, 2011 10:32 PMApex 2.2 on 10gXEStart by upgrading from this old unsupported version.
>
I always encounter this problem, the display column on report does not follow the select column sequence order in the "region source".
It is very tedious clicking the up/down arrow one-by-one in the "report attributes" especially if the display columns are plenty like 40 columns.
Is there a fix/patch or alternative tips for this bug?
>
This is not a bug. APEX maintains the original column order of the report. This avoids problems downstream in areas like the processing of <tt>g_fnn</tt> arrays in declarative tabular forms, where the assigned arrays are column-order dependent.
APEX 4.x fixes column ordering bugs that could lead to report corruption, and includes drag-and-drop column ordering in the new tree view in addition to the up/down arrow re-ordering.
If you really want column order to follow that in the source query, the workaround is to create a new report using the modified query. -
Interactive Report - Include Non-selected Column in Row Text Search
Hi,
We have an interactive report that contains many columns. NAME VARCHAR2(30), TYPE VARCHAR2(30) etc and DESCRIPTION VARCHAR2(4000).
Normally the DECRIPTION column would not be shown (avalilable for dispaly via 'Select Columns' but not selected), however we need the default Row Text Search (ie direct entry into the search field instead of creating a column filter) to search the DESCRIPTION field wether it is selected for display or not.
This will allow the user to enter a term that may be contained in the DESCRIPTION to find records without trying to display a 4000 character field in the report - which just looks horrible.
We are using ApEx 4.1.1.
Thanks,
Martin FiggHi Ray,
As I expected this has got us very close. We went with the code:
select
facility_id,
facility_name,
'<span title="' || facility_desc|| '">'||substr(facility_desc,0,50)||decode(sign(length( facility_desc)-50),1,' (More...)',NULL)||'</span>' facility_desc,
equipment_id,
equipment_name,
'<span title="' || equipment_desc|| '">'||substr(equipment_desc,0,50)||decode(sign(length( equipment_desc)-50),1,' (More...)',NULL)||'</span>' equipment_desc,
from
rf_full_item_list_vwhich works well. The only issue is that when you download to csv you get the html tags etc as well. Put I am a lot closer to a solution than we were.
Thanks again.
Martin -
How to find out the selected column in Table Control
Hi all,
How to find out the selected column in Table Control?
Thanks & Regards,
YJRHi,
Let your table control name in Screen painter be TC1.
READ TABLE TC1-COLS INTO WA_COLS (some wok area)
WITH KEY SELECTED = 'X'.
IF SY-SUBRC = 0.
CLEAR: W_DUMMY, W_COL_NAME.
SPLIT WA_COLS-SCREEN-NAME AT '-' INTO W_DUMMY
W_COL_NAME.
endif.
W_COL_NAME gives you the column name.
Hope it helps.
cheers
sharmistha -
How to select columns in a table by their column id and not the column name
Hello,
How do you select columns from a table based on their column_id.
I have create a view
but other than Select * , i cant now select one just one column from it
the view as follow:
create view count_student as
select a.acode "acode",aname "Activity Name",count(ae.acode) "Number of student joined"
from aenrol ae, activity a
where a.acode= ae.acode
group by a.acode,aname;The issue is that you have decided to use quoted column names. A pretty horrible idea (mostly for the reasons that you are now finding).
Re-create the view and get rid of the silly double quotes. -
How to export selected columns in a table using expdp of oracle10g
Hi all..
I have a table with 10 columns and i want to export only 4 columns(selected columns) data using expdp
Pl. tell me if we can do this and if yes what is the syntax.
Thanks..
SekharThat's not possible, you could use QUERY to specify where clause but not columns in select clause,
for example,
QUERY=employees:'"WHERE department_id > 10 AND salary > 10000"'
you could use
create table2export as select c1,c2,c3,c4 from tableof10columns;and export the temp table. -
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 -
Changes to selected columns to display in Details tab in Task Manager are not saved during a reboot
I use Task Manager on Server 2008 R2 to monitor performance. I have added several columns to the Processes tab using View..Select Columns. I have added CPU time, I/O read/write/other bytes, etc.
The equivalent in Server 2012 is the Details tab. I figured out how to add these same columns - right click on the column headers, and choose "Select columns". This is not obvious by the way.
However these selections are not persistent across a reboot. So every time I reboot, I need to add the columns back to the display. That is not the case with Server 2008 R2 - they are persistent.
I am using a domain administrator userid, but not using roaming profiles.
This is a retrogression vs. Server 2008 R2. Can it be fixed?Hi,
I meant that both the added columns of Windows Server 2008 R2 and Windows Serve r2012 RC are persistent.
You may install a new Windows Serve r2012 RC to check the symptom.
Currently, you may perform a System File Checker (SFC /Scannow) to check the result.
Regards,
Arthur Li
TechNet Community Support
Arthur, I have the exact same problem as David Trounce. The problem exists, as he described it, and I don't think you are understanding it. Please pass this problem/bug/defect report to someone else so that it can be logged and fixed before
RTM. Thanks! -Kent
Maybe you are looking for
-
A Case Study Question in the Use of Stills with Final Cut Pro
Believe it or not, for the last week I have been reading endlessly about pixels, resolution, aspect ratio, Photoshop resizing, the bloody battle over the usefulness of the term "dpi" in video discussions, and generally about what one should do in ord
-
Getting a new hard drive! Need HELP!
Okay, so I know how to switch out a hard drive in a Macbook Pro...no big deal. My question is if there is an easy way to get al your personal info, files, iphoto library, music...off the laptop onto one external hard drive. I figure I can hunt around
-
Issue with Electroni bank statement
Hi Gurus while uplaoding the EBS file i got the error message tht do not have authrorization for FB01, i was not tried to post any thing with FB01 but i understood that i this program might caled the FB01 transactin in back ground when i see the stat
-
WebSphere Default Server Starting Problem
I am using WebSphere AS and facing a strange problem at the time of starting the default server. I had to change the user name & password of the XADataSource of the Server. On applying this, and starting the Default server, the server started and app
-
Syncing Error! HELP! Error 354027
Everytime I sync my iPhone 4 to iTunes, it syncs everything but says that "354027 could not be copied to the phone because it could not be found". Does anyone know what this error means and how it can be fixed?