将扩展JTable添加到JFrame

时间:2015-05-17 11:11:38

标签: java swing jtable jframe imageicon

我想做什么:

我创建了一个扩展JTable的表类。我的目标是在获得用户的输入后显示在互联网上找到的图像(目前我只是使用硬编码的URL进行测试)。一列应该是ImageIcon类,第二列是String。当使用Test类时,JTable(未扩展)被正确绘制,hovewer我根本无法显示扩展的。我想我在TeamTable类中缺少一些东西,但是我无法弄清楚它是什么,为什么Test类(位于帖子末尾)实际上有效。

主要问题:

为什么Test.java可以使用ImageIcon正确显示JTable,但我的RoundRobin.java + TeamTable.java根本不显示任何内容?

代码:

from __future__ import unicode_literals 

这是扩展的JTable类,我正在创建一个新的JTable,覆盖getColumnClass方法并使用从主类中的用户收集的输入设置两个列。

public class TeamTable extends JTable{
    public static final String[] columnNames = {"Team Logo", "Team"};
    public static final Object[][] data = new Object[1][2];     // For test purposes set to [1][2], final value [8][2]

    public TeamTable(){
        JTable table = new JTable();
        configureTeamTable(table);      // Override the getColumnClass, set the model
    }                                   // Loop through all teams and assing the names and logos

    public static void configureTeamTable(JTable table){
        DefaultTableModel model = new DefaultTableModel(data, columnNames){
            @Override
            public Class<?> getColumnClass(int column){
                return getValueAt(0, column).getClass();
            }
        };
        table.setModel(model);
        table.setRowHeight(50);
        for(int i = 0 ; i < 1 ; i ++){   // For test purposes set to 1, final value 8
            ImageIcon currentTeamLogo = new ImageIcon(RoundRobin.getTeam(i).getLogo());
            String currentTeamName = RoundRobin.getTeam(i).getName();
            data[i][0] = currentTeamLogo;
            data[i][1] = currentTeamName;
        }        
    }
}

这是Team类,目前它总是搜索相同的图像,并且可以将其大小调整为30x30px大小。主要方法如下:

public class Team {
    private String name;
    private Image logo;
    private ArrayList<String> players;


    public Team(String name){
        this.name = name;
    }
    public Team(){}

    public void searchImg(){
        try{
            URL url = new URL("http://www.wearjersey.com/product_images/uploaded_images/milanlogo.jpg");
            this.logo = ImageIO.read(url);
        }catch(IOException e){
            System.out.println("IOe");
        }
    }

    public void resizeLogo(){
        Image tempImg = this.getLogo();
        tempImg = tempImg.getScaledInstance(30,30, java.awt.Image.SCALE_SMOOTH);
        setLogo(tempImg);
    }
} //only GETTERS and SETTERS below, removed to keep the code shorter

==============工作的Test类,Test.java和tje TeamTable.java之间的主要区别是什么? Test.java正确显示JFrame。

public class RoundRobin {

    private JFrame frame;
    private static ArrayList<Team> teams;
    private static int teamCounter = 0;

    final int HEIGHT = 400; //1024
    final int LENGTH = 600; //1920


    public static void main(String[] args) {
        RoundRobin rr = new RoundRobin();
        rr.paintStuff();
        rr.initFirstUI();
    }


    public void paintStuff() {
        frame = new JFrame();

        frame.setSize(LENGTH, HEIGHT);
        frame.getContentPane().setLayout(new MigLayout());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setVisible(true);
    }                                              // initiates, sets size etc.

    public void initFirstUI() {                                                  // creates the first part of the UI, which will later be replaced by group ladder
        teams = new ArrayList();
        frame.add(new JLabel("Teams"), "wrap, align center");
        JButton addTeamBut = new JButton("Add a team");
        frame.add(addTeamBut);
        addTeamBut.addActionListener(new AddTeamAct());

        refresh();
    }


    public void refresh() {                                                      // just a revaildate + refresh method
        frame.revalidate();
        frame.repaint();
    }

    public static Team getTeam(int index) {
        return teams.get(index);
    }


    class AddTeamAct implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent ae) {
            if(teamCounter < 1){    // For test purposes set to 1, final value 8
                String name = JOptionPane.showInputDialog("Name of the team");

                Team currentTeam = new Team(name);
                currentTeam.searchImg();
                currentTeam.resizeLogo();

                teams.add(currentTeam);
                teamCounter++;
            }
            if(teamCounter == 1){   // For test purposes set to 1, final value 8
                TeamTable a = new TeamTable();
                frame.add(a);
            }
            refresh();   
        }
    }
}

感谢您的任何见解。

1 个答案:

答案 0 :(得分:4)

TeamTable扩展了JTable并且有一个JTable的实例。这可能就是问题所在。

顺便说一句,代码试图实现的内容似乎更好地在工厂方法中实现,而不是扩展JTable

相关问题