-1

In my code i transfer the JPanel (Bestellpanel) from frame to frame1. After that, everytime i use the frame1 scrollbar it repaints frame1and my JPanel (Bestellpanel) is gone. That means I need a way to stop my JPanel getting overpainted. I read something about super.paint(); and other methods but I have major problems understanding them.

Here is a code example of my problem:

 import java.awt.Color;
 import javax.swing.JPanel;
 import java.awt.Container;
 import java.awt.Dimension;
 import java.awt.Insets;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import javax.swing.JButton;
 import javax.swing.JFrame;
 import javax.swing.JScrollPane;
 import javax.swing.ScrollPaneConstants;


public class weqe {


private static JFrame frame = new JFrame("First Frame");
private static JFrame frame1 = new JFrame("Second Frame");
private static JPanel Bestellpanel = new JPanel();

private static int kunde = 1;


public static void addComponentsToPane(final Container pane) {

    pane.setLayout(null);
    final Insets insets1 = pane.getInsets();    
    // Mitn Button

    JButton MitnIcon = new JButton("Mitnehmen");
            MitnIcon.setFocusPainted(false);
            MitnIcon.setVisible(true); 

    Dimension size2 = MitnIcon.getPreferredSize();
            MitnIcon.setBounds(1010 + insets1.left, 700 + insets1.top,
                    size2.width + 27, size2.height + 50);

    pane.add(MitnIcon);

    MitnIcon.addActionListener(new ActionListener(){

    public void actionPerformed(ActionEvent e) {

            if (kunde == 1) {

                frame.getContentPane().remove(Bestellpanel);
                Bestellpanel.setLocation(0, 0);
                frame1.getContentPane().add(Bestellpanel);  
                Bestellpanel.repaint();
                frame.repaint();


                    }

        }});

    // ScrollPane           

    JPanel panel1 = new JPanel();
    panel1.setPreferredSize(new Dimension(2000,800));
    panel1.setVisible(false);    


    JScrollPane scrollPane = new JScrollPane (panel1,
                ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
     frame1.add(scrollPane);

    Bestellpanel.setBounds(930 + insets1.left, 50 + insets1.top,size2.width 
    + 30, size2.height + 400);    

    Bestellpanel.setVisible(true);pane.add(Bestellpanel);
    Bestellpanel.setBackground(Color.green);     

        }

   private static void createAndShowGUI() {

        //Create and set up the window.

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     

        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     
        addComponentsToPane(frame.getContentPane());



        //Size and display the window.
        Insets insets = frame.getInsets();
        Insets insets1 = frame1.getInsets();
        frame.setSize(1200 + insets.left + insets.right,
                      900 + insets.top + insets.bottom);
        frame.setVisible(true);
        frame1.setSize(800 + insets1.left + insets1.right,
                600 + insets1.top + insets1.bottom);
        frame1.setVisible(true);

        frame.add(Bestellpanel);


    }


    public static void main(String[] args) {

    javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    createAndShowGUI();
    }
    });
    }

    }
Jennifer96
  • 55
  • 1
  • 8
  • Perhaps you would like to [fix your indentation](https://stackoverflow.com/posts/44369357/edit) so your code is easier to read. – khelwood Jun 05 '17 at 12:51

1 Answers1

2
  1. meinJDialog.setSize(800,800); and panel1.setPreferredSize(new Dimension(2000,800)); most likely are part of your problem, see Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? (The general consensus says yes and to override getPreferred|Maximum|MinimumSize() methods instead)

  2. Instead of removing/adding the JComponents yourself, try out Card Layout

  3. You don't need to manually change component's visibility, again, check the link in point number 2, for this line: Bestellpanel2.setVisible(true);

  4. Please follow the Java naming conventions: FirstWordUpperCaseClass, firstWordLowerCaseVariable, firstWordLowerCaseMethod() and ALL_WORDS_UPPER_CASE_CONSTANT), so, your code is easier to read and understand for you and for us.

If all the above points don't work, then consider posting a valid Minimal, Complete and Verifiable Example (MCVE) or Short, Self Contained, Correct Example (SSCCE) that demonstrates your issue, has no external dependencies or customizations such as background color / image, etc. It should be indented correctly, as said in the comments above.

Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • Thanks for the help Frakcool. So this means my panels disappear when i use Scrollbar because of the JDialog and Panel1 Size configurations? – Jennifer96 Jun 05 '17 at 14:07
  • I didn't say that, I said that they are *part* of it. We can't see the error in your code because it's not complete. Please take a look at the [mcve] page and post one. `Bestellpanel` We don't know how that class looks, create a whole new program that's simple enough to show your problem without custom classes if not needed but that's short enough to be posted here. Note that we don't want your whole program or code snippets, we want a runnable example that shows your problem – Frakcool Jun 05 '17 at 14:11
  • Nevermind I already saw that `Bestellpanel` is a `JPanel`, see? I got confused and that's why you need to indent your code correctly *AND* follow naming conventions – Frakcool Jun 05 '17 at 14:16
  • Ok i will try that :) – Jennifer96 Jun 05 '17 at 14:32
  • If you post the MCVE just comment here again and I'll check it out ^^ – Frakcool Jun 05 '17 at 14:36
  • I edited my post, i hope it's better that way. I gave my best. :) – Jennifer96 Jun 05 '17 at 20:29
  • Well i got it o0 And i dont no exactly how i did it ... I changed some pane stuff. Ok the moment i got it i will post the answer. Well that was luck... Funny how your suggestion to write it down more simple helped me so much^^ – Jennifer96 Jun 05 '17 at 21:27
  • That's why we ask you to create a [mcve], it narrows the problem down to the essential and you can focus on the problematic code and use the knowledge that this example provides to use it later in your application. I'm glad I was of help, and sorry, I was in the way back home when you replied, but nice you got it! – Frakcool Jun 05 '17 at 21:32
  • Ok here is how i fixed it: I made a new `JPanel` and addet it to the `frame1`. Also i addet `.setLayout(null);` to the new `JPanel`. Then in the `ActionListener` changed the transfer of `JPanel` (Bestellpanel) from `frame1` to the new `Jpanel`. At last i addet `panel1.setVisible(false);` and `panel1.setVisible(true)` to the `Actionlistener`. To understand how JScrollPane works did the Magic and ofc not missing `.setLayout(null);`^^ Now i can scroll as much as i want and the transferd `JPanel` still shows. Thanks for your help Frakcool :) – Jennifer96 Jun 05 '17 at 22:32