Is there a way to find out how to get the index of a column?

Hello,
I want to know the index of a column for example the column at the first position in a jtable is 0 so how can i get this index?
for example: the column called "surname" is index 0 but the user can move the column. So the name stays the same but the index of the column changed. I search for a method like
table.getColumnIndex("surname");

I think it really depends on what the OP is trying to do. I think the program
below offers up the possible choices pretty well.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class TableColumnIndexDemo extends JFrame {
     private JTable table;
     private String[] names = new String[]{ "One", "Two", "Three", "Four", "Five" };
     public static void main( String args[] ) {
          SwingUtilities.invokeLater( new Runnable() {
               public void run() { new TableColumnIndexDemo(); }
     public TableColumnIndexDemo() {
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          DefaultTableModel model = new DefaultTableModel( names, 1 );
          table = new JTable( model );
          JButton button = new JButton( "Print indices" );
          button.addActionListener( new ActionListener() {
               public void actionPerformed( ActionEvent e ) {
                    printIndices();
          getContentPane().setLayout( new BorderLayout() );
          getContentPane().add( button, BorderLayout.NORTH );
          getContentPane().add( new JScrollPane( table ), BorderLayout.CENTER );
          getContentPane().add( new JLabel( "Move columns around then print indices" ), BorderLayout.SOUTH );
          setSize( 300, 150 );
          setVisible( true );
          printIndices();
     private void printIndices() {
          TableColumnModel columnModel = table.getColumnModel();
          TableModel model = table.getModel();
          StringBuffer sb = new StringBuffer();
          System.out.println( "Visible order");
          for ( int k = 0; k < columnModel.getColumnCount(); k++ ) {
               if ( k > 0 ) sb.append( ", " );
               sb.append( k ).append( " " ).append( columnModel.getColumn( k ).getIdentifier() );
          System.out.println( sb );
          sb = new StringBuffer();
          System.out.println( "By name using getColumnIndex(TableModel,name)" );
          for( int k = 0; k < names.length; k++) {
               if ( k > 0 ) sb.append( ", " );
               sb.append( getColumnIndex( model, names[k] ) ).append( " " ).append( names[k] );
          System.out.println( sb );
          sb = new StringBuffer();
          System.out.println( "By name using columnModel.getColumnIndex(name)" );
          for( int k = 0; k < names.length; k++) {
               if ( k > 0 ) sb.append( ", " );
               sb.append( columnModel.getColumnIndex( names[k] ) ).append( " " ).append( names[k] );
          System.out.println( sb );
          sb = new StringBuffer();
          System.out.println( "Model index by visible order");
          for ( int k = 0; k < columnModel.getColumnCount(); k++ ) {
               if ( k > 0 ) sb.append( ", " );
               TableColumn column = columnModel.getColumn( k );
               sb.append( column.getModelIndex() ).append( " " ).append( column.getIdentifier() );
          System.out.println( sb );
     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;
}

Similar Messages

  • Is there a way of finding out how many active hours you've worked on a Premiere project?

    If not, it'd be a very cool addition for Adobe to implement... so you know exactly how many hours a project is taking (and how much to charge clients etc.) Obviously we're all prone to leaving Premiere on and wandering away, so I guess it would need to be able to differentiate between active and non-active time...

    Is there a way of finding out how many active hours you've worked on a Premiere project?
    There are two things a client get's billed for, my time, and my computer's time.  If either one of us are working on a project, the client get's billed.  Even if Adobe spent (wasted?) time on adding such a feature, they'd never be able to include any time I've spent working on the project with PP closed, so this feature would have very, very, VERY limited usefulness.
    The method suggested by the images is still the best way.

  • Is there a way to find out how much data was used over the lifetime of my phone (cumulative cellular and wifi)?

    Is there a way to find out how much data was used over the lifetime of my phone (cumulative cellular and wifi)?
    I was just curious

    Given that the phone doesn't track how much data is used over WiFi, no. And cellular data usage can be reset.

  • Is there a way to find out how much storage I have used on my iphone4s?

    I am new to icloud and want to back up my phone but it is saying I have too much data to back up to the free 5.0GB.  I can purchase more but note sure how much I will need.  Is there a way to find out how much storage my phone is currently using?

    Go to Settings - General - Usage, and you will see a breakdown of all your apps along with how much space they are using.
    My guess is that you have a lot of pictures that should probably be exported to your PC/Mac to free up your space.   5GB is a lot for one phone, you should be able to get by without buying more storage if you do a little housecleaning first.

  • Is there a way to find out how long my child has been playing with my ipad

    Is there a way to find out how long my child has been playing with my ipad???  He has been getting up before me in the morning and in the evening he is super tired and I'm wondering how early he has been getting up????

    No way to see exactly how long the iPad has been used but you may want to consider using parental controls.
    iOS: Understanding Restrictions (parental controls)

  • HT4597 Someone reset my iPhone and it was not me is there a way to find out how they were able to access my info?

    Someone reset my iPhone and it was not me is there a way to find out how they were able to access my info?

    There are two ways to reset an iPhone, 1)  the user actually using the iphone can perform a reset from Settings, and 2) a user to knows the iphone's iCloud ID and password and do a remote reset. 
    I'm assuming you didn't personally reset it while on the iPhone, so my question is ...  does anyone else know your iCloud ID and password?  Have you set up multiple devices (perhaps belonging to other members of your family - something you generally shouldn't do, if you don't want to sync the same data to all devices in the family) and used the same iCloud ID on all of them?  Perhaps a family member or a "friend" of a member used Find My iPhone to locate yours and remote wiped it.

  • Is there a way to find out how many ESS driven updates to data over a perio

    Is there a way to find out how many ESS driven updates to data over a period of time have been done.

    Assuming that all end users are accessing the back end via Portal, we figured out the answer.

  • Is there a way to find out which project using the record Z_student by sql

    Is there a way to find out which project using the record Z_student by sql
    use Find in in Application Designer is time consumer

    Try following SQL
    with all credits to the original poster of the greater portion of this sql
    http://srini-peoplesoft.blogspot.co.uk/2012/05/find-in-text-sql.html
    select projectname  from psprojectitem
    where (objectvalue1,objectvalue2,objectvalue3) in
    With Srch As
    Select Str, SubStr(Max(HexStr),3) Srch_Str
    From
    Select RNum, Str, Hexcode, Sys_connect_by_path(HexCode, '00') HexStr
    From (
    Select RNum, Str, Chr(Floor(Mod(Code/16, 16))
               + Decode(Floor(Floor(Mod(Code/16, 16))/10), 0, 48, 55)) ||
           Chr(Mod(Code, 16)
               + Decode(Floor(Mod(Code, 16)/10), 0, 48, 55)) HexCode
    From (        
    Select RNum, Str, Ascii(Substr(Str, RNum, 1)) Code
      From (Select Rownum RNum From Dual Connect By Level <= Length(:Str)),
           (Select :Str Str From Dual
            Union
            Select Upper(:Str) From Dual
            Union
            Select Lower(:Str) From Dual
            Union
            Select InitCap(:Str) From Dual
    Start With RNum = 1 Connect By Prior RNum = RNum - 1 And Prior Str = Str
    Group By Str
    select
    objectvalue1,objectvalue2,objectvalue3
    from pspcmprog p, srch
    where dbms_lob.instr(progtxt, srch_str)  > 0
    );

  • Trying to find out how to get the smallest value in a 2-D array

    I figured out how to get the largest and smallest in a 1-D array, but I'm having trouble with the for loop for the 2-D array
    I can't seem to figure out how I can get it to check every element in the 2-D array
    Lets say I have this array
    int[][] aRay = new int[][] {{ 1, 2, 3} , {4, 5, 6 }};and I want to find the smallest value, I think I can figure out what goes in the box of the for loop I just need help with how I can check every element..
    Thanks all

    Ok so here's what i've got so far and my brain is starting to hurt,
    My output right now is "2".. i'm almost there :/ aren't I?
    public class bladddhd
         public static void main( String [] args )
         int[][] aRay = new int[][] {{ 1, 2, 3} , {4, 5, 6 }};
              int length2 = aRay[0].length;
              int temp = 0;
              for(int i = 0; i < aRay.length; ++ i)
                        if(aRay[0] > temp)
                             temp = aRay[0][i];
                        for(int j = 0; j < length2; ++j)
                                  if(aRay[1][j] < temp)
                                       temp = aRay[1][j];
                   System.out.println(temp);

  • Is there a way to find out how much I have spent on apps for the iPad?

    I need to find out how much I have spent on apps for taxes.  But in the app store on my iPad under the purchased tab, it lists all the apps but the prices are not available.  Any suggestions?

    Hi...
    If you can access your iTunes account on a computer you can sign in then view your Purchase History.
    And check your bank statements.

  • How to get the index of a Column

    Hello,
    I want to know the index of a column for example the column at the first position in a jtable is 0 so how can i get this index?
    for example: the column called "surname" is index 0 but the user can move the column. So the name stays the same but the index of the column changed. I search for a method like
    table.getColumnIndex("surname");

    So why do you post questions on multiple forums.
    Like I said you are wasting peoples time. We don't know what has been suggested on the other forums. So by answering the question here we are wasting our time because you also probably got the answer on the other forum as well.

  • I lost my Download Stacks option. Now it is just a Download folder. Need to find out how to get the Download Stacks option back. Previous answers to this question don't make any sense to me. Please help in explicit detail. PS I'm using Mountain Lion.

    Need to know how to get it back to its original Download Stack. Previous answers to this questionin Apple Support just don't make any sense to me. I'd appreciate explicit directions on how to restore this feature. Currently it's just a Download folder... must have accidently poofed it away.
    Help!

    And if what WZZZ suggests doesn't work (and hopefully it will) move the file name below to the desktop, log out or restart and the Dock preferences with its defaults should be recreated. You'll need to re-configure it as you like it:
    com.apple.dock.plist
    It's located: in Finder hold the Alt/Option key and from the Go menu select Library>Preferences.
    If it fixes things, delete the file on your desktop.

  • How to get the index of Mult column list box

    Hello all,
    I need small help. Please help me.
    Is there is any property to change the "Multi column list box" row index position dynamically?
    I want to change the index position at runtime like below.
    Munna
    Solved!
    Go to Solution.

    It's called Top Left Visible Cell. There aren't that many properties there.
    Try to take over the world!

  • Is there a way to find out how many steps you walked on a specific day in the Apple Health App.  I don't want to know minute by minute.  But want to know if I can go back a week to a certain day.

    LF way to see how many steps I walked on a certain day last week.  Not minute by minute, just recall the day's steps.  All I get is a graph and then all data when I tap a particular day,  which is far too detailed and useless.

    here is a link to macosxhints.com that tells you how to output all of your feed URLs to a terminal window. from there you can cut and paste them into your new feed reader of choice...
    http://www.macosxhints.com/article.php?story=20080319094830396
    and if you are in the marked for a new reader may i suggest David Watanabe's excellent NewsFire. he had just recently made it totally free as a gift to the macintosh community.
    best of luck.

  • Is there any way to find out how much battery life you have on you're Bluetooth Magic Mouse and Apple Keyboard on your Windows Partion?

    I'm a avid gamer and for my gaming needs most of the games that I love require Windows ,but due to my constant clicking I drain my mouse battery very fast so I always have backups. Is there anyway I can check how much mouse battery I have left so I can be sure before I start a gaming session that I'll be fine? Switching back and forth between partions is an option ,but I find it as more of a last resort then something to do normally.
    Thanks in advance!

    call them, tell them you NEED a new battery installed, and they need to tell you when they can do it.  That is their job to help you! Give pushback if necessary.

Maybe you are looking for

  • Adobe 9 installation error

    I am running windows 7 and having installation problems with Adobe 9..It comes up with and error box and at the top of the box it says "get+(r) ;error

  • Dim display hp chromebook 14-C001SA

    hi, ive got a issue with a dim display on my hp 14 chromebook  14-C001SA, the output to my tv via hdmi is fine,ive changed the screen and the ram and still the same issue, any ideas im so confused to what the problem is, any help would be much apprec

  • POLICY-MAP counters

    I have configured policy-maps and class-maps on 3550 and 3560 switches. The following is excerpt.... class-map match-any voip_class match access-group 100 policy-map voip_policy class voip_class trust dscp interface GigabitEthernet0/12 service-policy

  • Font looks weird in my browser

    When i search something or anything in the search history has a weird looking font? any suggestions? i have tried disabling the hardware acceleration, and I have tried changing the font in the firefox settings... I am not sure what to do please help!

  • Reg Infoset query

    Hi, What is infoset query in generic extraction? Why and when do we go for it? What is the advantage of it? Any help would be appreciated the sdn way. Regards, Jon.