Hi!

I have a code that generates the table with 6 columns, and 6 textfields. Textfields are used to filter the table, and they are assigned to columns. Everything works normally, but looks badly. Attached you will find a picture to see what I mean. I don't like that textfields and table columns have different sizes. How could I make them looking more accurately? I was thinking about making a second table with 6 columns, 1 row and invisible header instead of 6 textfields. However, I'm not sure that such solution would be the optimal one. Any ideas?

This is the code:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.RowFilter;
import javax.swing.ScrollPaneConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableRowSorter;

public class FilterClass extends JPanel {
    TableRowSorter rowSorter;
    private DefaultTableModel tableModel;

    public FilterClass() {
        //setLayout(new BorderLayout());
        initComponents();
        rowSorter=new TableRowSorter();
        tableModel = (DefaultTableModel) tableDetails.getModel();
        rowSorter.setModel(tableModel);
        tableDetails.setRowSorter(rowSorter);
    }

    private void initComponents() {
        JPanel panfortable = new JPanel(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
	c.fill = GridBagConstraints.HORIZONTAL;

        tableDetails = new javax.swing.JTable();
        txtRegNum = new javax.swing.JTextField();
        txtFirstName = new javax.swing.JTextField();
        txtLastName = new javax.swing.JTextField();
        txtProfession = new javax.swing.JTextField();
        txtSex = new javax.swing.JTextField();
        txtDateofBirth = new javax.swing.JTextField();

        tableDetails.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {...},
                {...},
                {...}
            },
            new String [] {"A",
                          "B",
                          "C",
                          "D",
                          "E",
                          "F"}
        ));

        // Set column widths
        tableDetails.setFillsViewportHeight(true);
        //tableDetails.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        TableColumn col;
        int width = 100;
        col = tableDetails.getColumnModel().getColumn(3);
        col.setPreferredWidth(width);
        col = tableDetails.getColumnModel().getColumn(5);
        col.setPreferredWidth(width);
        jScrollPane1 = new javax.swing.JScrollPane(tableDetails, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        c.ipady = 0;
        c.weightx = 0.2;
        c.weighty = 0.0;
        //c.gridwidth = 1;
        //c.anchor = GridBagConstraints.CENTER;
        c.insets = new Insets(0,0,0,0);
        c.gridx = 0;
        c.gridy = 0;
        panfortable.add(txtRegNum, c);
        //c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.2;
        c.gridx = 1;
        c.gridy = 0;
        panfortable.add(txtFirstName, c);
        c.weightx = 0.2;
        c.gridx = 2;
        c.gridy = 0;
        panfortable.add(txtLastName, c);
        //c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.2;
        c.gridx = 3;
        c.gridy = 0;
        panfortable.add(txtProfession, c);
        //c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.2;
        c.gridx = 4;
        c.gridy = 0;
        panfortable.add(txtSex, c);
        //c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.2;
        c.gridx = 5;
        c.gridy = 0;
        panfortable.add(txtDateofBirth, c);
        //c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 50;      //make this component tall
        c.weightx = 0.0;
        c.gridwidth = 6;
        c.insets = new Insets(5,0,0,0);
        c.gridx = 0;
        c.gridy = 1;
        panfortable.add(jScrollPane1, c);
        add(panfortable);

        txtRegNum.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtRegNumKeyReleased(evt);
            }
        });

        txtFirstName.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtFirstNameKeyReleased(evt);
            }
        });

        txtLastName.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtLastNameKeyReleased(evt);
            }
        });

        txtProfession.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtProfessionKeyReleased(evt);
            }
        });

        txtSex.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtSexKeyReleased(evt);
            }
        });

        txtDateofBirth.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtDateofBirthKeyReleased(evt);
            }
        });
    }

    private void txtRegNumKeyReleased(java.awt.event.KeyEvent evt) {
        rowSorter.setRowFilter(RowFilter.regexFilter("(?i)^"+txtRegNum.getText(), 0));
    }

    private void txtFirstNameKeyReleased(java.awt.event.KeyEvent evt) {
        rowSorter.setRowFilter(RowFilter.regexFilter("(?i)^"+txtFirstName.getText(), 1));
    }

    private void txtLastNameKeyReleased(java.awt.event.KeyEvent evt) {
        rowSorter.setRowFilter(RowFilter.regexFilter("(?i)^" +txtLastName.getText(), 2));
    }

    private void txtProfessionKeyReleased(java.awt.event.KeyEvent evt) {
        rowSorter.setRowFilter(RowFilter.regexFilter("(?i)^"+txtProfession.getText(), 3));
    }

    private void txtSexKeyReleased(java.awt.event.KeyEvent evt) {
        rowSorter.setRowFilter(RowFilter.regexFilter("(?i)^" +txtSex.getText(), 4));
    }

    private void txtDateofBirthKeyReleased(java.awt.event.KeyEvent evt) {
        rowSorter.setRowFilter(RowFilter.regexFilter("(?i)^" +txtDateofBirth.getText(), 5));
    }

    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tableDetails;
    private javax.swing.JTextField txtRegNum;
    private javax.swing.JTextField txtFirstName;
    private javax.swing.JTextField txtLastName;
    private javax.swing.JTextField txtProfession;
    private javax.swing.JTextField txtSex;
    private javax.swing.JTextField txtDateofBirth;

}
Attachments tablewithfilters.jpg 29.08 KB

