Search code examples
javawindowsmacosswingjtable

Java JTable not showing on MacOS but on Windows


For an assignment we have to parse a XML File into a JTable. We created a ParserClass that gets the target file from a JFileChooser. After Parsing the XML, our programm hands over the required data into a TableModel for our JTable. We tested the programm on Windows in Eclipse and the JTable is perfectly showing our parsed data.

My Question is: When I try to run the code on MacOS (intellij or Eclipse), the JTable isn't refreshing the TabelModel and so our JTable isn't showing any data. Our code is not yet cleaned but I wanted to make sure why Windows does refresh the TableModel but MacOS doesn't.

GUI Class CODE:

public class GUI extends JFrame {
    
    //Attribute
    private XmlParser myParser = new XmlParser();
        
    //Main-Methode
    public static void main(String[] args)throws SAXException, IOException,
    ParserConfigurationException, TransformerException {
        
        //GUI wird erstellt
        GUI myGui = new GUI();
        myGui.setDefaultCloseOperation(EXIT_ON_CLOSE);;
        myGui.setBounds(100, 100, 606, 437);
        myGui.setTitle("TITEL");
        myGui.setVisible(true);     
    }

    //Konstruktor für die GUI
    public GUI() {
        
        JTable myTable = new JTable(new TableModel());
        myTable.setShowGrid(false);
        
        JPanel contentPane = new JPanel();
        
        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        
        JMenu dateiMenu = new JMenu("Datei");
        menuBar.add(dateiMenu);
        
        JMenuItem neuMenuItem = new JMenuItem("Neu");
        dateiMenu.add(neuMenuItem);
        
        JMenuItem openMenuItem = new JMenuItem("Öffnen");
        openMenuItem.addActionListener(new ActionListener() {
            
        public void actionPerformed(ActionEvent e) {
            JFileChooser openFile = new JFileChooser();
            openFile.addChoosableFileFilter(new FileNameExtensionFilter("Images", "jpg", "png", "gif", "bmp"));
            openFile.addChoosableFileFilter(new FileNameExtensionFilter("*.xml", "xml"));
            openFile.setDialogTitle("Datei auswählen"); 
            int rueckgabeWert = openFile.showOpenDialog(null);  
            
            //Testet, ob Button gedrückt wurde und gibt den Dateinamen dann raus
            if (rueckgabeWert == JFileChooser.APPROVE_OPTION) {
                
                String newXmlFile = openFile.getSelectedFile().getName();
                System.out.println(openFile.getSelectedFile().getName());
                
                //XML wird zum parsen übergeben
                myParser.xmlToDocument(newXmlFile);
                
                //neues TableModel mit idListe für das Initialisieren des data-Arrays(Zeilen der Tabelle)
                TableModel myNewTableModel = new TableModel(myParser.idListe, myParser.mengeListe, myParser.einheitListe, myParser.kurztextListe);

                //übergibt neues TableModel an Tabelle
                myTable.setModel(myNewTableModel);



            } 
        }
        });
        dateiMenu.add(openMenuItem);
        
        JMenuItem saveMenuItem = new JMenuItem("Speichern");
        dateiMenu.add(saveMenuItem);
        
        JMenuItem quitMenuItem = new JMenuItem("Beenden");
        quitMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        dateiMenu.add(quitMenuItem);
        
        //ContentPane + ScrollPane werden erzeugt
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new BorderLayout(0, 0));
        
        JScrollPane scrollPane = new JScrollPane(myTable);
        contentPane.add(scrollPane);
    }
    
    
    
    //TabelModel-Klasse
    public class TableModel extends AbstractTableModel {
        
        private String[] columnNames = {"Ordinalzahl", "Menge", "Einheit", "Kurztext"};
        private Object[][] data = {};
        
        //leerer Konstruktor
        public TableModel() {
        };
        
        //Konstruktor nach dem Parsen
        public TableModel (ArrayList<String> idListe, ArrayList<String> mengeListe, ArrayList<String> einheitListe, ArrayList<String> kurztextListe) {      
            this.data = new Object[idListe.size()][4];  
            
            for (int i = 0; i < idListe.size(); i++) {
                for (int j = 0; j < 4; j++) {
                    if (j==0) {
                        this.data [i][j] = idListe.get(i);
                    }
                    if (j==1) {
                        this.data [i][j] = mengeListe.get(i);
                    }
                    if (j==2) {
                        this.data [i][j] = einheitListe.get(i);
                    }
                    if (j==3) {
                        this.data [i][j] = kurztextListe.get(i);
                    }
                }
            }
            
        }
    
        public int getColumnCount() {
            return columnNames.length;
        }
        
        public int getRowCount(){
            return data.length;
        }
        
        public String getColumnName(int idx) {
            return columnNames[idx];
        }
        
        public Object getValueAt(int row, int col) {
            return data[row][col];
        }
        
        public void setValueAt(Object val, int row, int col) {
            data[row][col] = val;
        }
        
        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }
        
        public boolean isCellEditable(int row, int col) {
            if (col<3)
                return false;
            else 
                return true;
        }
    }   
}

Solution

  • myGui.setVisible(true); 
    

    Should be:

    /* Prompt the JRE to layout the components & size the GUI 
    just large enough to display those components. */
    myGui.pack();
    myGui.setVisible(true); 
    

    Remove:

    myGui.setBounds(100, 100, 606, 437); 
    

    This size is just a guess. The correct size of the GUI is provided by laying out the components according to their preferred size, then calling pack(). See also this answer to Best practice for setting JFrame locations.