0

What I want to do:

I have created a table class that extends the JTable. My aim is to display an image found in the Internet after obtaining input from the user (at the moment I'm just using a hardcoded URL for testing). One column is supposed to be ImageIcon class, the second one is String. When using a Test class, the JTable (not extended) is drawn correctly, hovewer I cannot get the extended one to display at all. I think I'm missing something in the TeamTable class, but I can't figure out what is it, and why does the Test class (located in the end of the post) actually work.

The main question:

Why can Test.java properly display the JTable with ImageIcon, but my RoundRobin.java + TeamTable.java does not display anything at all?

The code :

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

This is the extended JTable class, I'm creating a new JTable, overriding the getColumnClass method and setting both columns using the input gathered from the user in the main class.

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

This is the Team class, at the moment it always searches for the same image, and can resize it to 30x30px size. The main method is below:

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

============== The working Test class, what is the main difference between Test.java and tje TeamTable.java? The Test.java properly displays the 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;
    }
}

Thanks for any insight.

Community
  • 1
  • 1
  • 1
    1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. 3) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). 4) `TeamTable` both extends `JTable` and has an instance. That might well be the problem. Why ***are*** you extending table? – Andrew Thompson May 17 '15 at 11:15
  • @HovercraftFullOfEels *"At least your images are available online"* ..LOL! I completely failed to notice that. OP: Ignore the 3rd point in my comment above, since the code *already does that.* :P – Andrew Thompson May 17 '15 at 11:58

1 Answers1

4

TeamTable both extends JTable and has an instance of a JTable. That might well be the problem.

As an aside, what the code is trying to achieve seems better implemented in a factory method than by extending JTable.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433