You could add a TableColumnModelListener something like this to resize your filter fields

tableDetails.getColumnModel().addColumnModelListener(new TableColumnModelListener() {

        public void columnMarginChanged(ChangeEvent e) {
            txtRegNum.setPreferredSize(new Dimension(tableDetails.getColumn("A").getWidth(),txtRegNum.getPreferredSize().height));
            txtFirstName.setPreferredSize(new Dimension(tableDetails.getColumn("B").getWidth(),txtFirstName.getPreferredSize().height));
            txtLastName.setPreferredSize(new Dimension(tableDetails.getColumn("C").getWidth(),txtLastName.getPreferredSize().height));
            panfortable.revalidate();
        }

        public void columnAdded(TableColumnModelEvent e) {}
        public void columnRemoved(TableColumnModelEvent e) {}
        public void columnMoved(TableColumnModelEvent e) {}
        public void columnSelectionChanged(ListSelectionEvent e) {}
    });

You'll have to set your weightx=0 for those text components you want to size yourself like that, or they won't respect your preferred size setting.

Using a Map or List to associate your text fields to their respective columns would alleviate a lot of redundant code as well.

Edited 6 Years Ago by Ezzaral: n/a

Hi! Thanks u both! I've tried TableColumnModelListener.by inserting the code just after "tableDetails.setFillsViewportHeight(true);". Unfortunately, I've got the following errors:

Exception in thread "main" java.lang.IllegalArgumentException: Identifier not found
        at javax.swing.table.DefaultTableColumnModel.getColumnIndex(DefaultTableColumnModel.java:265)
        at javax.swing.JTable.getColumn(JTable.java:2529)
        at filetree.FilterClass$1.columnMarginChanged(FilterClass.java:76)
        at javax.swing.table.DefaultTableColumnModel.fireColumnMarginChanged(DefaultTableColumnModel.java:611)
        at javax.swing.table.DefaultTableColumnModel.propertyChange(DefaultTableColumnModel.java:675)
        at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:339)
        at javax.swing.event.SwingPropertyChangeSupport.firePropertyChange(SwingPropertyChangeSupport.java:75)
        at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:276)
        at javax.swing.table.TableColumn.firePropertyChange(TableColumn.java:261)
        at javax.swing.table.TableColumn.firePropertyChange(TableColumn.java:267)
        at javax.swing.table.TableColumn.setPreferredWidth(TableColumn.java:531)
        at filetree.FilterClass.initComponents(FilterClass.java:90)
        at filetree.FilterClass.<init>(FilterClass.java:35)
        at filetree.Form.createBaseRight(Form.java:122)
        at filetree.Form.createGUIforAdmin(Form.java:57)
        at filetree.Form.<init>(Form.java:45)
        at filetree.Form.main(Form.java:130)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)

I can't understand what are the possible causes of errors. Could u help pliz?

You've used an invalid identifier to get the column. I used the column name in the code above, since the identifier defaults to the header value.

In your own code you use:

col = tableDetails.getColumnModel().getColumn(3);

in the new code it's done as

tableDetails.getColumn("A").getWidth()

just guessing that could be a place to try debugging.

Mike

[edit sorry didn't notice the reply]

Edited 6 Years Ago by adams161: n/a

This article has been dead for over six months. Start a new discussion instead.