user1713336
user1713336

Reputation: 129

Extending JTable and displaying in a JFrame

My code is still heavily in progress but my problem is that my class that extends JTable will not display in a JFrame. Here is my class that extends JTable. Secondly, I would like to know if the design option of making my LotteryTable an entirely new class a good idea.

At first, my LotteryDisplay contained a JTable which then was adjusted through a method tableSetup() which is now in LotteryTable.

public class LotteryTable extends JTable
{
    private final String[] columnNames = {"Powerball Combos", "Odds of Winning", "Payout", "Number of Wins", "Win Frequency"};
    JTable table;

    public LotteryTable()
    {
        DefaultTableModel model = new DefaultTableModel(columnNames, 9);
        table = new JTable(model)
        {
            public Class getColumnClass(int column)
            {
                if(column == 0)
                    return ImageIcon.class;
                else
                    return String.class;
            }
        };

        tableSetup();
        setVisible(true);
    }

    public void tableSetup()
    {
        DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer();
        dtcr.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
        table.setDefaultRenderer(String.class, dtcr);

        ImageIcon p = new ImageIcon("Icons/Powerball.png"); //Not important to my problem
        ImageIcon w1p = new ImageIcon("Icons/WhiteplusPowerball.png");
        ImageIcon w2P = new ImageIcon("Icons/2WhitePlusPowerball.png");
        ImageIcon w3P = new ImageIcon("Icons/3WhitePlusPowerball.png");
        ImageIcon w4P = new ImageIcon("Icons/4WhitePlusPowerball.png");
        ImageIcon w5P = new ImageIcon("Icons/5WhitePlusPowerball.png");
        ImageIcon w3 = new ImageIcon("Icons/3White.png");
        ImageIcon w4 = new ImageIcon("Icons/4White.png");
        ImageIcon w5 = new ImageIcon("Icons/5White.png");

        table.setValueAt(p, 0, 0);
        table.setValueAt(w1p, 1, 0);
        table.setValueAt(w2P, 2, 0);
        table.setValueAt(w3, 3, 0);
        table.setValueAt(w3P, 4, 0);
        table.setValueAt(w4, 5, 0);
        table.setValueAt(w4P, 6, 0);
        table.setValueAt(w5, 7, 0);
        table.setValueAt(w5P, 8, 0);

        table.setValueAt("1 in 55", 0, 1);
        table.setValueAt("1 in 111", 1, 1);
        table.setValueAt("1 in 706", 2, 1);
        table.setValueAt("1 in 360", 3, 1);
        table.setValueAt("1 in 12,245", 4, 1);
        table.setValueAt("1 in 19,088", 5, 1);
        table.setValueAt("1 in 648,976", 6,1);
        table.setValueAt("1 in 5,153,633", 7, 1);
        table.setValueAt("1 in 175,223,510", 8, 1);

        for(int i = 0; i < 10; i++)
        {
            table.setRowHeight(i, table.getRowHeight(i) + 10);
        }

        for(int i = 0; i < columnNames.length; i++)
        {
            TableColumn column = table.getColumnModel().getColumn(i);
            column.setPreferredWidth(column.getPreferredWidth() + 80);
        }
    }

Here is the main class with the JPanel that is attempting to display my LotteryTable.

public class LotteryDisplay
{
private JFrame display;
private JPanel panel;
private LotteryTable table;
private static JCheckBox powerPlay;

public LotteryDisplay()
{
    display = new JFrame("Powerball");
    panel = new JPanel();
    table = new LotteryTable();
    powerPlay = new JCheckBox("Power Play");
    panel.setPreferredSize(new Dimension(800, 300));

    panel.add(powerPlay);

    //JTableHeader header = table.getTableHeader(); //Not important to problem
    //panel.add(header);

    panel.add(table);

    display.getContentPane().add(panel, BorderLayout.CENTER);
    display.pack();
    display.setVisible(true);
}

Upvotes: 1

Views: 5786

Answers (3)

jramoyo
jramoyo

Reputation: 336

Whatever you're trying to do with the inner JTable, you should be doing on the parent LotteryTable.

For illustration purposes, I replaced the reference to table with this:

public class LotteryTable extends JTable {
    private static final String[] COLUMN_NAMES = { 
                                    "Powerball Combos",
                                    "Odds of Winning", 
                                    "Payout", 
                                    "Number of Wins", 
                                    "Win Frequency" };

    public LotteryTable() {
        super(new DefaultTableModel(COLUMN_NAMES, 9));
        tableSetup();
        setVisible(true);
    }

    @Override
    public Class<?> getColumnClass(int column) {
        if (column == 0)
            return ImageIcon.class;
        else
            return String.class;
    }

