Questions tagged [jcomponent]

JComponent is the base class for all Java Swing components except top-level containers.

JComponent is the base class for all Java Swing components except top-level containers which descend from Window instead.

To use a component that inherits from JComponent, you must place the component in a containment hierarchy whose root is a top-level Swing container. Top-level Swing containers -- such as JFrame, JDialog, and JApplet -- are specialized components that provide a place for other Swing components to paint themselves.

768 questions
3
votes
3 answers

Using Java's JComponent repaint()

I'm writing a simple Game of Life program in Java and am having a bit of trouble getting it to animate. I've got a JComponent class called LifeDraw, which displays a grid of pixels, with the following paint method: protected void…
Adam
  • 57
  • 1
  • 4
3
votes
2 answers

How to determine the correct UI implementation for a custom component

I'm creating a custom component with its own custom UI. The question How to create a custom Swing Component provided a great start in developing the component, but I haven't been able to find a good way to connect the custom component with its…
Rangi Keen
  • 935
  • 9
  • 29
3
votes
5 answers

Where does Java copy and store objects when .add'ing them to a Swing component?

The overall question is - where does Java store component (buttons, menu items, etc.) objects when they're added to something like a JFrame, JPanel, JMenu? While digging through the documentation I saw something saying they're stored in a list, but…
Azoreo
  • 236
  • 1
  • 3
  • 14
3
votes
1 answer

Setting JButton Icon

I want a JButton to have an icon on it. So far I have this. ImageIcon ii = new ImageIcon("Button.png"); width = ii.getIconWidth(); height = ii.getIconHeight(); Toolkit tk = Toolkit.getDefaultToolkit(); windowW = (int)…
user2328614
3
votes
2 answers

How to get the painted size of a Swing component?

When I add Swing component (like a JButton) to a JPanel, it renders with it's 'preferred size'. However, the preferred size is actually larger than the painted button. There appears to be an invisible border around it. Here's a simple frame with my…
user550738
3
votes
1 answer

JComponent painting issue

I'm trying to create a custom Calendar in Java. For this I extend javax.swing.JComponent. public class GMCalendar extends JComponent { ... } In my constructor I do some basic setup and load my image: calendarDay =…
Fabio Poloni
  • 8,219
  • 5
  • 44
  • 74
3
votes
3 answers

What's overriding my preferred size? (JPanel)

So I'm pretty new to GUI, and came across the problem that whatever I set my preferred size to ( via setPreferredSize(new Dimension(width, height)) ) it adds 10 to both width and height. I was wondering, why does this happen? Yes I understand that…
user1945612
  • 31
  • 1
  • 2
3
votes
1 answer

How do I recursively disable my components in Swing?

How do I recursively disable all of my components in a JPanel?
sdasdadas
  • 23,917
  • 20
  • 63
  • 148
3
votes
4 answers

Trying to animate an ArrayList of lines/circles on a Jframe(forms a car), but I can't seem to erase the old lines drawn

As you can see from the image, I need to delete the old lines but I'm not sure how. Been stuck on this for a few days. I am pretty sure the problem is I am moving the lines and circles wrong (using setLine, setFrame respectively), since I replaced…
Abdi Osman
  • 43
  • 1
  • 7
3
votes
2 answers

Forcing JButton not to draw background

I am coding a little program and this is basically my first time to using a JComponent to draw stuff. I set the background of the component to black. But as soon as I draw a JButton in it, it gets overwritten by the default grey. I have been…
yawkat
  • 374
  • 2
  • 14
3
votes
1 answer

How to add buttons and other components dynamically with free layout in swing?

Hi i am new to java swing. i have to add buttons dynamically , when i try to add those buttons dynamically it does not add to panel when it is in free layout. it accepts any one layout like null layout or gridbaglayout. Is there any other way to…
Babu R
  • 1,025
  • 8
  • 20
  • 40
3
votes
3 answers

Do I actually call the paintComponent method I make when creating a rectangle in Java?

This is my current RectangleComponent class and I add it to a panel in my main JFrame but it never appears. I thought it wasn't drawing so I decided to call the paintComponent method in the Rectangle's constructor, and after sorting through 4-5…
keyert
  • 255
  • 1
  • 4
  • 12
2
votes
2 answers

java resizing methods

Could you tell me what methods are called upon a JFrame/JDialog after you resize it manually? ( after you resize it using the mouse cursor, while the frame is visible on the screen). I noticed that my JDialog is not valid eventhough I call…
Teo
  • 3,394
  • 11
  • 43
  • 73
2
votes
2 answers

JComponent not visible

I'm working on a project in swing, and I have a class that subclasses JComponent, but does not override any of its methods. Later, in a subclass of that class I call the following code: System.out.println(fImage.getHeight() + " " +…
Jesan Fafon
  • 2,234
  • 1
  • 25
  • 29
2
votes
2 answers

Image on Custom JComponent is not Visible?

When I run my code it doesn't show up. Basically I have a custom Jcomponent which I add to my JFrame or View and then create a View that makes the frame in my main method. I already added to JFrame here is my code for the JComponent: public class…
Rendition
  • 77
  • 9