1

I am trying to insert a newly created JTable into a panel I have also created but am having no luck. On top of this I am also trying to create a table that is edited based on a button being pressed where the button reads and stores the user input and then fills in the correct columns and was not sure if I have gone about this the right way.

Code is as follows:

public class resultTable extends JPanel {

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

public class myBottomPanel extends JPanel {

    public myBottomPanel()
    {
        setBorder(BorderFactory.createTitledBorder("Students/Results"));
        setOpaque(false);
        setPreferredSize(new Dimension(0, 100));
        add(resultField);
        resultField.setAlignmentX(LEFT_ALIGNMENT);
        add(resultTable);
    }
}

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);
        }
    }
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
SkyPlus
  • 105
  • 1
  • 9
  • I am not really sure about your class structure, but you need something to put the components on to, a main component so to speak. Then you can add a `JScrollPane` and to this `JScrollPane` you add your `JTable`. – hamena314 Mar 30 '16 at 12:35
  • 1
    See [Adding a table to a container](https://docs.oracle.com/javase/tutorial/uiswing/components/table.html) – hamena314 Mar 30 '16 at 12:36
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. 3) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) 4) `setPreferredSize(new Dimension(0, 100));` And.. what exactly did you think the `0` would do? – Andrew Thompson Mar 30 '16 at 12:59
  • @hamena314 I actually do have a main panel but I didnt include the code. Where would I begin the scroll pane? – SkyPlus Mar 30 '16 at 14:46
  • You should create a [mcve], so we can see your code and help you. It's hard to guess, how your classes look, what they contain, where you should integrate the `JScrollPane` etc. In general, you create a main panel, create a scroll pane, create a table, add the table to the scroll pane and then add the scroll pane to the main panel, which is actually what @Jorge is proposing in his answer. – hamena314 Mar 31 '16 at 07:32

1 Answers1

2

If you want the table to be scrollable and visible, make sure the table is added as a view of a JScrollPane

// Create the Scroll panel and add the table (view)
JScrollPane jsp = new JScrollPane(table)
....
// Add the Scroll Panel to the main panel
myBottomPanel.add(jsp)

Then, add the JScrollpane to a panel and the table should be visible and scrollable.

Jorge
  • 1,136
  • 9
  • 15