    private void tableSetup() {
        DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer();
        dtcr.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
        this.setDefaultRenderer(String.class, dtcr);

        ImageIcon p = new ImageIcon("Icons/Powerball.png");
        ImageIcon w1p = new ImageIcon("Icons/WhiteplusPowerball.png");
        ImageIcon w2P = new ImageIcon("Icons/2WhitePlusPowerball.png");
        ImageIcon w3P = new ImageIcon("Icons/3WhitePlusPowerball.png");
        ImageIcon w4P = new ImageIcon("Icons/4WhitePlusPowerball.png");
        ImageIcon w5P = new ImageIcon("Icons/5WhitePlusPowerball.png");
        ImageIcon w3 = new ImageIcon("Icons/3White.png");
        ImageIcon w4 = new ImageIcon("Icons/4White.png");
        ImageIcon w5 = new ImageIcon("Icons/5White.png");

        this.setValueAt(p, 0, 0);
        this.setValueAt(w1p, 1, 0);
        this.setValueAt(w2P, 2, 0);
        this.setValueAt(w3, 3, 0);
        this.setValueAt(w3P, 4, 0);
        this.setValueAt(w4, 5, 0);
        this.setValueAt(w4P, 6, 0);
        this.setValueAt(w5, 7, 0);
        this.setValueAt(w5P, 8, 0);

        this.setValueAt("1 in 55", 0, 1);
        this.setValueAt("1 in 111", 1, 1);
        this.setValueAt("1 in 706", 2, 1);
        this.setValueAt("1 in 360", 3, 1);
        this.setValueAt("1 in 12,245", 4, 1);
        this.setValueAt("1 in 19,088", 5, 1);
        this.setValueAt("1 in 648,976", 6, 1);
        this.setValueAt("1 in 5,153,633", 7, 1);
        this.setValueAt("1 in 175,223,510", 8, 1);

        for (int i = 0; i < 10; i++) {
            this.setRowHeight(i, this.getRowHeight(i) + 10);
        }

        for (int i = 0; i < COLUMN_NAMES.length; i++) {
            TableColumn column = this.getColumnModel().getColumn(i);
            column.setPreferredWidth(column.getPreferredWidth() + 80);
        }
    }
}

Upvotes: 1

MadProgrammer
MadProgrammer

Reputation: 347194

LotteryTable extends JTable, but then creates another JTable internally to itself which is never added to the any component capable of displaying it.

As to your second question...this arguable. I see no particular benefit in extending a JTable for the functionality you are adding. This could just as easily be achieved using a static configuration method which is passed a JTable

Working Example

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

public class LotteryTable {

    public static void main(String[] args) {
        new LotteryTable();
    }

    public LotteryTable() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JTable table = new JTable();
                configureLotteryTable(table);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new JScrollPane(table));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public static final String[] COLUMN_NAMES = {"Powerball Combos", "Odds of Winning", "Payout", "Number of Wins", "Win Frequency"};

    public static void configureLotteryTable(JTable table) {
        DefaultTableModel model = new DefaultTableModel(COLUMN_NAMES, 0) {
            @Override
            public Class<?> getColumnClass(int columnIndex) {
                Class clazz = String.class;
                switch (columnIndex) {
                    case 0:
                        clazz = ImageIcon.class;
                        break;
                    default:
                        clazz = String.class;
                        break;
                }
                return clazz;
            }            
        };
        table.setModel(model);

        model.addRow(new Object[]{new ImageIcon(LotteryTable.class.getResource("/icons/Powerball.png")), "1 in 55"});
        model.addRow(new Object[]{new ImageIcon(LotteryTable.class.getResource("/icons/Powerball.png")), "1 in 111"});
        model.addRow(new Object[]{new ImageIcon(LotteryTable.class.getResource("/icons/Powerball.png")), "1 in 706"});
        model.addRow(new Object[]{new ImageIcon(LotteryTable.class.getResource("/icons/Powerball.png")), "1 in 360"});
        model.addRow(new Object[]{new ImageIcon(LotteryTable.class.getResource("/icons/Powerball.png")), "1 in 12,245"});
        model.addRow(new Object[]{new ImageIcon(LotteryTable.class.getResource("/icons/Powerball.png")), "1 in 19,088"});
        model.addRow(new Object[]{new ImageIcon(LotteryTable.class.getResource("/icons/Powerball.png")), "1 in 648,976"});
        model.addRow(new Object[]{new ImageIcon(LotteryTable.class.getResource("/icons/Powerball.png")), "1 in 5,153,633"});
        model.addRow(new Object[]{new ImageIcon(LotteryTable.class.getResource("/icons/Powerball.png")), "1 in 175,223,510"});
    }
}

I would spend some more time reading through How to use Tables to freshen up on some of the concepts ;)

Upvotes: 4

SaintLike
SaintLike

Reputation: 9579

I'd rather use a JFrame or JDialogand place a JPanel to display your JTable.

Upvotes: 0

Related Questions