0

I'm trying to create a JPanel in the size of the screen (It might be bigger than the JFrame) But I want the JFrame to be resizable. is it possible to see the hidden part of the JPanel (The part outside the JFrame) with scrolling (JScrollPane)?

Its my first Java project so please explain your answers

Example in html

<html>
<body> <!-- This is the JFrame -->
  <div style="width: 120%; height: 200%;"> <!-- This is the JPanel -->

  </div>
</body>
</html>

Paint Example Paint Example

  • If you need an example you can look at Paint when the window is not full sized – user15329314 Mar 04 '21 at 14:28
  • So, from what I understand you want a scrollable `JPanel` inside a resizable `JPanel`? If so, [check this answer](https://stackoverflow.com/a/39480214/2180785) – Frakcool Mar 04 '21 at 14:57
  • Take a look at his [StackOverflow answer](https://stackoverflow.com/questions/66263438/paintcomponent-not-called-when-i-scroll-both-horizontal-and-vertical-scrollbars/66267763#66267763) showing a scrollable drawing JPanel. – Gilbert Le Blanc Mar 04 '21 at 14:58
  • 1
    *Its my first Java project* - learn Swing basics by reading the [Swing Tutorial](https://docs.oracle.com/javase/tutorial/uiswing/TOC.html) *please explain your answers* - explain your question better by posting a [mre] demonstrating what you have tried. – camickr Mar 04 '21 at 15:06
  • "So, from what I understand you want a scrollable JPanel inside a resizable JPanel? If so," No i want a scrollable JPanel inside a resizable JFrame. But thanks for the links – user15329314 Mar 04 '21 at 15:12
  • "showing a scrollable drawing JPanel" I already created the drawing part – user15329314 Mar 04 '21 at 15:13
  • English is not my main language so its hard to write the question better but did u look at Paint behavior when the window is not full? – user15329314 Mar 04 '21 at 15:15
  • I know how to create a scrollable JPanel but I want it to be bigger than the JFrame – user15329314 Mar 04 '21 at 15:17
  • You override the `getPreferredSize()` method of your drawing panel. Read the Swing tutorial. There is a section on "Custom Painting" that contains a working example of how to do custom painting. You just adjust your preferred size. – camickr Mar 04 '21 at 15:56
  • And should I remove the setPreferdSize of the JSrollPane? – user15329314 Mar 04 '21 at 16:41
  • @Override public Dimension getPreferredSize() { return new Dimension(1000, 1000); } – user15329314 Mar 04 '21 at 16:42
  • That is in the JPanel extended class and the scroll pane is JScrollPane scroll = new JScrollPane(canvas, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); //scroll.setPreferredSize(new Dimension(600,600)); – user15329314 Mar 04 '21 at 16:43
  • canvas is the JPanel – user15329314 Mar 04 '21 at 16:43

1 Answers1

0

I found a way to do that if anyone else needs you need to @Override getPreferredSize in the JPanel and DONT add the JPanel to the JFrame create a JScrollPane with the JPanel and add the JScrollPane to the JFrame (I think my problem was that i added the JPanel twice)

EXAMPLE -- In extended JFrame class

JPanel panel = new JPanel();
panel.setPreferredSize(**SIZE**); // not tested I use @Override on the getPreferredSize - idea from camickr
JScrollPane scroll = new JScrollPane(panel, 
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

this.add(scroll);

Thanks for the help