Home > Blockchain >  How do I make it so that the table is displayed inside the JFrame in Main?
How do I make it so that the table is displayed inside the JFrame in Main?

Time:05-22

I am currently making a program for my school project, it is a fast food order calculator, my problem is that I have to be able to display the table inside the JFrame in Main, if I add "frame.add(table);" inside Main, nothing happens. Any help would be appreciated, thank you!

JTable table;
JTable FastFoodCalculatorProgramming;

public FastFoodCalculatorProgramming() {
    setLayout(new FlowLayout());
    String[] columnNames = {"Product", "Description", "Item Price"};
    Object[][] data = {
        {"Product1", "Description1", "Price1"},
        {"Product2", "Description2", "Price2"},
        {"Product3", "Description3", "Price3"},
        {"Product4", "Description4", "Price4"},
        {"Product5", "Description5", "Price5"},
        {"Product6", "Description6", "Price6"},
        {"Product7", "Description7", "Price7"},
        {"Product8", "Description8", "Price8"},};

    table = new JTable(data, columnNames) {
        public boolean isCellEditable(int data, int columnNames) {
            return false;
        }
    };
    table.setPreferredScrollableViewportSize(new Dimension(500, 250));
    table.setFillsViewportHeight(true);

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

}

public static void main(String[] args) {

    FastFoodCalculatorProgramming table = new FastFoodCalculatorProgramming();
    table.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    table.setSize(600, 200);
    table.setVisible(true);
    table.setTitle("Fast Food Order Calculator");

    JLabel label = new JLabel();
    label.setText("Welcome to Fast Food Calculator!");
    label.setFont(new Font("Century Gothic", Font.BOLD, 30));
    label.setVerticalAlignment(JLabel.TOP);
    label.setHorizontalAlignment(JLabel.CENTER);
    label.setBorder(new EmptyBorder(20, 0, 0, 0));
    JFrame frame = new JFrame();
    frame.setVisible(true);
    frame.setSize(1000, 800);
    frame.setResizable(false);
    frame.setTitle("Fast Food Calculator");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setBackground(Color.gray);
    frame.add(label);
}

}

CodePudding user response:

  • Add frame.add(JScrollPane(table));
  • Change frame.add(label); to frame.add(label, BorderLayout.NORTH);
  • Move frame.setVisible(true); to the end of the main method
  • Remove frame.setSize(1000, 800); and frame.setResizable(false);
  • Add frame.pack() before frame.setVisible(true);

enter image description here

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

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

    private JTable table;
    private TableModel tableModel;
    private JLabel titleLabel;

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(getTitleLabel(), BorderLayout.NORTH);
                frame.add(new JScrollPane(getTable()));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    protected JLabel getTitleLabel() {
        if (titleLabel != null) {
            return titleLabel;
        }
        titleLabel = new JLabel();
        titleLabel.setText("Welcome to Fast Food Calculator!");
        titleLabel.setFont(new Font("Century Gothic", Font.BOLD, 30));
        titleLabel.setVerticalAlignment(JLabel.TOP);
        titleLabel.setHorizontalAlignment(JLabel.CENTER);
        titleLabel.setBorder(new EmptyBorder(20, 0, 0, 0));
        return titleLabel;
    }

    protected JTable getTable() {
        if (table != null) {
            return table;
        }
        table = new JTable(getTableModel());
        return table;
    }

    protected TableModel getTableModel() {
        if (tableModel != null) {
            return tableModel;
        }
        String[] columnNames = {"Product", "Description", "Item Price"};
        Object[][] data = {
            {"Product1", "Description1", "Price1"},
            {"Product2", "Description2", "Price2"},
            {"Product3", "Description3", "Price3"},
            {"Product4", "Description4", "Price4"},
            {"Product5", "Description5", "Price5"},
            {"Product6", "Description6", "Price6"},
            {"Product7", "Description7", "Price7"},
            {"Product8", "Description8", "Price8"}
        };
        tableModel = new DefaultTableModel(data, columnNames);
        return tableModel;
    }
}

Personally, I always get out of a static context as soon as I can, it will just make life easier

  • Related