简体   繁体   中英

Remove a column from Jtable and rearrange it

I have JComboBox to select Designation. and I have JTable it's columns are EmpId,Name,Status,Start Time,End Time. When I select designation "Clerk" JTable should appears with only EmpId,Name,Status If I select designation as "Labourer" JTable should appears with EmpId,Name,Status,Start Time,End Time columns. I did thi but it gives a error,

jTable1.removeColumn(jTable1.getColumnModel().getColumn(3)); jTable1.removeColumn(jTable1.getColumnModel().getColumn(4));

Then only EmpId,Name,Status,End Time appears and It gave arrayoutofboundsexception

what is the mistake here

and also I need to get those Columns(3 and 4(Start time, End time Columns)) again to display when I select designation "Clerk" so I used this code but it's not giving required output

String desig=cmbAtSelectDesig.getSelectedItem().toString();
if(desig.contentEquals("Clerk")){
 jTable3.addColumn(jTable3.getColumnModel().getColumn(3));
           jTable3.addColumn(jTable3.getColumnModel().getColumn(4));
}
   What is the mistake here Please Give me a solution        

once you remove object from the middle of array, it removes TableColumn that renders this column, thus current array that hold all columns will be "PRIOR size -1", so the one that is on the right (the last one) will move to the left, you "End Time" position is now at "3 ", to avoid this problem, don't even think about deleting twice 3rd column, it's unprofessional and messy, simply start deleting from the end of the array, 4 ----> 3

Updated:

first of all i never used JTABLE before, almost for sure what i'm doing is an embarrassment to all JAVA community, never the less...

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;


public class Tester {
    JFrame frane;
    JPanel mainP;
    JTable table;
    public void testIt() {
        frane=new JFrame("testing");
        populateGui();
        frane.setContentPane(mainP);
        frane.pack();
        frane.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frane.setVisible(true);
    }
    private void populateGui() {
        mainP=new JPanel();
        String[] colName={"first col","second","3rd coll"};
        String[][] information={
                    {"DUCK","QUACK","MacDuck"}
                };
        table=new JTable(information, colName);
        table.removeColumn(table.getColumnModel().getColumn(2));
        System.out.println(table.
                getColumnModel().
                    getColumnCount()
        );
        System.out.println(table.
                getModel().
                    getColumnCount()
        );
        table=new JTable(table.getModel());  // yet all the data object that were present in data array () "information" are still there
        /*table.addColumn(
                table.getColumnModel().          there is no column 2 since it was removed from ColumRendere (ColumnModel)
                    getColumn(2));*/
        mainP.add(table);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM