N
N
Nikita Filatov2015-10-09 17:36:45
Java
Nikita Filatov, 2015-10-09 17:36:45

How to create a table in Java Swing GUI?

Good day. I have been tormenting myself for an hour over the problem: it is necessary to create an interface in which JTable will be included. I threw a form in the GUI that is in the IDE. And I added a model to the table from the GUI, but for some reason it is not displayed if I add the entire Frame via JFrame.add. But if you add a separate table, then it is displayed.
MainForm.java

public class MainForm  extends  JFrame{


    private JPanel mainPanel;
    private JPanel leftPanel;
    private JPanel rightPanel;
    private JLabel middleResult;
    private JTextPane textPane1;
    private JLabel items;
    private JTable table1;
    private JTable table2;
    private JTable table3;
    private JButton считатьButton;
    private JButton очиститьButton;
    private JButton выходButton;
    private JScrollPane ScrollPane;

    public MainForm() {
        //Отрисовыаем окно
        setTitle("Transport Task");
        setSize(new Dimension(700, 500));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        ItemsTableModel itm = new ItemsTableModel();
        table1 = new JTable(itm);
        ScrollPane = new JScrollPane(table1);
        add(mainPanel);
        setVisible(true);
    }
}

d6698fc3f9ee4932b3f8a655585d30c2.png
import javax.swing.*;
import java.awt.*;

/**
 * Created by muffass on 09.10.15.
 */
public class MainForm  extends  JFrame{


    private JPanel mainPanel;
    private JPanel leftPanel;
    private JPanel rightPanel;
    private JLabel middleResult;
    private JTextPane textPane1;
    private JLabel items;
    private JTable table1;
    private JTable table2;
    private JTable table3;
    private JButton считатьButton;
    private JButton очиститьButton;
    private JButton выходButton;
    private JScrollPane ScrollPane;

    public MainForm() {
        //Отрисовыаем окно
        setTitle("Transport Task");
        setSize(new Dimension(700, 500));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        ItemsTableModel itm = new ItemsTableModel();
        table1 = new JTable(itm);
        ScrollPane = new JScrollPane(table1);
        add(ScrollPane);
        setVisible(true);
    }

}

59ff8fd58fa04851be5fc32f9c483f12.png
Added elements via GUI IDEA
3ba6b2619a1648dc827ef05b5469b4d2.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Danil Antoshkin, 2015-10-10
@TwerTrue

I would advise you to start working with JavaFX already, especially since you do not have a complicated graphical interface

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question