Juxhin
Juxhin

Reputation: 5620

Alternatives around JTable constructor

After spending countless hours working with Swing and trying my best to learn, I came across this issue. Before going to the exact problem I'd like to give a brief rundown of the situation. Feel free to skip this it if you notice the issue immediately.

I've spent the past 3 days working with JTables (or atleast trying to). I am having a couple issues with the JTable constructor:

JTable(Object[][] rowData, Object[] columnNames)

After reading and rereading oracle documentation I've stumbled across a set of points regarding JTable constructor(cons of using it):


Both the first and third points are giving me issues so I would like to ask a few question:

  1. How can I make individual column width static, right now, all the column widths are even and scale evenly when resizing. This is an issue as column "ID" clearly doesn't need as much space as column "E-mail"

  2. I've also been having issue displaying the column names for some odd reason, having my String[] columnNames passed into the constructor but nothing is displayed on the frame.


JTable code:

private JTable addStudentsJTable(){
    String[] columnNames = {
            "Teacher ID",
            "Student ID",
            "Name",
            "Surname",
            "E-mail",
            "Date of Birth",
            "Telephone",
            "Course",
            "OOP",
            "Year of Membership",
            "Consent"};

    Object[][] data = {
            {1, 1, "Random", "Student", "[email protected]", "1/1/1970", 9999999, "MC", true, 2014, true},
            {2, 2, "Student", "Surname", "[email protected]", "1/1/1970", 11111111, "AIO", true, 2000, true}
    };
    studentsJTable = new JTable(data, columnNames);

    studentsJTable.setPreferredScrollableViewportSize(new Dimension(850,200));
    studentsJTable.setFillsViewportHeight(true);

    JScrollPane scrollPane = new JScrollPane(studentsJTable);
    add(scrollPane);
    return studentsJTable;
}

Whole JFrame class:

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableModel;
import java.awt.*;

public class JDatabaseMainFrame extends JFrame {
    private JTable studentsJTable;
    private JPanel contentPane;

public JDatabaseMainFrame(){
    JFrame frame = new JFrame("JDatabase");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setTitle("JDatabase");
    setResizable(true);
    setMinimumSize(new Dimension(850,350));

    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(new CardLayout(0, 0));

    //Adding tabs
    JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.LEFT);
    contentPane.add(tabbedPane, "Tabs");

    //Adding Student's panel
    JPanel studentsPanel = new JPanel();
    tabbedPane.addTab("Students", null, studentsPanel, null);
    studentsPanel.setLayout(new BorderLayout(0, 0));

    //Adding JTable to Student's panel
    JTable studentsJTable = addStudentsJTable();
    studentsPanel.add(studentsJTable, BorderLayout.CENTER);

    //Adding Teacher's panel
    JPanel teachersPanel = new JPanel();
    tabbedPane.addTab("Teachers", null, teachersPanel, null);

    //Adding Outing's panel
    JPanel outingsPanel = new JPanel();
    tabbedPane.addTab("Outings", null, outingsPanel, null);

    setBounds(200,200,650,650);
    pack();
    setVisible(true);
}

private void printData(JTable jTable){
    int numberOfRows = jTable.getRowCount();
    int numberOfColumns = jTable.getColumnCount();

    TableModel model = jTable.getModel();

    for(int i = 0; i < numberOfRows; i++){
        System.out.println("Row: " + i + ":");
        for(int j = 0; j < numberOfColumns; j++){
            System.out.println("  " + model.getValueAt(i, j));
        }
        System.out.println();
    }
    System.out.println("-------------------------------");
}

private JTable addStudentsJTable(){
    String[] columnNames = {
            "Teacher ID",
            "Student ID",
            "Name",
            "Surname",
            "E-mail",
            "Date of Birth",
            "Telephone",
            "Course",
            "OOP",
            "Year of Membership",
            "Consent"};

    Object[][] data = {
            {1, 1, "Random", "Student", "[email protected]", "1/1/1970", 9999999, "MC", true, 2014, true},
            {2, 2, "Student", "Surname", "[email protected]", "1/1/1970", 11111111, "AIO", true, 2000, true}
    };
    studentsJTable = new JTable(data, columnNames);

    studentsJTable.setPreferredScrollableViewportSize(new Dimension(850,200));
    studentsJTable.setFillsViewportHeight(true);

    JScrollPane scrollPane = new JScrollPane(studentsJTable);
    add(scrollPane);
    return studentsJTable;
}
public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            JDatabaseMainFrame frame = new JDatabaseMainFrame();
            frame.setVisible(true);
        }
    });
}
}

Feel free to compile it to see the current outcome!

Upvotes: 1

Views: 1030

Answers (1)

Kevin Workman
Kevin Workman

Reputation: 42174

You should set up a custom TableModel, that way you can use any data structures you want instead of arrays: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data

That will also allow you to specify which cells are editable.

You can set the widths of the JTable columns just by calling the appropriate methods in the TableColumn class. More info here: http://docs.oracle.com/javase/8/docs/api/javax/swing/table/TableColumn.html

The JTable header is only automatically shown when it's in a JScrollPane. You do add your JTable to a JScrollPane, but then you don't do anything with that JScrollPane. The JScrollPane itself has to be in a JFrame somewhere.

Upvotes: 2

Related Questions