webForumDet fria alternativet

JTable

1 svar · 483 visningar · startad av Asa

AsaMedlem sedan dec. 20011 934 inlägg
#1

Har jag gjort fel? Den skall via en JMenu anropa denna funktionen i min lyssnare och öppna det i en ny ruta. Alltså det som ska visas är en JTable i en JFrame.

    private JPanel getFileListPanel()
    {
        fileListPanel = new JPanel();
        fileListPanel.setName("Visa allt i en tabell");
        fileListPanel.setLayout(new BorderLayout());
        fileListPanel.setBorder(new EtchedBorder());
    
        String[] columnNames = { "Path","File Name","File Size" };
        Object[][] rowData = {
            { "a","b","c" }
            ,
            { "d","e","f" }
        };
    
        fileList = new JTable(rowData, columnNames);
        fileList.setName("Visa allt i en tabell");
        fileListPanel.add(fileList, BorderLayout.CENTER);
    
        return fileListPanel;
    }
AsaMedlem sedan dec. 20011 934 inlägg
#2

Jag gjorde så här:

    private void getFileListPanel()
    {
        JFrame tabell = new JFrame("Visa bilarna i tabell");
        //tabell.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        fileListPanel = new JPanel();
        fileListPanel.setName("Visa allt i en tabell");
        fileListPanel.setLayout(new BorderLayout());
        fileListPanel.setBorder(new EtchedBorder());
    
        String[] columnNames = { "Regnr","Märke","Modell","Årsmodell","BildURL" };
        Object[][] rowData = new Object[bilDB.antalBilar()][columnNames.length];
        for(int i = 0; i<bilDB.antalBilar(); i++){
            Bil c = bilDB.getBil(i);
            for (int j = 0; j<c.getBilar().length; j++){
                rowData[i][j] = c.getBilar()[j];
            }
        }
        
        fileList = new JTable(rowData, columnNames);
        fileList.setName("Visa allt i en tabell");
        fileListPanel.add(fileList);
        tabell.getContentPane().add(fileListPanel);
        tabell.setLocation(300, 150);
        tabell.pack();
        tabell.setVisible(true);
    }

Sen anropar jag den med denna i samma fil:

getFileListPanel();
Genererad på 358 ms · cache AV · v20260730165559-full.f96bc7eb