Search code examples
javaswingjlabeljtextfieldlayout-manager

Put JLabels and JTextField in same frame as JTable


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?


Solution

  • 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