开发者

JTable column titles dont show up, instead they are 'A', 'B',

I'm new at using Java GUI and i'm trying to make a table with some scoring results of my soccerteam.

At first I was using the DefaultTableModel and I didnt have any problems with my columnnames. They showed up fine by using the JScrollpane. Then I wanted to try out to implement my own TableModel and since then i don't get to see my table column headers anymore. They show as 'A', 'B', ... , 'G', while I'm still using the ScrollPane. I need this TableModel cause i'm planning on using the exact same structure for adding some other stuff too.

Can anyone help me please trying to solve this issue.

Thanks

class MyTableModel extends AbstractTableModel{  
    private String[] titles;  
    private Object[][] data;

    public MyTableModel(String [] t, Object [][] d){
        this.titles = t;
        this.data   = d;
    }
    public Object getValueAt(int row, int col){
        return data[row][col];
    }
    public int getColumnCount(){
        return titles.length;
    }
    public int getRowCount(){
        return data.length;
    }
}

public static void main(String[] args) {  
    SwingUtilities.invokeLater(new Runnable() {  
        public void run() {  
            createAndShowGUI();  
        }  
    });  
}

public JPanel createContentPane(){
    mainPanel = new JPanel();
    mainPanel.setLayout(new BorderLayout(10, 10));

    String [] titles = {"Naam", "Doelpunten", "% Doelpunten", "Assists", "% Assists", "", "% Totaal"};
    Object [][] data = new Object[spelers.size()][titles.length];
    int counter = 0;
    spelers = getSpelersFromDB("all");
    for (Iterator<Speler&开发者_如何学Cgt; iter = spelers.iterator(); iter.hasNext();){
        Speler temp = iter.next();
        data[counter][0] = temp.naam;
        data[counter][1] = temp.doelpunten;
        data[counter][2] = String.format("%3.1f %%", berekenPercentageDoelpunten(temp));
        data[counter][3] = temp.assists;
        data[counter][4] = String.format("%3.1f %%", berekenPercentageAssists(temp));
        data[counter][5] = "";
        data[counter][6] = String.format("%3.1f %%", berekenTotaalPercentage(temp));
        counter += 1;
    }

    JTable table = new JTable(new MyTableModel(titles, data));

    JScrollPane scrollPane = new JScrollPane(table);
    scrollPane.setPreferredSize(new Dimension(500, 500));
    mainPanel.add(scrollPane, BorderLayout.CENTER);
    mainPanel.setOpaque(true);
    return mainPanel;
}

private static void createAndShowGUI(){
    frame.setMinimumSize(new Dimension(700, 300));
    frame.setPreferredSize(new Dimension(700, 500));
    frame.setMaximumSize(new Dimension(800, 800));

    Voetbal demo = new Voetbal();
    frame.setContentPane(demo.createContentPane());
    frame.setJMenuBar(demo.createJMenuBar());
    frame.setVisible(true);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}


AbstractTableModel will always return 'A', 'B'.... etc as a column name (check the documentation), what you need to do is overwrite the getColumnName() method, with something like:

    public String getColumnName(int column) {
        return titles[column]
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