Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Want to add Horizontal and vertical scroll bar in my code

 
Parth Pateld
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my code :


I want that Horizontal and vertical scroll bar should be added automatically.
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Why are you using AWT? Swing's been around for more than 10 years now.
2. If you must use AWT, check out the API for ScrollPane.
3. Better, use Swing and follow the links to relevant tutorial from the API for JScrollPane. Also, change your code to do custom painting in an extended JPanel or JComponent, following the guidelines in the tutorial on Performing Custom Painting, and respect Swing's single threaded rule, for which refer to the tutorial on Concurrency in Swing. And don't use getGraphics() of a component.
 
Parth Pateld
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:1. Why are you using AWT? Swing's been around for more than 10 years now.
2. If you must use AWT, check out the API for ScrollPane.
3. Better, use Swing and follow the links to relevant tutorial from the API for JScrollPane. Also, change your code to do custom painting in an extended JPanel or JComponent, following the guidelines in the tutorial on Performing Custom Painting, and respect Swing's single threaded rule, for which refer to the tutorial on Concurrency in Swing. And don't use getGraphics() of a component.


I am new to Swing.
I want to draw hexagons and also want to add scrollpane in window
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> I want to draw hexagons and also want to add scrollpane in window

please enlighten us as to what part of Darryl's reply you cannot understand.
 
Parth Pateld
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Dunn wrote:> I want to draw hexagons and also want to add scrollpane in window

please enlighten us as to what part of Darryl's reply you cannot understand.


As per Darryl's reply I have to search for JScrollPane and JComponent.

I could find a airtcle in which they have show how to use JScrollPane and also draw custom shapes in JScrollPane using Graphics Object.

I want to draw custome shapes and also want scrollpane in same Panel but i could not find any solution for the same.
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you really never referred to the API?
 
Parth Pateld
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:Have you really never referred to the API?


I have gone through those API, But Can i know how to draw a custom shape in JFrame and I also want ScrollPane in JFrame. ?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl's reply explains the drawing bit.
Do you need it in braille?
 
Parth Pateld
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Dunn wrote:Darryl's reply explains the drawing bit.
Do you need it in braille?


I have created Two files

Points.java



and another file named
CustomPanel.java



I do get ScrollPane but it is not scrolling by resizing JFrame.

Untitled.png
[Thumbnail for Untitled.png]
Output of above code
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the component in the scrolllPane needs to have a preferredSize larger than the scrollPane.
as your CustomPanel has no components, its preferredSize will be 0,0

add a constructor to CustomPanel

where 800,600 is anything you want
 
Parth Pateld
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Dunn wrote:the component in the scrolllPane needs to have a preferredSize larger than the scrollPane.
as your CustomPanel has no components, its preferredSize will be 0,0

add a constructor to CustomPanel

where 800,600 is anything you want

I got scrollpane in it, but now problem is that If I do any changes in output window means GUI (like: Resize the window, Activating a window, minimizing or maximizing a window ) , at that time paintComponent of CustomPanel() are re initiated and they are drawn again.
How can i stop that ?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
paintComponent is called numerous times, for numerous reasons, and you don't want to stop it.

sounds more like there's a problem in your code.
 
Parth Pateld
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Dunn wrote:paintComponent is called numerous times, for numerous reasons, and you don't want to stop it.

sounds more like there's a problem in your code.

Can we control number of times paintComponent is called.
Can you tell me where is the problem in my code. ?
I am stuck in this problem.
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Parth Pateld wrote:Can we control number of times paintComponent is called.

No. And you shouldn't even want to do that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic