user2054151
user2054151

Reputation: 7

Swing - text input and ouput to text area

I want to make simple GUI with Swing, which will have 2 input text fields and a button.

Based on the input the program will fetch name/rol-no/age/emailid from table and need to show into a text area below in the same screen. The text area should be scrollable. I want input/output to be in the same screen. Could some one tell which layout will be ideal for this?

---------------------------------------------------------------------------
School(label1):textfield1____
class(label2):textfield2____
SUbmit(Button1)

      name(label2)     roll-no(label3)      age(label4)   email-id(label5) 
row-1  -----
row-2  ----
row-3  ----
row-4  ----
.
.
row-nth ---
----------------------------------------------------------------------------

Hi
Tried with below code.. but the table positined incorreclty.Please help. public class test {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    final JFrame f = new JFrame();
    f.setSize(1000, 1000);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setResizable(true);

    final JPanel pnl = new JPanel(new GridBagLayout());
    final GridBagConstraints c = new GridBagConstraints();

    JLabel lbl = new JLabel("School ");
    c.gridx = 0;
    c.gridy = 0;
    pnl.add(lbl, c);

    final JTextField tf = new JTextField(20);
    c.gridx = 1;
    c.gridy = 0;
    tf.setText("");
    pnl.add(tf, c);

    JLabel lb2 = new JLabel("Class");
    c.gridx = 0;
    c.gridy = 1;
    pnl.add(lb2, c);

    final JTextField tf2 = new JTextField(20);
    c.gridx = 1;
    c.gridy = 1;
    tf2.setText("");
    pnl.add(tf2, c);

    final String[] columnNames = { "name", "roll-no", "age", "email-id" };
    Object[][] columnData = new String[2][4];
    columnData[0][0] = "X";
    columnData[0][1] = "1";
    columnData[0][2] = "22";
    columnData[0][3] = "[email protected]";
    columnData[1][0] = "Y";
    columnData[1][1] = "2";
    columnData[1][2] = "24";
    columnData[1][3] = "[email protected]";
    JTable table = new JTable(columnData, columnNames);
    table.setBounds(300, 600, 700, 150);

    JScrollPane scrollpane = new JScrollPane(table);
    pnl.add(scrollpane);

    f.getContentPane().add(BorderLayout.NORTH, pnl);
    f.setVisible(true);

}

}

Upvotes: 0

Views: 2185

Answers (3)

MadProgrammer
MadProgrammer

Reputation: 347194

I would start with a JPanel using BorderLayout, let's call it content for now...

I would then create a JPanel using a GridBagLayout and add your fields to it, lets call this form for now...

I would add the form to the content in the BorderLayout.NORTH position.

I would then add a JTable, in a JScrollPane to the BorderLayout.CENTER position of content

enter image description here

Start by having a look at...

For more details...

Updated with example based on updated question

You mean something more like...

enter image description here

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestLayout30 {

    public static void main(String[] args) {
        new TestLayout30();
    }

    public TestLayout30() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new MainPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class MainPane extends JPanel {

        public MainPane() {            
            setLayout(new BorderLayout());

            JPanel pnl = new JPanel(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();

            JLabel lbl = new JLabel("School ");
            c.gridx = 0;
            c.gridy = 0;
            c.anchor = GridBagConstraints.WEST;
            pnl.add(lbl, c);

            final JTextField tf = new JTextField(20);
            c.gridx = 1;
            c.gridy = 0;
            c.weightx = 1;
            tf.setText("");
            pnl.add(tf, c);

            JLabel lb2 = new JLabel("Class");
            c.gridx = 0;
            c.gridy = 1;
            c.weightx = 0;
            pnl.add(lb2, c);

            final JTextField tf2 = new JTextField(20);
            c.gridx = 1;
            c.gridy = 1;
            c.weightx = 1;
            tf2.setText("");
            pnl.add(tf2, c);

            add(pnl, BorderLayout.NORTH);

            final String[] columnNames = {"name", "roll-no", "age", "email-id"};
            Object[][] columnData = new String[2][3];
            columnData[0][0] = "X";
            columnData[0][4] = "1";
            columnData[0][5] = "22";
            columnData[0][6] = "[email protected]";
            columnData[1][0] = "Y";
            columnData[1][7] = "2";
            columnData[1][8] = "24";
            columnData[1][9] = "[email protected]";
            JTable table = new JTable(columnData, columnNames);
            table.setBounds(300, 600, 700, 150);

            JScrollPane scrollpane = new JScrollPane(table);
            add(scrollpane);

        }
    }
}

Instead of adding all the fields and table to the same panel, use a "master" panel, which uses a BorderLayout and add the fields (on a separate panel) to the NORTH position and the table to the CENTER position. This is commonly known as compound layouts. It allows you to devise complex layouts with all the complexity of trying to get it all to fit into a single component...

You also use the GridBagConstraints weightx = 1, weighty = 1, fill = GridBagConstraints.BOTH, gridwidth = GridBagConstraints.REMAINDER to add the table, but that just gets messy...

Updated with selectable but not editable example

The DefaultTableModel is editable by default. The only way to change it is to override the isCellEditable method and make it return false

enter image description here

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

public class TestLayout30 {

    public static void main(String[] args) {
        new TestLayout30();
    }

    public TestLayout30() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new MainPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class MainPane extends JPanel {

        public MainPane() {            
            setLayout(new BorderLayout());

            JPanel pnl = new JPanel(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();

            JLabel lbl = new JLabel("School ");
            c.gridx = 0;
            c.gridy = 0;
            c.anchor = GridBagConstraints.WEST;
            pnl.add(lbl, c);

            final JTextField tf = new JTextField(20);
            c.gridx = 1;
            c.gridy = 0;
            c.weightx = 1;
            tf.setText("");
            pnl.add(tf, c);

            JLabel lb2 = new JLabel("Class");
            c.gridx = 0;
            c.gridy = 1;
            c.weightx = 0;
            pnl.add(lb2, c);

            final JTextField tf2 = new JTextField(20);
            c.gridx = 1;
            c.gridy = 1;
            c.weightx = 1;
            tf2.setText("");
            pnl.add(tf2, c);

            add(pnl, BorderLayout.NORTH);

            final String[] columnNames = {"name", "roll-no", "age", "email-id"};
            DefaultTableModel model = new DefaultTableModel(columnNames, 0) {

                @Override
                public boolean isCellEditable(int row, int column) {
                    return false;
                }

            };
            Object[][] rowData = new String[2][11];
            rowData[0][0] = "X";
            rowData[0][12] = "1";
            rowData[0][13] = "22";
            rowData[0][14] = "[email protected]";
            rowData[1][0] = "Y";
            rowData[1][15] = "2";
            rowData[1][16] = "24";
            rowData[1][17] = "[email protected]";

            for (Object[] row : rowData) {
                model.addRow(row);
            }

            JTable table = new JTable(model);
            JScrollPane scrollpane = new JScrollPane(table);
            add(scrollpane);
        }
    }
}

Also, avoid using setBounds, it just creates more problems then it solves...

Upvotes: 1

user1831840
user1831840

Reputation:

I was tired of Java complex layout managers that did not make much sense and moved to:

http://www.miglayout.com/

Easy to install,10 ~ 20 min to get hang of all the basics,it was heaven and made designing for me much easier. I recommend it over other layout managers.

Upvotes: 0

Vimal Bera
Vimal Bera

Reputation: 10497

You can use either TableLayout or GridBagLayout. Here are the link for both.
1. GridBagLayout
2. TableLayout

Upvotes: 0

Related Questions