Home > OS >  How to update a JTable during runtime as a static type from another class
How to update a JTable during runtime as a static type from another class

Time:08-13

Anyone know how to update a JTable during runtime as a static type from another class? example of method trying to be accessed:

public static void initEvents(String category, String task, String duration, String organization, StatusType type){
        table.addRow(new Object[]{category, task, duration, organization,type});
        table.repaint();
    }

example of method being called from another class

Form_Dashboard.initEvents("","","","",StatusType.Active);

example of addRow method

    public void addRow(Object[] row) {
        DefaultTableModel model = (DefaultTableModel) getModel();
        model.addRow(row);
    }

example of full table class

package com.swift.view.component;

import com.swift.controller.StatusType;

import java.awt.Color;
import java.awt.Component;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class Table extends JTable {

    public Table() {
        setShowHorizontalLines(true);
        setGridColor(new Color(0,0,0));
        setRowHeight(40);
        getTableHeader().setReorderingAllowed(false);
        getTableHeader().setDefaultRenderer(new DefaultTableCellRenderer() {
            @Override
            public Component getTableCellRendererComponent(JTable jtable, Object o, boolean bln, boolean bln1, int i, int i1) {
                TableHeader header = new TableHeader(o   "");
                if (i1 == 4 || i1 == 3) {
                    header.setHorizontalAlignment(JLabel.CENTER);
                }
                return header;
            }
        });
        setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
            @Override
            public Component getTableCellRendererComponent(JTable jtable, Object o, boolean selected, boolean bln1, int i, int i1) {
                if (i1 != 4) {
                    Component com = super.getTableCellRendererComponent(jtable, o, selected, bln1, i, i1);
                    com.setBackground(Color.BLACK);
                    setBorder(noFocusBorder);
                    if (selected) {
                        com.setForeground(new Color(15, 89, 140));
                    } else {
                        com.setForeground(new Color(255,255,255));
                    }
                    return com;
                } else {
                    StatusType type = (StatusType) o;
                    CellStatus cell = new CellStatus(type);
                    return cell;
                }
            }
        });
    }

    public void addRow(Object[] row) {
        DefaultTableModel model = (DefaultTableModel) getModel();
        model.addRow(row);
    }
}

EDIT found something out during testing, Ran the following

    public static void initEvents(String category, String task, String duration, String organization, StatusType type){
        System.out.println("Made it to initEvents");
        table.addRow(new Object[]{category, task, duration, organization,type});
        System.out.println("Made it below initEvents");
        System.out.println(table.getRowCount());
    }

The terminal is stating that the rows are in fact incrementing and everything is being read. This seems to be a repainting issue. Any suggestions? Link to the terminal in imgur post below

repaint and revalidate will not work due to this being a JPanel and it being in a static context. Currently exploring options with paintComponent()

CodePudding user response:

The "problem" (more like an issue) I see here is that you are using the DefaultTableModel.

I would strongly encourage you to create your own TableModel that interfaces with your data. That way, when it gets updated, fire a TableModelEvent and it should update your JTable automatically

CodePudding user response:

repaint(),revalidate() method in Java Swing could help, but it make Frame re-render too. Hope it will help you

  • Related