0

I am trying to put some text fields and labels underneath a JTable or a similar GridLayout.

When I type data in the fields, I want to pass them on to the table. I now have text fields and labels within a panel, but want to add a table above.

I tried using JPanel, JFrame, JTable. But I could not get them working together.

What are the experiences with Swing/AWT imports, any suggestions about the best way to get this result?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
SetFlame
  • 3
  • 1
  • 3

2 Answers2

4

You could....

Use a BorderLayout, leaving the JTable (wrapped in a JScrollPane) in the NORTH position.

Add a JPanel, containing the JLabel and JTextField, using a FlowLayout or GridBagLayout to the SOUTH position

You could...

Use a GridBagLayout, adding all the components to a single panel...

You should take a look at Laying Out Components Within a Container for more details...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • It is a pretty simple project, it should be as simple as possible. If I fill in the fields I want to add a row to the table with the data and calculate some things. will a GridBagLayout be a good solution for this? – SetFlame Jan 14 '14 at 10:26
  • Adding data to the table is irrelevant, but yes, GridBagLayout can allow you to add the table above the two fields... – MadProgrammer Jan 14 '14 at 10:28
2

This could easily be accomplished with a BorderLayout. Add the JLabel and the JTextField to a JPanel with its default FlowLayout and add the JPanel and the JTabel to the frame using proper BorderLayout position

Something like this

    JTable table = new JTable(data, cols);
    JTextField jtf = new JTextField(20);
    JLabel label = new JLabel("This is a text field");
    JPanel panel = new JPanel();
    panel.add(label);
    panel.add(jtf);

    JFrame frame = new JFrame();
    frame.add(panel, BorderLayout.SOUTH);                     <----
    frame.add(new JScrollPane(table), BorderLayout.CENTER);   <----

Rum this example

import java.awt.BorderLayout;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class TableWithOthers {

    public TableWithOthers() {

        Object[][] data
                = {{"Hello", "World"},
                {"Hello", "World"},
                {"Hello", "World"},
                {"Hello", "World"}};
        String[] cols = {"Hello", "World"};

        JTable table = new JTable(data, cols);
        JTextField jtf = new JTextField(20);
        JLabel label = new JLabel("This is a text field");
        JPanel panel = new JPanel();
        panel.add(label);
        panel.add(jtf);

        JFrame frame = new JFrame();
        frame.add(panel, BorderLayout.SOUTH);
        frame.add(new JScrollPane(table), BorderLayout.CENTER);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new TableWithOthers();
            }
        });
    }
}

For adding the rows dynamically, you could just use the basic DefaultTableModel. If you know your column names you could do something like this

String[] cols = {"Col 1, "Col 2", "Col 3};
DefaultTableModel model = new DefaultTableModel(cols, 0);
JTable table = new JTabel(model);

Then to add the row, just do somethind like this in a button actionPerformed method

public void actionPerformed(ActionEvent e) {
    String data1 = textField1.getTex();
    String data2 = textField2.getTex();
    String data3 = textField3.getTex();

    Object[] row = {dat1, data2, data3};
    model.addRow(row);
}

Just adding rows to the model with update your table dynamically

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • thanks a lot! I used parts of this code to pass all the fields and labels to my own project. I have exactly what I wanted now. – SetFlame Jan 14 '14 at 11:04