I am creating an application the displays information from a database in the form of a table. As the number of rows in the table can vary, I have created a generic method that will create a single row of the table, either the header of the table or the individual rows. I have done so using a JPanel.
My problem is that each time I add a new row to the table, there is a gap between the rows. I know I can use a Table, but this is also a learning experience for me using swing, so I would like to accomplish this using JPanels if possible.
Below is the method to create the row:
public static JPanel customersTable(){
FlowLayout FlowCustTable = new FlowLayout();
FlowCustTable.setHgap(1);
FlowCustTable.setVgap(0);
JPanel customersTable = new JPanel(FlowCustTable);
JTextField surname = new JTextField();
FieldSetup(surname, 120, 25, " Surname", Color.CYAN, false);
JTextField firstname = new JTextField();
FieldSetup(firstname, 120, 25, " Firstname", Color.CYAN, false);
JTextField mobile = new JTextField();
FieldSetup(mobile, 120, 25, " Mobile", Color.CYAN, false);
JTextField homePhone = new JTextField();
FieldSetup(homePhone, 120, 25, " Home Phone", Color.CYAN, false);
JTextField address = new JTextField();
FieldSetup(address, 280, 25, " Address", Color.CYAN, false);
JTextField postcode = new JTextField();
FieldSetup(postcode, 120, 25, " Postcode", Color.CYAN, false);
customersTable.add(surname);
customersTable.add(firstname);
customersTable.add(mobile);
customersTable.add(homePhone);
customersTable.add(address);
customersTable.add(postcode);
return customersTable;
}
This is the method used to set up the JTextfields:
public static void FieldSetup(JTextField field, int x, int y, String text, Color color, Boolean Editable){
field.setText(text);
field.setBackground(color);
field.setPreferredSize(new Dimension(x, y));
field.setEditable(Editable);
Border border = BorderFactory.createLineBorder(Color.BLACK);
field.setBorder(border);
}
and this is the method to create the final table and the User Interface:
public static void CustomersGui(){
final JFrame frame = new JFrame("Customers");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel customers = new JPanel();
customers.add(customersTable());
customers.add(customersTable());
frame.setContentPane(customers);
frame.setSize(1200,500);
frame.setVisible(true);
}
For the purpose of this question, I added 2 instances of the header of the table, but the same principle still applies.
I have tried setHgap and setVgap of the JPanel, and whilst this does affect the gap, it will not make it zero.