Create a JTable inside a JTabbedPane using Swing UI designer - Intellij

862 Views Asked by At

I'm trying to create a frame with multiple tabs, each with a different list. I've used the Swing UI designer with Intellij to create the "assets" and then modified them in my main class file. I've looked up some examples of creating a JTable using the designer, but these don't use multiple tabs, which I think is the main problem for me right now as I am not able to figure out how to do it.

This is the code for now.

package client;

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

public class ATCGUI extends GUI {

    private JPanel mainPanel;
    private JTabbedPane tabbedPanel;
    private JPanel tabbedAirportInfo;

    private JScrollPane scrollPane;
    private JButton addButtonOut;
    private JButton deleteButtonOut;
    private JButton addButtonIn;
    private JButton deleteButtonIn;
    private JTable tableOut;
    private JTable tableIn;
    private JPanel panelOut;
private JScrollPane tabbedPaneOut;

    public ATCGUI(String AirportCode) {

        this.add(mainPanel);

        String airportID = getID(AirportCode);

        TableOut(airportID);

        pack();
        setVisible(true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

    private void TableOut(String airportID){
        String[] columnNames = {"Airline", "Destination", "Aircraft"};

        Object[][] dataTest = {{"Iberia", "London", "737"}, {"AirEuropa", "Paris", "320"}, {"BritishAirways", "Berlin", "330"}};

        JTable TableOut = new JTable(dataTest, columnNames);

        setContentPane(panelOut);
    }
    public static void main(String[] args) {
        new ATCGUI("MAD");
    }
}

For now it shows the different tabs, but the JTable doesn't appear in any of the tabs. And I did make sure to put the JTable inside a JScrollPane.

I'm pretty new to this so maybe there's something I've missed out or just simply don't know.

1

There are 1 best solutions below

1
macrofox On

Without your JTabbedPane code it will be difficult to suggest what you can "fix" in your own code.

This is a good reference for using a JTabbedPane with multiple tabs:

https://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html

An example of how JTabbedPane might be used with your data:

package whatever;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;

public class MyGui extends JFrame {

    private JTabbedPane tPane;

    MyGui(String t){
        super(t);
        this.tPane = new JTabbedPane();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        tPane.addTab("Tab 1", createTab());
        tPane.addTab("Tab 2", createTab());
        tPane.addTab("Tab 3", createTab());
        tPane.addTab("Tab 4", createTab());
        this.setContentPane(this.tPane);
        this.setSize(400, 400);
        this.setVisible(true);
    }
    
    public static void main(String[] args) {
        new MyGui("My Tabbed App");
    }

    private JScrollPane createTab(){
        String[] columnNames = {"Airline", "Destination", "Aircraft"};
        Object[][] dataTest = {{"Iberia", "London", "737"}, {"AirEuropa", "Paris", "320"}, {"BritishAirways", "Berlin", "330"}};
        JTable tableOut = new JTable(dataTest, columnNames);
        return new JScrollPane(tableOut);
    }
}