PROGRAMMING

m9/ JAVA
REMEMBERS




Last update:   26-07-2021

JTable

! JTable does not contain or cache data. It is {{simply a view{{ of your data.
 
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.DefaultTableModel;

public class App extends JFrame {

    public static void main(String[] args) {
        App frame = new App();
        frame.setBounds(200, 200, 600, 400);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.setVisible(true);
    }

    public App() {
        initMenu();
    }

    public void initMenu() {
        JMenuBar menuBar = new JMenuBar();
        JMenu menuOpen = new JMenu("Open");
        JMenuItem menuOpenItem = new JMenuItem("Table");

        menuBar.add(menuOpen);
        menuOpen.add(menuOpenItem);

        setJMenuBar(menuBar);
        
        // Look here        
        menuOpenItem.addActionListener(new MenuAction(new Action1()));
    }

    private class MenuAction implements ActionListener {

        private JPanel panel;
        private MenuAction(JPanel panel) {
            this.panel = panel;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            changePanel(panel);
        }
    }

    public void changePanel(JPanel panel) {
        getContentPane().removeAll();
        getContentPane().add(panel, BorderLayout.CENTER);
        getContentPane().doLayout();
        validate();
        repaint();
    }
}

class Action1 extends JPanel {
    
    public Action1() {
        
        // Look here
        
        // table model
        Object[][] data = new Object[][] { };
        String[] columns = new String[] {"Total", "Max" };
        DefaultTableModel tableModel = new DefaultTableModel(data, columns);

        JTable table = new JTable(tableModel);

        // table add rows
        tableModel.addRow(new Object[] {10,20});
        tableModel.addRow(new Object[] {15,25});

        // table dimensions and scrollPane (to show columns)
        table.setPreferredScrollableViewportSize(new Dimension(280, 100));
        table.setFillsViewportHeight(true);
        JScrollPane scrollPane = new JScrollPane(table);
        this.add(scrollPane);
    }
}
... 55 lines
 
You can change cell color using setForeground() method.
 
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class App extends JFrame {

    public static void main(String[] args) {
        
        //setLookAndFill();
        
        App frame = new App();
        frame.setBounds(200, 200, 600, 400);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.setVisible(true);
    }

    public App() {
        initMenu();
    }

    public void initMenu() {
        JMenuBar menuBar = new JMenuBar();
        JMenu menuOpen = new JMenu("Open");
        JMenuItem menuOpenItem = new JMenuItem("Table");

        menuBar.add(menuOpen);
        menuOpen.add(menuOpenItem);

        setJMenuBar(menuBar);
        
        menuOpenItem.addActionListener(new MenuAction(new Action1()));
    }

    private class MenuAction implements ActionListener {

        private JPanel panel;
        private MenuAction(JPanel panel) {
            this.panel = panel;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            changePanel(panel);
        }
    }

    public void changePanel(JPanel panel) {
        getContentPane().removeAll();
        getContentPane().add(panel, BorderLayout.CENTER);
        getContentPane().doLayout();
        validate();
        repaint();
    }
}

class Action1 extends JPanel {
    
    public Action1() {
                
        // table model
        Object[][] data = new Object[][] { };
        String[] columns = new String[] {"Total", "Max" };
        DefaultTableModel tableModel = new DefaultTableModel(data, columns);

        JTable table = new JTable(tableModel);

        // table add rows
        tableModel.addRow(new Object[] {10,20});
        tableModel.addRow(new Object[] {15,25});
        tableModel.addRow(new Object[] {11,22});
        tableModel.addRow(new Object[] {33,44});

        // table dimensions and scrollPane (to show columns)
        table.setPreferredScrollableViewportSize(new Dimension(280, 100));
        table.setFillsViewportHeight(true);
        table.setGridColor(Color.decode("#CCCCCC"));
        
        // Look here --- col / row
        table.getColumnModel().getColumn(0).
            setCellRenderer(new CustomRenderer(0, 3));
        table.getColumnModel().getColumn(1).
            setCellRenderer(new CustomRenderer(1, 2)); 
                
        JScrollPane scrollPane = new JScrollPane(table);
        this.add(scrollPane);
    }
}

class CustomRenderer extends DefaultTableCellRenderer { // Look Here
    
    int col; 
    int row;
    
    public CustomRenderer (int col, int row) {
       this.col = col;
       this.row = row;
    }
    
    @Override
    public Component getTableCellRendererComponent
            (JTable table, Object value, boolean isSelected, 
                boolean hasFocus, int row, int column) {
        
        Component c = super.getTableCellRendererComponent(
            table, value, isSelected, hasFocus, row, column
        );
        setForeground( (column == this.col && row == this.row) ? 
                            Color.red : Color.black);
        setBackground( (column == this.col && row == this.row) ? 
                            Color.decode("#FBEFF5") : Color.white);
        return c;
    }
}
... 84 lines
 

Questions    
Layout

        A B C D E F
🔔
1/1