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