简体   繁体   English

从Jtable移除一列并重新排列

[英]Remove a column from Jtable and rearrange it

I have JComboBox to select Designation. 我有JComboBox选择指定。 and I have JTable it's columns are EmpId,Name,Status,Start Time,End Time. 我有JTable,它的列是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. 当我选择名称“职员”时,JTable应该只显示EmpId,名称,状态。如果我选​​择名称“劳工”,则JTable应该出现与EmpId,名称,状态,开始时间,结束时间列。 I did thi but it gives a error, 我做了这件事,但它给出了一个错误,

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

Then only EmpId,Name,Status,End Time appears and It gave arrayoutofboundsexception 然后仅出现EmpId,Name,Status,End Time,并且给出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 并且我还需要再次选择那些列(3和4(开始时间,结束时间列))以在选择名称“文员”时显示,因此我使用了此代码,但未提供所需的输出

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 从数组中间删除对象后,它将删除呈现此列的TableColumn ,因此保存所有列的当前数组将为“ PRIOR size -1”,因此右侧的数组(最后一个)将移至在左侧,您的“结束时间”位置现在位于“ 3”,为避免此问题,甚至不要考虑删除两次第3列,这很不专业而且比较混乱,只需从数组末尾开始删除即可,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... 首先,我之前从未使用过JTABLE,几乎可以肯定的是,我所做的一切使所有JAVA社区陷入了尴尬境地……

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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