Show hidden columns in a JTable

Hi,
I have a requirement for hiding some columns of a JTable and showing them back based on user actions.
I have gone through some topics about hiding columns. which can be done by table.removeColumn();
But when I use table.addColumn(); it adds the column at the end of the table.It should add in the same location as the previous column.
How to show /reveal the hidden column back?.

* Hide_Columns.java
* This code contains a table model that allows you to
* specify the columns to be hidden in a boolean array.
* To use the model:
*       model = new MyTableModel(data, columnNames);
*       table.setModel(model);
* The most important method in the model is "getNumber()", which converts a column number
* into the number corresponding to the data to be displayed.
* Visible columns can be dynamically changed with
* model.setVisibleColumns(0, column0.isSelected());
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class Hide_Columns extends JFrame {
    public Hide_Columns() {
        setTitle("Hide columns");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        model = new MyTableModel(data, columnNames);
        table.setModel(model);
        getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
        for( int i=0; i<4; i++ ){
            final JCheckBox columnX = new JCheckBox("Column "+i);
            columnX.setSelected(true);
            final int col = i;
            columnX.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    model.setVisibleColumns(col, columnX.isSelected());
            toolBar.add(columnX);
        getContentPane().add(toolBar, BorderLayout.NORTH);
        setSize(400,300);
        setLocationRelativeTo(null);
        model.addRow(new Object[]{null,null,null,null});
        model.setValueAt("test0",0,0);
        model.setValueAt("test1",0,1);
        model.setValueAt("test2",0,2);
        model.setValueAt("test3",0,3);
    public static void main(String args[]) { new Hide_Columns().setVisible(true); }
    private JTable table = new JTable();
    private  JToolBar toolBar = new JToolBar();
    private   MyTableModel model;
    /** This is the data of the table*/
    private  Vector<Object> data = new Vector<Object>();
    /** Column names */
    Vector<String> columnNames = new Vector<String>();{
        columnNames.addElement("0");
        columnNames.addElement("1");
        columnNames.addElement("2");
        columnNames.addElement("3");
class MyTableModel extends DefaultTableModel {
    /** Shows which columns are visible */
    private   boolean[] visibleColumns = new boolean[4];{
        visibleColumns[0] = true;
        visibleColumns[1] = true;
        visibleColumns[2] = true;
        visibleColumns[3] = true;
    public MyTableModel(Vector<Object> data, Vector<String> columnNames){
        super(data, columnNames);
    protected void setVisibleColumns(int col, boolean selection){
        visibleColumns[col] = selection;
        fireTableStructureChanged();
     * This function converts a column number of the table into
     * the right number of the data.
    protected int getNumber(int column) {
        int n = column;    // right number
        int i = 0;
        do {
            if (!(visibleColumns)) n++;
i++;
} while (i < n);
// When we are on an invisible column,
// we must go on one step
while (!(visibleColumns[n])) n++;
return n;
// *** METHODS OF THE TABLE MODEL ***
public int getColumnCount() {
int n = 0;
for (int i = 0; i < 4; i++)
if (visibleColumns[i]) n++;
return n;
public Object getValueAt(int row, int column) {
return super.getValueAt(row, getNumber(column));
public void setValueAt(Object obj, int row, int column) {
super.setValueAt(obj, row, getNumber(column));
public String getColumnName(int column) {
return super.getColumnName(getNumber(column));

Similar Messages

  • How to show hidden columns list

    Hi,
    I got one requirement,
    when user hide comuns using actions in IR,
    when he log out and log in next time, have to display column names, which columns are hidden(if any)?
    Thanks in advance

    You can only achieve this by using custom task forms. Check below links for creating custom task forms.
    http://office.microsoft.com/en-us/office365-sharepoint-online-enterprise-help/customize-a-task-details-page-HA103858063.aspx
    http://msdn.microsoft.com/en-us/library/office/dn518136(v=office.15).aspx
    Adnan Amin MCT, SharePoint Architect | If you find this post useful kindly please mark it as an answer.

  • How can i make hidden column in JTable

    hi, how can i make hidden column in JTable,
    basically i have a ID field in JTable, i have to use this ID , but i also dont want to show this ID in JTable.
    any idea how ??

    staiji its not working
    i did this :
    first :
    TableColumnModel columnModel =
    usersTable.getColumnModel();TableColumn column =
    columnModel.getColumn(1);
    usersTable.removeColumn(column);
    then when i trying to get this :
    Integer userId =
    (Integer)usersTable.getValueAt(UserBrowser.this.usersTa
    le.getSelectedRow(), 0);
    it not give me ID column's value .
    i have a column in JTable like :
    ID | Username | First name | Last name
    i want to hide ID column , but get this ID's value
    when user clicks on JTable row.Hi, if you would read the documentation about JTable.getValueAt(...) you will find, that there is a significant difference between this method and the datamodels getValueAt(...) method. JTables getValueAt(...) method interprets column-index as index in its TableColumnModel - in your TableColumnModel there is no column any longer that holds ID values, therefore you were not able to get it by JTable.getValueAt(...). Do not use these methods for the purpose you want it for - you will also get the wrong values, if the user has repositioned columns - the column index is always interpreted as index in the currently used TableColumnModel and IS NOT A MODELINDEX.
    greetings Marsian

  • JTable, stopping navigation into hidden columns?

    Hello,
    In JTable I have done .setPreferedWidth(0) along with every other width setting =0; I did this to hide columns which are necessary but uninformative to the user like primary key ids. The problem is that you can still hit tab, shift-tab, and right arrow which will put the "focus" in the hidden columns, effectively 'losing' the cursor for the user.
    How do I fix this?
    I've tried using focusLost listeners but they don't fire every time I need. When a user starts editing a cell by arrow-keying over then just typing (never double clicking), then subsequently exiting the cell using the tab key or right arrow, I don't get the event to fire.
    I've also tried a KeyListener, but this doesn't work every time I'd expect.
    Any help would be greatly appreciated!!!

    There is a way to make the navigation keys skip the column with a width of zero, but the real question is how badly do you want to get it done? If you want it bad enough, then I suggest you override JTable's processKeyBinding method and for every navigation key (up, down, left, right, tab, shift-tab, home, end, etc., etc., etc.) check to see if it lands on such a column, if so manually move to the non-zero column in the direction of the key -- otherwise, let JTable's own processKeyBinding method consume the event.
    ;o)
    V.V.
    PS: If you are one of those people who don't like to go down to the low level, you can add as many InputMap/ActionMap to take care of these keys.

  • How do I get a hidden column to show in a library?

    I am having an odd behavior on one of my site columns, and am hoping someone might be able to shed some light on this for me. I have just built a content type hub. Also created some site columns....I have one particular column (which is a choice type), that
    is hidden at the parent content type level. I see this hidden column each time i view the settings of any content type that inherits from the parent content type. However, when I add these child content types to a library, I no longer see the hidden column.
    I don't understand why this is the case. 
    Can anyone shed some light on this for me? Is this the way it is designed to work in SharePoint?
    Update: When I view the inheriting content type from the hub, I see the hidden column. But if I view the same content type from within my site, I do not see the hidden column. Is this a publishing issue? Or is this by design in SharePoint?

    That definitely does not sound like design.  Have you tried editing the content type on the library level NOT the hub level?  You should be able to set the hidden status to display within the Content Type section on the library.
    If that doesn't work, it'll be worth looking at something like SharePoint Manager to open up the column and inspect its properties.
    Steven Andrews
    SharePoint Business Analyst: LiveNation Entertainment
    Blog: baron72.wordpress.com
    Twitter: Follow @backpackerd00d
    My Wiki Articles:
    CodePlex Corner Series
    Please remember to mark your question as "answered" if this solves (or helps) your problem.

  • Column Hide in JTable

    Hi There,
    We are using Jtable to show data using DefaultTableModel. Now we have to implement some hidden columns in this existing JTable.
    For this we have tried many ways but not succeeded.
    1. We have use width=0 and setting header caption ="" but it shows column with some width insteed of being hidden.
    2. further we tried dtm.setColumnCount(numCols) to set required number of columns to be displayed and it does so but ignore the rest columns. So this way is not useful as well.
    Please suggest us how to implement hidden columns in JTable ?
    Thanx in Adv,

    I would strongly recommend that you use JXTable instead from the SwingX project. It even has a simple Popup Column Control that helps you hide and show columns as desired. But here is some simple code that might be useful
    import java.awt.Point;
    import java.awt.Rectangle;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.util.Enumeration;
    import java.util.Vector;
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableColumnModel;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.JTableHeader;
    import javax.swing.table.TableCellRenderer;
    import javax.swing.table.TableColumn;
    import javax.swing.table.TableModel;
    public class TableUtilities {
         * Restores a hidden TableColumn based on the supplied column index and sets to column
         * width to the supplied preferredWidth
         * @param table
         * @param modelIndex
         * @param columnWidth
        public static void restoreColumn(JTable table, int modelIndex, int columnWidth) {
            if (table.getColumnCount() <= 0) {
                return;
            TableColumn column = new TableColumn(modelIndex);
            column.setHeaderValue(table.getModel().getColumnName(modelIndex));
            column.setCellRenderer(table.getCellRenderer(0, modelIndex));
            column.setCellEditor(table.getCellEditor(0, modelIndex));
            column.setPreferredWidth(columnWidth);
            DefaultTableColumnModel model = (DefaultTableColumnModel) table.getColumnModel();
            model.addColumn(column);
            model.moveColumn(model.getColumnCount() - 1, modelIndex);
         * Restores a hidden TableColumn based on the supplied column index.
         * @param table
         * @param modelIndex
        public static void restoreColumn(JTable table, int modelIndex) {
            TableColumn column = new TableColumn(modelIndex);
            column.setHeaderValue(table.getModel().getColumnName(modelIndex));
            column.setCellRenderer(table.getCellRenderer(0, modelIndex));
            column.setCellEditor(table.getCellEditor(0, modelIndex));
            column.setPreferredWidth(120);
            DefaultTableColumnModel model = (DefaultTableColumnModel) table.getColumnModel();
            model.addColumn(column);
            model.moveColumn(model.getColumnCount() - 1, modelIndex);
         * Restores a hidden TableColumn based on the supplied column name.
         * @param table
         * @param column
        public static void restoreColumn(JTable table, String column) {
            restoreColumn(table, getColumnIndex(table.getModel(), column));
         * Restores a hidden TableColumn based on the supplied column name and sets to column
         * width to the supplied preferredWidth
         * @param table
         * @param columnWidth
         * @param column
        public static void restoreColumn(JTable table, String column, int columnWidth) {
            restoreColumn(table, getColumnIndex(table.getModel(), column), columnWidth);
         * Carries out the process to hide a table column from the view based on the supplied
         * column name
         * @param table
         * @param column
        public static void hideColumn(JTable table, String column) {
            hideColumn(table, table.convertColumnIndexToView(getColumnIndex(table.getModel(), column)));
         * Carries out the process to hide a table column from the view based on the supplied
         * column index
         * @param table
         * @param column
        public static void hideColumn(JTable table, int column) {
            DefaultTableColumnModel model = (DefaultTableColumnModel) table.getColumnModel();
            if (model.getColumnCount() != 0 && column != -1) {
                model.removeColumn(model.getColumn(column));
         * Returns a Vector of Strings containing the columnNames of for the specified TableModel
         * @param model
         * @return
        public static Vector<String> getColumnNames(TableModel model) {
            Vector<String> colNames = new Vector<String>();
            for (int i = 0; i < model.getColumnCount(); i++) {
                colNames.add(model.getColumnName(i));
            return colNames;
         * Returns the first index of the column with the specified name in the supplied TableModel.
         * @param model
         * @param name
         * @return
        public static int getColumnIndex(TableModel model, String name) {
            for (int i = 0; i < model.getColumnCount(); i++) {
                if (model.getColumnName(i).equalsIgnoreCase(name)) {
                    return i;
            return -1;
    }ICE
    Edited by: fadugyamfi on Aug 17, 2009 11:05 AM

  • Hiding a column in a JTable

    How can I code something so that when I click on a button, it will go and hide or show various columns from the table?
    I want to do this, so the JTable won't take up the whole screen.
    Vermelh0

    make 2 vectors.
    1 vector x with all the values
    1 vector y with only the values you want to display
    put vector y into the table.
    when you want to change whats in the table, redo vector y clear the table and put vector y back in.
    I would code that, but I'm lazy and I hate doing tables....

  • Custom table model, table sorter, and cell renderer to use hidden columns

    Hello,
    I'm having a hard time figuring out the best way to go about this. I currently have a JTable with an custom table model to make the cells immutable. Furthermore, I have a "hidden" column in the table model so that I can access the items selected from a database by their recid, and then another hidden column that keeps track of the appropriate color for a custom cell renderer.
    Subject -- Sender -- Date hidden rec id color
    Hello Pete Jan 15, 2003 2900 blue
    Basically, when a row is selected, it grabs the record id from the hidden column. This essentially allows me to have a data[][] object independent of the one that is used to display the JTable. Instinctively, this does not seem right, but I don't know how else to do it. I know that the DefaultTableModel uses a Vector even when it's constructed with an array and I've read elsewhere that it's not a good idea to do what I'm trying to do.
    The other complication is that I have a table sorter as well. So, when it sorts the objects in the table, I have it recreate the data array and then set the data array of the ImmutableTableModel when it has rearranged all of the items in the array.
    On top of this, I have a custom cell renderer as well. This checks yet another hidden field and displays the row accordingly. So, not only does the table sort need to inform the table model of a change in the data structure, but also the cell renderer.
    Is there a better way to keep the data in sync between all of these?

    To the OP, having hidden columns is just fine, I do that all the time.. Nothing says you have to display ALL the info you have..
    Now, the column appears to be sorting properly
    whenever a new row is added. However, when I attempt
    to remove the selected row, it now removes a seemingly
    random row and I am left with an unselectable blank
    line in my JTable.I have a class that uses an int[] to index the data.. The table model displays rows in order of the index, not the actual order of the data (in my case a Vector of Object[]'s).. Saves a lotta work when sorting..
    If you're using a similar indexing scheme: If you're deleting a row, you have to delete the data in the vector at the INDEX table.getSelectedRow(), not the actual data contained at
    vector.elementAt(table.getSelectedRow()). This would account for a seemingly 'random' row getting deleted, instead of the row you intend.
    Because the row is unselectable, it sounds like you have a null in your model where you should have a row of data.. When you do
    vector.removeElementAt(int), the Vector class packs itself. An array does not. If you have an array, when you delete the row you must make sure you dont have that gap.. Make a new array of
    (old array length-1), populate it, and give it back to your model.. Using Vectors makes this automatic.
    Also, you must make sure your model knows the data changed:
    model.fireTableDataChanged(); otherwise it has no idea anything happened..
    IDK if that's how you're doing it, but it sounds remarkably similar to what I went thru when I put all this together..

  • Grand total on a Hidden column in OBIEE11g

    Hi All,
    Is any way to display a grand total value for a hidden column in OBIEE11g analysis.
    Actually my requirement is to show a total count of specific values(lets say 'Yes') in column which contain 'Yes' and 'No' values.
    So for this, i have created one hidden column and that contain 1 and 0 for corresponding 'Yes' and 'No' and then sum of this column will solve my purpose. But i dont want to show this column in my report. Is any way to do this?
    Thanks,
    Archie.

    Err..ok. If u want to show total count of yes in grand total section...then just filter ur report by 'yes' and change the aagr rule of same col to count in fx. No need for seperate column here. Ur report will just show where col value is 'yes' and its total count will be displayed at the grand total section.
    Hope this helps

  • How to identify the hidden columns in table by using af:panelCollection

    Hi,
    We are using af:panelCollection tag for a table to show personalization features. i.e. to hide or disaply colums in the table.
    We also have a print button to print data in the table. the issue is when we hide any column in teble using the personalization feature given by af:panelCollection , we can see the hidden columns also on the page to be printed.which should not happen.
    For printing table we cant use showPrintablePageBehavior coz it prints data on current page only where as we also have Print all data button which should print all data on table which is not visible on scrren due to pagination.
    Using ADF 11.1.1.5.0 and JSF 2.0 . Please help how to capture whci colums are made hidden.
    The code used for printing data is
    <div class="print_data">Print Current Data</div>
    <ahref="printCurrentData.jsp" target="_blank" class="left">
    <img src="print.png" alt="abc"
    width="24" height="22" border="0"/></a>
    <div class="print_data">Print All Data</div>
    <ahref="printAllData.jsp" target="_blank" class="left">
    <img src="print.png" alt="abc"/></a>
    Edited by: 924834 on Aug 27, 2012 4:27 AM
    Edited by: 924834 on Aug 27, 2012 4:28 AM

    There are some things which don't add up here.
    You are using jdev 11.1.1.5.0 which only comes with jsf 1.2, but you are using jsf 2.0?
    Then you are using jsp instead of jsf which are the normal way to go for jsf.
    Can you please clarify on this?
    How is the printCurrentData.jsp setup? Do you have a table on it based on the same iterator as on the visible page with the hidden column?
    Timo

  • How to make hidden columns shown on Fusion Audit Report.

    I have several questions about Fusion Audit Report here:
    1.  How to create Business Object Attributes, the seeded ones can't meet client's requirement.
    2. I can see from the audit report that, there are 10 columns hidden, how to show them? someone said to export the report, then hidden columns will show, I tried, it is not working for me.
    3. How to set date range as parameters.

    Hi,
    1. The creation of business object attributes depends on the customization options available to you. In summary it's really only Design Time Customization (JDeveloper) or Application Composer that permit business object attribute assition. Check out this document for details.
    https://docs.oracle.com/cd/E51367_01/fa_lcm_gs/OADTC/fa-cust-extend-intro.htm#OADTC157
    2. Do you mean in the Configure Business Object Attributes?  Does the View menu on the table not allow this?
    3. Use the Date dropdown and pick Between.
    In addition, please review the following resources related to using the Audit report.
    https://www.youtube.com/watch?v=bDzf99UvlT8&list=PL1ZiAfFIniZeMX-tZnKvxnosy-CUn-eT3&index=5
    https://blogs.oracle.com/FunctionalArchitecture/entry/auditing_in_fusion_applications
    Kind regards
    Richard
    FA Developer Relations at blogs.oracle.com/fadevrel

  • Creating hidden columns in tables?

    I want to create a table, with 2 hidden columns that I can enter names, tel#'s, when in edit mode. When it is published they won't show, however, when I go into edit - I can read the clients names. Possible?

    Hi Don,
    do you think about something like that?
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Two columns</title>
    </head>
    <body>
    <table width="900" border="0" cellspacing="5" cellpadding="5">
      <tr>
        <td colspan="2">Header text goes here.</td>
      </tr>
      <tr>
        <td width="650">Current news.</td>
        <td width="300">Yesterday's headlines.</td>
      </tr><tr>
        <td width="650">Current news text goes here.</td>
        <td width="300">Yesterday's headlines text goes here.</td>
      </tr>
    </table>
    <p> </p>
    <p> </p>
    </body>
    </html>
    Hans-Günter

  • Hidden column expecting metadata

    When I try to save a doc to a specific doc library Word says that metadata is missing or invalid but according to the DIP nothing is missing. So I go into the content type and unhide a couple of columns (setting them to optional) just to see if they are
    causing an issue.
    When I go to the DIP now, one of the previously hidden columns is showing as required.
    How can that be? How can a hidden column be expecting a value to be supplied. This column may have been required at some stage but has since been set to hidden. Surely this action would take away the requirement for metadata for that column?
    There doesn't seem to be an option (under edit column) to remove the requirement which convinces me that this may be a bug?

    Hi,
    Thank you for sharing this with us, and it will help others who have met with this issue.
    Best regards,
    Victoria
    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact
    [email protected]

  • Problem in updating first column header in JTable

    hi
    I am using JTable in my application. I want to change first column name of JTable every time when a JList is selected. I am using DefaultTableModel for JTable. I added first column in Constructor and another in a method. I have to edit only first column header. How can i do this???
    I used this code but it is not working.
    table.getColumnModel().getColumn(0).setHeaderValue("Name of column");
    table.getTableHeader().resizeAndRepaint();
    Your help will be appreciated.
    Thanks in advance
    Sonal

    table.getColumnModel().getColumn(0).setHeaderValue("Name of column");
    table.getTableHeader().repaint();Works for me so post a SSCCE ( http://www.sscce.org/ ) that shows the problem.

  • Multi-Row Hidden Column Submit Processing

    Hi,
    My attempts to select a hidden column value for use in Submit processing only works when I actually display the column in the report region. I want to store the column value in the array (htmldb_item.?????) without displaying it in the report region.
    I have tried using the following htmldb_item functions: hidden, display_and_save, text. When I use the hidden function and set the column attribute to "Show" then the heading displays, no column data displays, and the htmldb_application.g_f01 value is present for use in Submit processing. When I set various column attributes to cause the column to not display then the column value is not present during Submit processing.
    My code snippets for Region Select and Submit processing are:
    "select htmldb_item.hidden(1, sac.student_acad_cred_id) q_acad_cred_id,..."
    "BEGIN
    for i in 1..htmldb_application.g_f01.count
    loop..."
    Couldn't find answer with Forum search or in the Guide. Thanks for any help!

    Bernhard,
    There are two ways to build tabular forms (multi-row update forms). You can either use calls to the htmldb_item API in your query. Or you can use the built-in display types. The build-in types are generally the better option for a number of reasons, for example, the built-in form elements are only rendered for the rows you actually show on your current page. If you use the htmldb_item API and pagination with several pages, then you would make calls to PL/SQL for every row in your report, no matter whether they are shown or not.
    With the built-in display types you can choose the display type “Hidden”, which will actually append the hidden fields to the last column in your report, so the array is there, but you won’t have to deal with column headings and an empty column.
    When using the htmldb_item API, you should simply append the hidden column to a displayed column, e.g. select htmldb_item.text(…)||htmldb_item.hidden(…) [column alias], … from …
    Hope this helps
    Marc

Maybe you are looking for