Search code examples
javaswingcomponentsjscrollpanejtabbedpane

Java. Cant add JPanel in JScrolledPane


I'm adding JTabbedPane to JFrame.

Each tab contains a different bgPanel (JPanel with drawn image as background), each bgPanel contains JScrollPane, and each JScrollPane contains a JPane (nowyPanel).

bgPanels are only a background. Each JScrollPane has setOpaque(false); and getViewport().setOpaque(false);. Each JPanel nowyPanel has setOpaque(false); only.

I can see the background and JScrollPanels but no JPanels. I Checked it and it seems that content is correctly added there. The problem is with JPanels which doesn't show up.

Here is some code:

static class JBackgroundPanel extends JPanel {

    private BufferedImage img;

    JBackgroundPanel() {
        try {
            img = ImageIO.read(new File("2.jpg"));
        } catch (IOException e) {
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
    }
}

//method to add components to element of array nowyPanel[] JPanel
//i[a] and o are set earlier and they are fine
static void skrotyDoPaneli() {
    int a, b;
    for (a = 0; a < o; a++) {
        for (b = 0; b < i[a]; b++) {
            nowyPanel[a].add(new dodanieIkony(ikonki[a][b], podpisyIkonek[a][b], listaOdnosnikow[a][b]));
            nowyPanel[a].repaint();
            nowyPanel[a].revalidate();
        }
    }
}

//set properties for all panels 
static void ladowaniePaneli() {
    int b;
    Dimension osiemsetnaszescset = new Dimension(800, 600);
    for (b = 0; b < o; b++) {
        bgPanel[b] = new JBackgroundPanel();
        vertical[b] = new JScrollPane();
        nowyPanel[b] = new JPanel();

        ((FlowLayout) bgPanel[b].getLayout()).setVgap(0);
        bgPanel[b].setPreferredSize(osiemsetnaszescset);
        nowyPanel[b].setPreferredSize(osiemsetnaszescset);
        vertical[b].setPreferredSize(new Dimension(789, 517));
        vertical[b].setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

        vertical[b].setOpaque(false);
        nowyPanel[b].setOpaque(false);
        vertical[b].getViewport().setOpaque(false);
        vertical[b].add(nowyPanel[b]);
        bgPanel[b].add(vertical[b]);
        vertical[b].add(nowyPanel[b]);
    }
}
//each element of bgPanel array is set as individual tab
static JTabbedPane tabbedPane() {
    JTabbedPane panelZakladek = new JTabbedPane();
    int j;
    for (j = 0; j < o; j++) {
        panelZakladek.addTab(nazwyGrup[j], bgPanel[j]);
    }
    return panelZakladek;
}

//does everything what needs to be done before JFrame shows up
static void przedStartem() throws FileNotFoundException, IOException {
    odczyt();
    odczytPaneli();
    ladowaniePaneli();
    skrotyDoPaneli();
}

//does przedStartem() and then initialize JFrame with content
public static void main(String[] args) throws FileNotFoundException, IOException {
    przedStartem();
    //przywolanie do zycia okna glownego JFrame
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            glowneOkno okno = new glowneOkno();
            okno.setVisible(true);
        }
    });
}
//Variables Declaration
static int o = 0;
static int[] i = new int[1000];
static JBackgroundPanel[] bgPanel = new JBackgroundPanel[1000];
static JScrollPane[] vertical = new JScrollPane[1000];
static JPanel[] nowyPanel = new JPanel[1000];
static FileSystemView fsv = FileSystemView.getFileSystemView();
static String[] nazwyGrup = new String[1000];
static File plikZGrupami = new File("grupy.txt");
static String[][] podpisyIkonek = new String[1000][1000];
static Icon[][] ikonki = new Icon[1000][1000];
static String[][] listaOdnosnikow = new String[1000][1000];
static File[][] listaPlikow = new File[1000][1000];

Solution

  • bgPanel[b].add(vertical[b]);
    vertical[b].add(nowyPanel[b]);
    

    Don't add components to a scrollpane. Instead the code should be:

    vertical[b].setViewportView(nowyPanel[b]);