Question

I am trying to put the JTextFild over the JTable using GridBagLayout but the thing is only one component is showing. That means I have to add a JPanel for each component, because I tried to add both of them in on a JPanel, then I tried to add them to the container.

The piece of code:

  package AnimeAid;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.TableColumn;


/**
 *
 * @author isslam
 */
public class GuiInterface extends JFrame {
    JTable table;
    JTextField enterText;
     public static void main(String[] args) {
     GuiInterface is = new GuiInterface("t");
     is.setVisible(true);
     }

    public GuiInterface(String title){
    setSize(900, 700);
    setTitle(title);
    setDefaultCloseOperation(GuiInterface.EXIT_ON_CLOSE);
    String[] columnNames = {"#","Start","End","Translation column"};
    Object[][] data = {
    {"1", "00:00:01,600","00:00:04,080", "Mr Magnussen, please state your\n" +
    "full name for the record."},
    {"2", "00:00:04,080 ","00:00:07,040","Charles Augustus Magnussen."}};
    enterText = new JTextField();

    table = new JTable(data, columnNames);
    table.setFillsViewportHeight(true);
    table.setAutoResizeMode( JTable.AUTO_RESIZE_ALL_COLUMNS );
        TableColumn columnA = table.getColumn("#");
        columnA.setMinWidth(10);
        columnA.setMaxWidth(20);
        TableColumn columnB= table.getColumn("Start");
        columnB.setMinWidth(80);
        columnB.setMaxWidth(90);

        TableColumn columnC= table.getColumn("End");
        columnC.setMinWidth(80);
        columnC.setMaxWidth(90);

        JPanel textFiled = new JPanel(new GridBagLayout());
        textFiled.add(enterText);
        GridBagConstraints co = new GridBagConstraints();
        co.fill = GridBagConstraints.HORIZONTAL;
        co.gridx =0;
        co.gridy =0;
        co.weightx=0.5;
        co.weighty=1;
        co.gridheight=0;
        co.gridwidth=0;
        co.ipadx=900;
        co.ipady=80;
        co.anchor = GridBagConstraints.PAGE_START;
        co.insets = new Insets(5, 5, 0, 0);
        textFiled.add(enterText,co);

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

        Container cp = getContentPane();
        cp.add(textFiled);



    }}

how it should look like but what happen is only one component are showing in the JFrame why and How to show all the Component

example

Was it helpful?

Solution

Just add the text field to the NORTH position of the default BorderLaoyout. Instead of this

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

    Container cp = getContentPane();
    cp.add(textFiled);

Just do this

    JScrollPane scrollPane = new JScrollPane(table);
    add(scrollPane, BorderLayout.CENTER);

    add(textFiled, BordderLayout.NORTH);

Also why in the world are you adding enterText into textFiled twice. You may want to remove one of the.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top