0

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.

  • 2
    Try changing the gap in `customers`. Maybe better suited for BoxLayout instead of the default FlowLayout, if you want components added vertically. The FlowLayout adds them horzontally. Overflow would cause it to appear vertical, but you don't want to depend on that, when BoxLayout already offered the functionality you want – Paul Samsotha Oct 29 '14 at 13:36
  • 1
    1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example). 2) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Oct 29 '14 at 13:37
  • @AndrewThompson I tried to adhere to MCVE principles. Can I ask what information I could have excluded to better comply with these principles? –  Oct 29 '14 at 13:40
  • 1
    On another note, why not just use a JTable, as it looks like the field are to create some tabular format. Considering you don't want the gaps, it seams reasonable, Especially because I don't see any labels for the fields. You can set the column width and row height on the table – Paul Samsotha Oct 29 '14 at 13:43
  • 2
    As to my first comment `FlowLayout layout = (FlowLayout)customers.getLayout(); set gaps` – Paul Samsotha Oct 29 '14 at 13:44
  • 1
    An MCVE should be one source file, with `main(String[])` and imports. It would not have 6 x `FieldSetup` calls when the problem can be demonstrated using using 1 or two. It would either hot link to, or generate images in, the source code.. Just think to yourself, "What can I do to make it effortless to copy/paste this code on another PC, and see the problem?". And on the code posted: Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! – Andrew Thompson Oct 29 '14 at 23:02

1 Answers1

0

As @peeskillet pointed out the default layout manager for panels is FlowLayout and thus you'd have to remove the gaps in customers panel as well:

FlowLayout layout = (FlowLayout)customers.getLayout();
// set gaps here....

However if the goal is making a grid (tabular form) then probably either GridLayout or GridBagLayout are better choices than FlowLayout.

On the other hand, be aware that is really hard (I'd say impossible) to align components placed in different containers, so your current approach of doing each row a different panel may run into troubles when a single row has a different width. See a more detailed explanation in this topic: Align the label column of different DesignGridLayout

Community
  • 1
  • 1
dic19
  • 17,821
  • 6
  • 40
  • 69