简体   繁体   中英

Called this method in the constructor, but nothing happens?

I wanna call this method in the constructor so my program can adjust its text fields according to the sizes and positions of jTable. but when I call this method in the constructor , nothing happens. can someone tell me how to make this work when the program starts?

also I wanna call this method whenever user try to resize the jTable's columns. How can I do that?

private void setTextFields() {
    try {
        cpono.setSize(grntable.getColumnModel().getColumn(0).getWidth(), cpono.getSize().height);
        cicode.setSize(grntable.getColumnModel().getColumn(0).getWidth(), cicode.getSize().height);
        cdesc.setSize(grntable.getColumnModel().getColumn(0).getWidth(), cdesc.getSize().height);
        cum.setSize(grntable.getColumnModel().getColumn(0).getWidth(), cum.getSize().height);
        ccost.setSize(grntable.getColumnModel().getColumn(0).getWidth(), ccost.getSize().height);
        crqty.setSize(grntable.getColumnModel().getColumn(0).getWidth(), crqty.getSize().height);
        cdisrate.setSize(grntable.getColumnModel().getColumn(0).getWidth(), cdisrate.getSize().height);
        csubtot.setSize(grntable.getColumnModel().getColumn(0).getWidth(), csubtot.getSize().height);
        cdis.setSize(grntable.getColumnModel().getColumn(0).getWidth(), cdis.getSize().height);
        ctot.setSize(grntable.getColumnModel().getColumn(0).getWidth(), ctot.getSize().height);
        ccomments.setSize(grntable.getColumnModel().getColumn(0).getWidth(), ccomments.getSize().height);

        cicode.setLocation((int) (cpono.getLocation().getX() + cpono.getSize().getWidth()), (int) cicode.getLocation().getY());
        cdesc.setLocation((int) (cicode.getLocation().getX() + cicode.getSize().getWidth()), (int) cicode.getLocation().getY());
        cum.setLocation((int) (cdesc.getLocation().getX() + cdesc.getSize().getWidth()), (int) cum.getLocation().getY());
        ccost.setLocation((int) (cum.getLocation().getX() + cum.getSize().getWidth()), (int) ccost.getLocation().getY());
        crqty.setLocation((int) (ccost.getLocation().getX() + ccost.getSize().getWidth()), (int) crqty.getLocation().getY());
        cdisrate.setLocation((int) (crqty.getLocation().getX() + crqty.getSize().getWidth()), (int) cdisrate.getLocation().getY());
        csubtot.setLocation((int) (cdisrate.getLocation().getX() + cdisrate.getSize().getWidth()), (int) csubtot.getLocation().getY());
        cdis.setLocation((int) (csubtot.getLocation().getX() + csubtot.getSize().getWidth()), (int) cdis.getLocation().getY());
        ctot.setLocation((int) (cdis.getLocation().getX() + cdis.getSize().getWidth()), (int) ctot.getLocation().getY());
        ccomments.setLocation((int) (ctot.getLocation().getX() + ctot.getSize().getWidth()), (int) ccomments.getLocation().getY());
    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, e, "Error", 0);
        return;
    }
}

to call this method whenever your table's columns resize, first of all add the ColumnModeListener to your JTable (say jtable) :

jtable.getColumnModel().addColumnModelListener()

then call your desired method (setTextFields) in the columnMarginChanged() method. take a look at this page and for the first part of your question please show what's before the snippet you've shown here?

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