Hiding and showing columns in JTable

Hi
When I use removeColumn on a JTable I see that
it also disapears from the tablecolumnmodel. (I thought that the jtable was the view and the data would remain in the tablecolumnmodel)
So how do I show the column again ?
//I used the following
TableColumnModel colModel = table.getColumnModel();
TableColumn tc = table.getColumn("header one");
table.removeColumn(tc);
Thanks
Tim

You are misreading it (I believe). The column is removed, but the data still exists in the model. The information about the column itself is lost.
What I did, when columns are removed (I call it hiding), I remove them from the table and keep them in an arraylist. When I want to bring them back, I find it in the arraylist, add it back to the table and then move it to the correct place.

Similar Messages

  • Cfgrid hiding and showing columns dynamically

    I am trying to get my grid and some checkboxes to integrate
    with each other in order to show and hide different columns through
    actionscript. I am using the code found at this website:
    http://www.asfusion.com/blog/entry/mxna-reader-built-with-cfforms
    and I have been having no luck in trying to implement it. I
    would like to do away with the .as files that come along with this
    code but I can't get it to work with or without it. Does anybody
    know how to do this, I am at a loss and I need to get it working
    soon.
    Thanks in advance!
    Jeff
    Here's the code I have been working with:
    <cfformitem type="script">
    <!---
    function _global.hideColumns()
    _global.postColumns = [];
    for(var i =0; i< contactList2.columnCount; i++)
    _global.postColumns.push(posts.getColumnAt(i));
    contactList2.removeColumnAt(6);
    contactList2.removeColumnAt(5);
    contactList2.removeColumnAt(4);
    contactList2.removeColumnAt(3);
    function _global.setColumn(target, col)
    if(target.value)
    contactList2.addColumn(_global.postColumns[col.index]);
    contactList2.spaceColumnsEqually();
    contactList2.getColumnAt(0).width = 0;
    else
    for(var i = 0; i< contactList2.columnCount; i ++)
    if(contactList2.getColumnAt(i).columnName == col.data)
    contactList2.removeColumnAt(i);
    </cfformitem>
    <cfform action="#cgi.script_name#" method="post"
    name="form" preservedata="true" preloader="yes" format="flash"
    height="605" width="765" skin="haloblue"
    style="themeColor:##56A1E1; marginRight:0;
    background-color:##7B8DA1;">
    <cfgrid name="contactList2" query="GetActivity"
    rowheaders="false" height="365" colheaderalign="center"
    colheaderfontsize="12" colheaderbold="yes"
    onchange="getUrl('act_view.cfm?ActivityID=' +
    contactList2.dataProvider[contactList2.selectedIndex]['ActivityID']);">
    <cfgridcolumn name="ActivityID" display="no">
    <cfgridcolumn name="Title" header="Activity Title"
    width="150">
    <cfgridcolumn name="Details" header="Details">
    <cfgridcolumn name="SiteArea" header="Site Area"
    width="70">
    <cfgridcolumn name="Mission" header="Mission"
    width="70">
    <cfgridcolumn name="Date" header="Activity Date"
    mask="mm/dd/yyyy" width="95">
    </cfgrid>
    <cfformgroup type="horizontal" height="14">
    <cfinput type="text" name="columns" visible="no"
    label="Check here to show columns:" width="0">
    <cfinput type="checkbox" name="chktitle" checked="true"
    label="Title" onClick="_global.setColumn(chktitle, {index:1,
    data:'title'})">
    <cfinput type="checkbox" name="chkdetails" checked="true"
    label="Details" onClick="_global.setColumn(chkdetails, {index:2,
    data:'details'})">
    <cfinput type="checkbox" name="chkSite" checked="false"
    label="SiteArea" onClick="_global.setColumn(chkSite, {index:3,
    data:'siteArea'})">
    <cfinput type="checkbox" name="chkMission"
    checked="false" label="Mission"
    onClick="_global.setColumn(chkMission, {index:4,
    data:'mission'})">
    <cfinput type="checkbox" name="chkDate" checked="true"
    label="Date" onClick="_global.setColumn(chkDate, {index:5,
    data:'Date'})">
    </cfformgroup>
    </cfform>

    Hmm. Perhaps the problem is that when these columns are hidden or shown, the data is cleared out so there is nothing visible in any column.  Still, shouldn't the width stay static according to the width I specified for each column?  It seems like a bug, really, but I'm not sure why it seems uncommon.
    Unfortunately an upgrade to Flex 3 isn't possible at the moment, I don't think.

  • Data grid within advanced data grid (Hiding and showing columns)

    Hi,
    I have a dta grid within advanced datagrid. When the row of advanced data grid is expanded, the datagrid opens up.
    I want to control the columns of datagrid based on the value of a field in advanced data grid. I want to show all the columns of the datagrid if the field value of advanced data grid is "X" else I want to show only few columns of the data grid.
    I will set the id for all the columns of the datagrid. Then I think I can write some code in a method to set the visibility of the columns and assign the function to item opening property of the advanced data grid.
    Please give me some ideas.
    Regards,
    Mahee   

    Hi,
    Please go through following links:
    http://www.cflex.net/showFileDetails.cfm?ObjectID=553
    http://flexmusings.wordpress.com/2009/08/03/hide-row-column-datagrid/
    http://www.java2s.com/Code/Flex/Grid/HidinganddisplayingDataGridcolumns.htm
    Thanks and Regards,
    Vibhuti Gosavi | [email protected] | www.infocepts.com

  • 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();

  • Hiding and showing Flash Player causes a reload

    I have a Flex application embedded in an IFRAME that is
    inside a DIV.
    If I hide the DIV and then make it visible, the application
    reinitializes itself. Is there a way to keep this from
    happening?

    Instead of showing/hiding the DIV, you could try to change
    its size to 1px height and 1px width.
    I guess it's not a real good way to do it, but it may works
    :)

  • Hide and show columns

    Hi Guys,
    I'd like to control some columns show or hide by a option input from user interface. I can assigne the user's input to a parameter and use that parameter to suppress sections which contains to-be-hidden columns. I use section's underly merge them together when those columns needs shown. but when I hide columns, there are some empty space occupied by the hidden fields, how can I overcome it?
    Thank  you very much
    Clara

    There are THREE very recent threads where the poster is looking to do similar things, [here|Fetching Columns in For Loop; , [here|End User Customisation at Runtime; and another I can quite put my finger on.  Interestingly, each give different solutions to the problem.
    Please search the forum prior to posting your question.
    HTH,
    Carl

  • Trouble hiding and showing captions with a single button

    I am trying to show a caption (MSO) with the click of a button and then hide it again with the same button. I have created a MSO with two states one with the text, one without and then created an icon with a plus sign [normal] that triggers the second state that has the text. After the button is pressed it changes to a minus sign [click] the next step is where I am running into trouble. I just want the user to be able to click the minus button to hide the caption before moving on to the next page or article. I have tried every button state combination but can't hide the caption short of creating a new button somewhere else on the page, this is not desired. I have seen this function in publications like National Geographic, and Vogue so I know it is possible. It seems so simple but I can't figure it out. Being able to close the caption after showing it will add another level of interaction and professionalism to my publication...please help someone.

    Being inmersed in this thing, I am gathering some instructions and notes about very basic DPS things for our working team use and my Spanish speakers readers. Here you have the first one: Cómo hacer que un pie de foto se muestre al pulsar encima de la imagen en InDesign para DPS
    They are based in Bob Bringhusrt's DPS tips —thanks a lot!—, what I have read in this forum and the Adobe's help data.
    There's a translate button that works quite fine.
    Is that of any help? Hope so
    Gustavo (Posting from heat-melting Madrid)

  • Hiding and showing borders on tables nested inside other tables

    I'm using this code:
    <?CHOOSE:?><?WHEN:position() > 1 ?><?attribute@incontext:border-top-color;'black'?>
    <?attribute@incontext:border-top-style;'solid'?>
    <?attribute@incontext:border-top;'0.25pt'?>
    <?end when?>
    <?end choose?>
    I want the top border to apply to the entire row, not just the cell. This is probably easy syntax but it isn't accepting anything i throw at it.
    Edited by: CISguy on Feb 23, 2010 4:32 PM
    Edited by: CISguy on Feb 24, 2010 10:49 AM

    I got it to work, but I think the PDF output is just buggy and code only works when it wants to. If anyone else is trying to use a table inside a table and is facing the "unexpected thick table border" issue, this is because of the table borders stacking on top of each other. I got it to work by disabling almost all borders on the inner table and using this code to show a border when i wanted it to.
    <?if@row:position() >1?>
    <?attribute@incontext:border-top-color;'black'?>
    <?attribute@incontext:border-top-style;'solid'?>
    <?attribute@incontext:border-top;'0.25pt'?>
    <?end if?>

  • Hideing and showing a page or security for some pages

    i have developed an applciation where in which i want users to have acces
    to some pages and some pages are secured they can be viewed only after entering some credentials.
    how can we do this some body help me

    Create an authentication scheme and an authorization scheme (under Shared components)
    and in the page attributes (top left) you can select these schemes to be active for the page.
    Hope this helps.
    Leo

  • Hiding and restoring table columns from file?

    how i handle hiding and unhiding columns in a jtable is that i have a checkbox for each column, depending on which is checked "on" the tablecolumn is found and added to a vector and removed from the table.
    when the checkbox is checked "off" this vector is searched for the correct tablecolumn, and then added back to the table. this works fine.
    when i close out of the program, i go thru the table and write the visible column names along with its size to file.
    when i re-open the program, i create the table normally, then go thru the file and hide(remove) the tablecolumns found in it..this works fine.
    but when i go back to this checkbox list and try to unhide the tablecolumn, i get an exception
    java.lang.IllegalArgumentException: Cannot format given Object as a Number
         at java.text.NumberFormat.format(NumberFormat.java:219)
         at java.text.Format.format(Format.java:133)
         at javax.swing.JTable$DoubleRenderer.setValue(JTable.java:3397)
         at javax.swing.table.DefaultTableCellRenderer.getTableCellRendererComponent(DefaultTableCellRenderer.java:160)
         at javax.swing.JTable.prepareRenderer(JTable.java:3731)
         at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:1149)
         at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:1051)
         at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:974)
         at javax.swing.plaf.ComponentUI.update(ComponentUI.java:142)
         at javax.swing.JComponent.paintComponent(JComponent.java:541)
         at javax.swing.JComponent.paint(JComponent.java:808)
         at javax.swing.JComponent.paintChildren(JComponent.java:647)
         at javax.swing.JComponent.paint(JComponent.java:817)
         at javax.swing.JViewport.paint(JViewport.java:722)
         at javax.swing.JComponent.paintChildren(JComponent.java:647)
         at javax.swing.JComponent.paint(JComponent.java:817)
         at javax.swing.JComponent.paintWithOffscreenBuffer(JComponent.java:4787)
         at javax.swing.JComponent.paintDoubleBuffered(JComponent.java:4740)
         at javax.swing.JComponent._paintImmediately(JComponent.java:4685)
         at javax.swing.JComponent.paintImmediately(JComponent.java:4488)
         at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:410)
         at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:117)
         at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:178)
         at java.awt.EventQueue.dispatchEvent(EventQueue.java:454)
         at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
         at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
         at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
         at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
         at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)before i unhide the columns, the table is already populated with values. so it might have something to do with that...cuz there is no problems unhiding them if the table contains no data
    any ideas?
    thanks

    If you're interested, this method hides a table column without any modifications of its content:
       public static void setTableColumnVisible(JTable table, int col,                                                                             
                                                  boolean visible, int width) {
          try {
             TableColumn column = table.getColumnModel().getColumn(col);
             if (!visible) {
                column.setMinWidth(0);
                column.setWidth(0);
                column.setMaxWidth(0);
                column.setPreferredWidth(0);
             else {
                column.setWidth(width);
                column.setPreferredWidth(width);
          catch(Exception e) {
             e.printStackTrace();
       }Method use:
    . setTableColumnVisible(table, col, false, 0) hides the column
    . setTableColumnVisible(table, col, true, 100) restores the column to its size of 100
    Hope this helped,
    Regards.

  • Hide and show tab canvas

    Is it possible to disable all the tabs in a tab canvas in Forms 10g?
    I am developing a new form in Forms 10g. Problem is with hiding and showing tab canvases.
    I have a main canvas and a tab canvas.
    In the when-new-form-instance I need to hide the two tabs. I have a list item that has two values. Based on the value chosen in the list I need to show the tab canvas. How can I do it. I tried hide_view and show_view. It gives me an error FRM-41053. Cannot find canvas Invalid ID.
    Help is appreciated. Thaks in advance
    Anu

    If you are looking to hide individual tabs on a tabbed canvas, refer to SET_TAB_PAGE_PROPERTY
    http://www.oracle.com/webapps/online-help/forms/10g/topics/f1_help/builts/f50settppr.html

  • HIDE/SHow Column Saving preferences

    Hello All,
    I am trying to have a report that allows users to hide and show columns.
    I have looked at the example on this link
    http://htmldb.oracle.com/pls/otn/f?p=31517:78:1596122781426415::NO:::
    My only issue is:
    How do you save the preferences, as I cannot seem to see how its done on the link
    Frank

    Hi,
    Recently I met the same question and the solution is , give column level security in presentation layer by
    * Click the column
    * In general tab permission tab is there, give access to specific users and save.
    Now when you login with the user and see the column, it will throw error. In order to avoid that
    Change the following property in security section of nqsconfig file
    * PROJECT_INACCESSIBLE_COLUMN_AS_NULL     =     yes;
    It works.
    Regards,
    Anitha.B

  • JTable -showing column headers and displaying multi-line strings

    Hi,
    This is two questions really.
    #1 - Does anyone know why my column headers aren't showing in my jtable using the model below?
    #2 - Does anyone know how I can display, mulitple line strings in a jtable? Currently my newline character ('\n') is just being displayed as a character.
    any help very much appreciated,
    Tom
    private class TaskHistoryTableModel extends AbstractTableModel {
    private List taskHistory = new ArrayList();
    public Object getValueAt(int row, int col) {
    if (col == 0) {
    return ((TaskHistoryItem) taskHistory.get(row)).getText();
    } else {
    return ((TaskHistoryItem) taskHistory.get(row)).getDate().getTime();
    public int getRowCount() {
    return taskHistory == null ? 0 : taskHistory.size();
    public int getColumnCount() {
    return 2;
    /** Getter for property taskHistory.
    * @return Value of property taskHistory.
    public List getTaskHistory() {
    return taskHistory;
    /** Setter for property taskHistory.
    * @param taskHistory New value of property taskHistory.
    public void setTaskHistory(List taskHistory) {
    this.taskHistory = taskHistory;
    public String getColumnName (int col) {
    return col == 0 ? "Text" : "Entered At";
    public boolean isCellEditable(int row, int col) {
    return false;
    }

    fixed my own problem - make your mulit-line string into html format e.g.
    this string will appear on two lines in a jtable
    "<html><p>A much more interesting entry</p> <p>on multiple lines</p></html>"

  • Hiding a column in JTable

    Hi,
    I want to hide a column when i select Hide option from a popup menu and
    again bring it back when i select show option again from the popup menu.
    How to get this done?

    Hi!
    The simplest way to do that, is to set the column width to 0 for hiding and back to its normal size to show it again.
    Cheers
    K.

  • Show and hide columns.

    I have a rather large JTable with a lot of columns that are updated once in a while.
    Is there an easy way to show and hide columns, so that I don't have to keep track on which a shown and hide when I use setValueAt()...?

    JTable does the conversion between the column indexes of the view and the model.
    You should be able to add remove columns using addColumn and removeColumn methods. and in the TableColumn class you can specify the coulmn in the model that the TableColumn refers to.

Maybe you are looking for

  • PeopleSoft Oracle Document Techonology disabled if using AIX?

    Does anyone have experience using the new Documents Technology_ in Peoplesoft while running AIX on the application and database side_? Looking through the delivered code for Consume Web Service component for instance I'm running across code specifica

  • HR related Query

    Dear All,      Why do we use LDB in HR module?      What is the difference between the infotypes and tables ?      Do we use LDB in other modules? Thanks, Santhiya.B

  • Question about Director Flash Components's TextInput

    Hello,everybody! I used the Flash Components's TextInput in  Director 11.5,but there was a problem that when the TextInput in edit status the cursor is missing. And the TextInput still can be edited !  I want  when the TextInput in edit status the cu

  • What to do when update for app stops loading

    I updated an app on my iPod touch and it stopped loading so none of my other apps can be updated? How do u cancel an update or fix this?

  • Bruit au démarrage d'un macbook pro

    Je viens d'acheter un macbook pro retina. Il y a un bruit au démarrage même si les haut parleurs sont coupés. Cela dure environ une seconde mais c'est assez fort.  C'est un disque ssd et il ne devrait pas faire de bruit. Est-ce normal?