0

so my problem is quite bizarre. So I am trying to write a java program in the eclipse ide. I have one JFrame and multiple JPanels added to it. You can navigate through the JPanels via buttons setting one panel to not be visible and another to be visible, you get the idea. However, after some time this started to occur: when I press a button the correct JPanel is being set to visible and I can see it, but it is completely empty. Sometimes its content is displayed correctly, but this is rather inconsistent.

As I said: sometimes this happens, sometimes it doesnt happen. I would start the program and close it 5 times and the 6th time it finally works. I don't think the code is necessary (as it is all correct because, after all, it does work sometimes), but if you want to see it feel free to ask. My eclipse workspace is on my C drive on an SSD and it is not too full. it has like 100 out of 250 gigs left. Thank you for your time and help in advance!

Edit: So heres the code example. Two of the JPanels Im trying to switch between: The "main" panel

Var.menuPanel = new JPanel();
        Var.jf1.add(Var.menuPanel);
        Var.menuPanel.setBounds(0, 0, Var.screenwidth, Var.screenheight);
        Var.menuPanel.setLayout(null);
        Var.menuPanel.setVisible(true);
        Var.menuPanel.revalidate();
        Var.menuPanel.repaint();

This loads all fine. The button to switch to the other panel:

Var.editCourse = new JButton("Edit");
        Var.menuPanel.add(Var.editCourse);
        Var.editCourse.setBounds(550, 120, 200, 50);
        Var.editCourse.setVisible(true);
        Var.editCourse.addActionListener(handler);
        Var.editCourse.setBackground(Color.YELLOW);

The other JPanel + UI elements

Var.editCoursePanel = new JPanel();
        Var.jf1.add(Var.editCoursePanel);
        Var.editCoursePanel.setBounds(0, 0, Var.screenwidth, Var.screenheight);
        Var.editCoursePanel.setLayout(null);
        Var.editCoursePanel.setVisible(false);
        Var.editCoursePanel.revalidate();
        Var.editCoursePanel.repaint();
        
        Var.editCourseToMenu = new JButton("Back");
        Var.editCoursePanel.add(Var.editCourseToMenu);
        Var.editCourseToMenu.setVisible(true);
        Var.editCourseToMenu.setBounds(550, 400, 200, 50);
        Var.editCourseToMenu.addActionListener(handler);
        
        Var.editCourseSelection = new JComboBox<String>(JSONHandler.courses);
        Var.editCoursePanel.add(Var.editCourseSelection);
        Var.editCourseSelection.setBounds(550, 200, 200, 50);
        Var.editCourseSelection.setVisible(true);
        Methods.setArrowBounds(Var.editCourseSelection);
        Var.editCourseSelection.setSelectedItem("Edit.");
        
        Var.submitCourse = new JButton("Continue");
        Var.editCoursePanel.add(Var.submitCourse);
        Var.submitCourse.setBounds(550, 300, 200, 50);
        Var.submitCourse.setVisible(true);
        Var.submitCourse.addActionListener(handler);

The line in the AcionHandler:

else if(e.getSource() == Var.editCourse) {
        MenuHandler.showEditCoursePanel();
}

The showEditCoursePanel methtod:

public static void showEditCoursePanel() {
        
        Var.menuPanel.setVisible(false);
        Var.editCoursePanel.setVisible(true);
        
}

Oh and also, sometimes when it doesnt load, there is the scrollbar of a JScrollPane on the side, which, as you can see, is never even mentioned.

Edit 2: Okay, I found the source of the problem. When I add the raw JPanel to the JFrame and workt with that it all works fine, I cant scroll of course. But when I add the JScrollPane the newCoursePanel doesnt show up at all. Everything else is now working fluently, except for the panel with the scrollbar. Heres the code form the nwCoursePanel im talking about:

Var.newCoursePanel = new JPanel();
        Var.newCoursePanel.setBounds(0, 0, Var.screenwidth, Var.screenheight);
        Var.newCoursePanel.setLayout(null);
        Var.newCoursePanel.revalidate();
        Var.newCoursePanel.repaint();
        Var.newCoursePane = new JScrollPane(Var.newCoursePanel);
        Var.newCoursePane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        Var.newCoursePane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        Var.newCoursePane.setVisible(false);
        Var.jf1.add(Var.newCoursePane);

I cant figure out how to get it working though. in to show the panel I just Var.menuPanel.setVisible(false) and Var.newCoursePane.setVisible(true).

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
jw-de
  • 39
  • 4
  • 1
    "...as it is all correct because, after all, it does work sometimes..." Code that works sometimes is not correct. Possibly a timing issue. Generally, you create one JFrame and display multiple JPanels, one at a time, using a CardLayout. You create all the Swing components and panels first, then use them. Edit your question to include a [mre] that we can copy into our IDE, run, and test. – Gilbert Le Blanc Oct 18 '21 at 17:18
  • Done, sorry it took so long! – jw-de Oct 18 '21 at 19:49
  • 1
    I can't tell with the limited amount of code you're posting, but a `JScrollPane` **must** be added to the CENTER of a `JPanel` with a `BorderLayout`. A [mre] would include code to create the `JFrame` and create all the `JPanels`, just to get started. The Oracle tutorial. [Creating a GUI With Swing](https://docs.oracle.com/javase/tutorial/uiswing/index.html) will take you through all the steps to create a working Swing GUI. Skip the Netbeans section. – Gilbert Le Blanc Oct 20 '21 at 14:06
  • Okay, it appears I got it working now. The issue(s) were that, when creating the scroll pane, I didnt do setVisible(true) with it. Also I added BorderLayout.CENTER when adding it to the JFrame. So now i set the panel itself visible and not visible rather than the scroll pane and it works! Thank you for your help! Oh and sorry I failed to provide the minimal reproducible code example, Ill do better next time! – jw-de Oct 20 '21 at 17:42
  • *"my problem is quite bizarre."* I find it quite bizarre that the first `?` on this page (before I post this comment) is below the comment section. *What is your question?* – Andrew Thompson Nov 29 '21 at 09:40

0 Answers0