【问题标题】:Adding extended JTable to JFrame将扩展的 JTable 添加到 JFrame
【发布时间】:2015-05-17 11:11:38
【问题描述】:

我想做的事:

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

主要问题:

为什么Test.java可以正确显示带有ImageIcon的JTable,而我的RoundRobin.java + TeamTable.java却什么都没有显示?

代码:

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;
        }        
    }
}

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

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

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

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();   
        }
    }
}

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

public class Test {
        private JFrame frame;
        private JTable teamTable;
        private Image logo;
        
    public static void main(String[] args) {
        Test test = new Test();
        test.paintStuff();
        test.addTab();
    }
    
    
    
    
    public void paintStuff(){
        frame = new JFrame();

        frame.setSize(800, 600);
        frame.getContentPane().setLayout(new MigLayout());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setVisible(true);
    }        
    
    
    public void addTab(){
        searchImg();
        resizeLogo();
        ImageIcon a = new ImageIcon(logo);
        String[] columnNames = {"Team"};
        Object[][] data = new ImageIcon[8][1];
        data[0][0] = a;
        for(int i = 0 ; i < 8 ; i ++){
            data[i][0] = a;
        }        
        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        
        teamTable = new JTable(model){
            public Class getColumnClass(int column){
                return getValueAt(0, column).getClass();
            }
        };
        
        teamTable.setRowHeight(50);
        
        frame.add(teamTable);
        frame.revalidate();
        frame.repaint();
    }
    
    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("IOException");
        }
    }
        
    public void resizeLogo(){
        Image tempImg = this.logo;
        tempImg = tempImg.getScaledInstance(30,30, java.awt.Image.SCALE_SMOOTH);
        logo = tempImg;
    }
}

感谢您的任何见解。

【问题讨论】:

  • 1) 为了尽快获得更好的帮助,请发布MCVE(最小完整可验证示例)或SSCCE(简短、自包含、正确示例)。 2) 曾经只需要源代码中的一个空白行。 { 之后或} 之前的空行通常也是多余的。 3) 例如,获取图像的一种方法是热链接到在this Q&A 中看到的图像。 4)TeamTable 都扩展了JTable 并有一个实例。这很可能是问题所在。为什么你扩展表?
  • @AndrewThompson:非常感谢!
  • @HovercraftFullOfEels “至少你的图片可以在线获取” ..LOL!我完全没有注意到这一点。 OP:忽略我上面评论中的第三点,因为代码已经这样做了。:P

标签: java swing jtable jframe imageicon


【解决方案1】:

TeamTable 都扩展了JTable 并具有JTable 的实例。这很可能是问题所在。

顺便说一句,代码试图实现的目标似乎在​​工厂方法中实现比通过扩展 JTable 更好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-20
    • 1970-01-01
    • 1970-01-01
    • 2012-11-21
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2020-09-29
    相关资源
    最近更新 更多