Home > Back-end >  Unable to show JTable in JPanel from dynamically retrieved data
Unable to show JTable in JPanel from dynamically retrieved data

Time:11-18

I haven't been able to show a JTable inside a JPanel from dynamically generated data. I have even tried adding a layout manager so that I don't end up with a null layout manager and no table. Here is the code I'm using.

public void setReferencePanel(ArrayList<Item> items, String refFile) {
    String[] columns = {"first", "last"};
    String[][] data = {{"Adam", "Smith"}, {"Jon", "Bon Jovi"},{"John", "Doe"}};
    JTable sample = new JTable(data, columns);
    refListingPanel.add(sample);
    refListingPanel.setBorder(BorderFactory.createTitledBorder("Reference File - "   refFile));
}

and earlier in the same file.

private JMenuBar menuBar;

private JPanel testListingPanel;
private JScrollPane testScroller;
private JPanel refListingPanel;
private JScrollPane refScroller;

private static Dimension listingDefault = new Dimension(350, 230);
private IDiffPresenter presenter;
private boolean allItems;
private boolean unChangedItems;
private boolean changedItems;
private JTable refTable;
private JTable testTable;

public MasterFrame (IDiffPresenter presenter) {
    super("Magic Diff - Under Construction");
    this.presenter = presenter; 
    menuBar = new JMenuBar();       
    setupFileMenu(presenter);
    setupExportMenu(presenter); 
    setupDisplayChangedMenu();
    setupAboutMenu();

    setupReferencePanel();

    setupTestPanel();
    getContentPane().setLayout(new GridLayout(1,2));
    setJMenuBar(menuBar);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize((int)listingDefault.getWidth()   10, (int)listingDefault.getHeight()   60); 
    Dimension DimMax = Toolkit.getDefaultToolkit().getScreenSize();
    this.setMaximumSize(DimMax);

    this.setExtendedState(JFrame.MAXIMIZED_BOTH);
    ...
}
private void setupReferencePanel() {
    refListingPanel = new JPanel();
    refListingPanel.setLayout(new BorderLayout());
    refListingPanel.setBorder(BorderFactory.createTitledBorder("Reference File"));
    refScroller = new JScrollPane(refListingPanel);
    getContentPane().add(refScroller);
}

What am I missing or failing to do? I have even tried some sample data, what is currently in the code, and I get the same issue.

CodePudding user response:

This is based on what cmickr and Andrew Thompson posted. I modified the following functions to work.

public void setReferencePanel(ArrayList<Item> items, String refFile) {
    DefaultTableModel model = new DefaultTableModel(vectorize(items), makeHeaderVector());
    refTable.setModel(model);
    refListingPanel.setBorder(BorderFactory.createTitledBorder("Reference File - "   refFile));
}

private Vector vectorize(ArrayList<Item> items) {
    Vector results = new Vector();
    for (int i = 0; i < items.size(); i  ) {
        results.addElement(items.get(i).vectorize());
    }
    return results;
}

private Vector makeHeaderVector() {
    String[] cols = { ... }; // hid application specific string array
    Vector<String> results = new Vector<String>(Arrays.asList(cols));
    return results;
}

This is my basic understanding of vectors, since I do not use them much. Also, this may not be the fastest way of approaching the problem, but my first goal is to get it working, then improve it. The important part was to use a TableModel, in my case DefaultTableModel, and then setModel() of the the JTable to the new model, like was referenced.

  • Related