SkyPlus
SkyPlus

Reputation: 105

How to generate a table that can be inserted in a panel?

I am really struggling to assemble this code together. The final plan is that I have two entry fields, one asking for a name, one asking for a mark. I would then input a name and a mark and the program upon a push of a button would enter these values into a table, which through a sum would assign a grade to that mark (A, B, C etc.); and finally when all values were entered, the user would push another button which would print to a text file a list of names and their appropriate grade. That is the end game.

Currently I am really struggling to create the table and place it in the lower portion of my frame so that it will update each time one button is pressed. The lower portion of my frame being 'myBottomPanel'. Would really appreciate it if you could advise me and potentially help me on my way to the end game.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

public class GradePanel2 extends JPanel {

    private JButton addEntry, calculate;
    private JLabel name, grade, nameResult, gradeResult;
    private JTextField nameField, gradeField;

    public GradePanel2() {
        // Button to add entry to list
        addEntry = new JButton("Add entry to list");
        addEntry.addActionListener(new buttonListener());
        // Button to print all entries in correct format
        calculate = new JButton("Print all user grades");
        calculate.addActionListener(new buttonListener());
        // Create Labels
        name = new JLabel("Enter student name: ");
        nameField = new JTextField(10);
        nameField.addActionListener(new buttonListener());
        grade = new JLabel("Enter students mark: ");
        gradeField = new JTextField(5);
        gradeField.addActionListener(new buttonListener());
        // Bottom segment for result
        setLayout(new BorderLayout());
        // Button Panel
        JPanel ButtonPane = new JPanel();
        ButtonPane.setLayout(new BoxLayout(ButtonPane, BoxLayout.PAGE_AXIS));
        addEntry.setAlignmentX(CENTER_ALIGNMENT);
        calculate.setAlignmentX(CENTER_ALIGNMENT);
        ButtonPane.add(Box.createVerticalStrut(15));
        ButtonPane.add(addEntry);
        ButtonPane.add(Box.createVerticalStrut(15));
        ButtonPane.add(calculate);
        // Label Panel
        JPanel labelPane = new JPanel();
        labelPane.setLayout(new BoxLayout(labelPane, BoxLayout.PAGE_AXIS));
        labelPane.add(name);
        labelPane.add(Box.createRigidArea(new Dimension(5, 0)));
        labelPane.add(nameField);
        labelPane.add(Box.createRigidArea(new Dimension(0, 2)));
        labelPane.add(grade);
        labelPane.add(Box.createRigidArea(new Dimension(5, 0)));
        labelPane.add(gradeField);
        myBottomPanel mybottompanel = new myBottomPanel();
        // Add all panels to the main panel
        add(labelPane, BorderLayout.NORTH);
        add(ButtonPane, BorderLayout.CENTER);
        add(mybottompanel, BorderLayout.SOUTH);
        setBackground(Color.WHITE);
        setPreferredSize(new Dimension(400, 300));
    }

    public class resultTable extends myBottomPanel {

        public void mainTable() {
            GridLayout mainLayout = (new GridLayout(1, 0));
            String[] columnNames = {"NAME", "GRADE"};
            Object[][] data = {{nameField.getText(), gradeField.getText()}};
            JTable table = new JTable(data, columnNames);
            table.setPreferredScrollableViewportSize(new Dimension(500, 70));
            table.setFillsViewportHeight(true);
            JScrollPane scrollPane = new JScrollPane(table);
            table.setFillsViewportHeight(true);
        }
    }

    public class myBottomPanel extends JPanel {

        public myBottomPanel() {
            resultTable results = new resultTable();
            add(results);
            setBorder(BorderFactory.createTitledBorder("Students/Results"));
            setOpaque(false);
        }
    }

    public class buttonListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent event) {
            String studentName;
            int studentMark;
            if (event.getSource() == addEntry) {
                studentName = nameField.getText();
                String intMark = gradeField.getText();
                studentMark = Integer.parseInt(intMark);
                System.out.println(studentName);
                System.out.println(studentMark);
            }
        }
    }
}

Finally I'm unsure on how to get code pasted correctly for the forum, so excuse the top few lines being misplaced; I just added 4 spaces to them so they show up in the code.

Upvotes: 1

Views: 115

Answers (1)

trashgod
trashgod

Reputation: 205785

Your existing code throws java.lang.StackOverflowError as it attempts to invoke the MyBottomPanel constructor recursively from the GradePanel2 constructor.

Focusing on how to add a row to a table, you might start with this complete, working example. Change the TableModel to match your use case, add a pair of text fields to panel, and alter addRow() to use the field values.

image

Upvotes: 2

Related Questions