Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

What swing component should I use  RSS feed

 
Timmy Ryan
Ranch Hand
Posts: 63
IntelliJ IDE Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

I want to create a program that displays text at random spots on the screen.
What swing component should I use for this?
 
Campbell Ritchie
Sheriff
Posts: 55292
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not certain, but you can certainly do that using the paintComponent() method of a JPanel.
You should be able to work out how using the API documentation for the Graphics class.
 
Rob Spoor
Sheriff
Posts: 21044
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another option is use null as LayoutManager, and use JLabels. You will need to set their sizes and locations manually (e.g. with setBounds), and you'll need to give the panel (or whatever it is) a predefined preferred size since it usually takes that from the LayoutManager - which you don't have.

Alternatively, you can create a custom LayoutManager that has an empty body for the layoutContainer method - only implement preferredLayoutSize and minimumLayoutSize. The preferredLayoutSize would be max(X location + preferred width), max(Y location + preferred height) over all components. Similarly, minimumLayoutSize would use the mimimum width and height instead. I have created just the thing once when I needed custom locations for child components but still wanted the preferred size to be calculated for use inside JScrollPanes.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By "screen" do you mean within a JFrame, or within the entire monitor?
 
Timmy Ryan
Ranch Hand
Posts: 63
IntelliJ IDE Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From what I have been reading inside a JFrame.
I don�t want to be able to edit the text just display text at random locations around the application screen.
Probably using the drawString method.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, just checking

Either of the suggestions in the 2nd and 3rd posts should work fine for this.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!