-1

I'm trying to fit three JPanel (dataPanel, tablePanel and btnPanel), so this is how far I got:

enter image description here

The thing is that the middle panel (tablePanel) is cropped on the top and the header is not visible. Also I added a JScrollPane to it that doesn't appear either. I'm new to Java Swing, so here's my code:

JFrame frame = new JFrame("Crear pedido");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(500,500);
//frame.setSize(300, 150);
// or frame.pack() to "pack" all the components in this frame
frame.setVisible(true);  // show it
frame.setResizable(false);
frame.setLocationRelativeTo(null);

JPanel dataPanel = new JPanel();
dataPanel.setLayout(new BoxLayout(dataPanel, BoxLayout.X_AXIS));
//frame.add((Component) BorderFactory.createEmptyBorder(30,0,0,0));
dataPanel.setBorder(new EmptyBorder(20,0,30,0));
frame.add(dataPanel, BorderLayout.NORTH);

JLabel id = new JLabel("ID:");
JLabel date = new JLabel("Fecha:");
JLabel dept = new JLabel("Departamento");

JTextField idInput = new JTextField (5);
idInput.setMaximumSize(new Dimension(70,25));
JFormattedTextField dateInput = new JFormattedTextField(df);
dateInput.setMaximumSize(new Dimension(70,25));
JComboBox deptSelect = new JComboBox(selectArray);
deptSelect.setMaximumSize(new Dimension(70,25));

id.setAlignmentX(Component.CENTER_ALIGNMENT);
date.setAlignmentX(Component.CENTER_ALIGNMENT);
dept.setAlignmentX(Component.CENTER_ALIGNMENT);
idInput.setAlignmentX(Component.CENTER_ALIGNMENT);
dateInput.setAlignmentX(Component.CENTER_ALIGNMENT);
deptSelect.setAlignmentX(Component.CENTER_ALIGNMENT);

dataPanel.add(Box.createRigidArea(new Dimension(30,0)));
dataPanel.add(id);
dataPanel.add(idInput);
dataPanel.add(Box.createRigidArea(new Dimension(30,0)));
dataPanel.add(date);
dataPanel.add(dateInput);
dataPanel.add(Box.createRigidArea(new Dimension(30,0)));
dataPanel.add(dept);
dataPanel.add(deptSelect);

JPanel productPanel = new JPanel();
productPanel.setLayout(new BoxLayout(productPanel, BoxLayout.Y_AXIS));
frame.add(productPanel, BorderLayout.CENTER);

JTable table = new JTable(data, headerProductos);
table.setMaximumSize(new Dimension(500,250));
table.setAlignmentX(Component.CENTER_ALIGNMENT);
table.setAlignmentY(Component.CENTER_ALIGNMENT);
table.getTableHeader().setForeground(Color.blue);
table.setEnabled(false);

JScrollPane scroll = new JScrollPane(table);
productPanel.add(table);
table.add(scroll);

JPanel btnPanel = new JPanel();
btnPanel.setLayout(new BoxLayout(btnPanel, BoxLayout.X_AXIS));
btnPanel.setBorder(new EmptyBorder(150,0,0,0));
frame.add(btnPanel, BorderLayout.SOUTH);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • You have a line that says `table.add(scroll);`, probably what you want is `frame.add(scroll)` instead. – Magdrop Aug 31 '18 at 18:13
  • I just want the scroll on the table, not the entire frame – Facundo Schiavoni Barreyro Aug 31 '18 at 18:14
  • If you're developing with Eclipse try using WindowBuilder plugin (can be downloaded from Eclipse Marketplace). It's a WYSIWYG editor for forms, but you can still edit generated code if you want. – Krystian G Aug 31 '18 at 18:14
  • I'm using IntelliJ Idea, I don't like Eclipse at all .. – Facundo Schiavoni Barreyro Aug 31 '18 at 18:14
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height - to show how the extra space should be used. 3) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. – Andrew Thompson Sep 01 '18 at 01:26
  • .. 4) `idInput.setMaximumSize(new Dimension(70,25));` See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) 5) `//frame.setSize(300, 150); // or frame.pack() to "pack" all the components in this frame frame.setVisible(true); // show it` No! The methods & order should be: 1) add **all** components 2) pack the top level container 3) set the TLC visible. – Andrew Thompson Sep 01 '18 at 01:29

1 Answers1

1

Replace lines:

    productPanel.add(table);
    table.add(scroll);

with:

    productPanel.add(scroll);
Krystian G
  • 2,842
  • 3
  • 11
  • 25