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.

743 questions
46
votes
3 answers

Difference between JPanel, JFrame, JComponent, and JApplet

I'm making a physics simulator for fun and I was looking up graphics tutorials when I tried to figure out the difference between all these J's. Can somebody elaborate on them or perhaps provide a link to a helpful source?
Andyroo
  • 461
  • 1
  • 5
  • 3
38
votes
6 answers

How to create a custom Swing Component

I've always wanted to create custom components in Java, or customize existing ones, but my searches never resulted in anything useful. So I decided to ask the StackOverflow community: Where can I find information about customizing Java GUI…
pek
  • 16,939
  • 27
  • 83
  • 98
27
votes
4 answers

Can't a Swing component be added to multiple containers?

I'm trying (testing something else) to add one JButton reference into two JPanels to test it, and it disappears from the first panel it was added to! So, can't a Swing component be added to multiple containers? Thank you in advance.
Juan Diego
  • 861
  • 1
  • 10
  • 22
26
votes
4 answers

JSplitPane splitting 50% precisely

In Swing, what's the best way to make the JSplitPane to split two jpanels with 50% size each. It looks like if I don't set preferred sizes on the panels it always makes the first panel almost invisible (2%) and the second one (98%) Thanks in advance
user278731
  • 263
  • 1
  • 3
  • 4
23
votes
1 answer

JPanel which one of Listeners is proper for visibility is changed

Are there some rules, or good/bad experiences with AncestorListener, ComponentListener or HierarchyListener listening for visibility of changes with JPanel and JComponents? Is one of them better or safer than the others? I would especially like to…
mKorbel
  • 108,320
  • 17
  • 126
  • 296
20
votes
4 answers

How to get all elements inside a JFrame?

I have this code to get all the elements I need and do some processing. The problem is I need to specify every panel I have to get the elements inside it. for (Component c : panCrawling.getComponents()) { //processing } for (Component c :…
Renato Dinhani
  • 30,005
  • 49
  • 125
  • 194
17
votes
7 answers

Java get JPanel Components

I have a JPanel full of JTextFields... for (int i=0; i
Devoted
  • 90,341
  • 41
  • 85
  • 110
16
votes
1 answer

Is MVC in Swing Thread Safe

I'm trying to touch limits of MVC architecture in Swing, but as I tried everything all (from SwingWorker or Runnable#Thread) are done on EDT my questions: is there some limits or strictly depends by order of the implementations (wrapped into…
mKorbel
  • 108,320
  • 17
  • 126
  • 296
13
votes
2 answers

How can I get all the components of a panel in Java Swing?

How can I get all the components of a panel in Java Swing? Is there any method like foreach in C# to deal all the child components of a JPanel?
user1918908
11
votes
7 answers

Swing HTML drawString

I'm trying to create some special component for a specific purpose, on that component I need to draw a HTML string, here's a sample code: public class MyComponent extends JComponent{ public MyComponent(){ super(); } …
George Casttrey
  • 407
  • 5
  • 16
11
votes
1 answer

How does JComponent.paintImmediately() work in Java Swing?

My understanding: Unlike most of the components/operations in Swing call to JComponent.repaint() is thread-safe i.e. though a repaint request is made from another thread (i.e. not from EDT), the actual painting happens in EDT only. Below code…
Learner
  • 413
  • 4
  • 17
11
votes
6 answers

Placing a JLabel at a specific x,y coordinate on a JPanel

I'm trying to place a series of JLabels at specific X and Y coordinates on a JPanel (and set its height and width, too). No matter what I do, each label winds up immediately to the right of the previous label and has the exact same size as all of…
AndroidDev
  • 18,031
  • 39
  • 126
  • 220
11
votes
3 answers

Difference between JComponent.isShowing() and isDisplayable()

What's the difference between Component.isShowing() and Component.isDisplayable()? I want to use them to decide wheter I should stop/start a Timer.
keuleJ
  • 3,060
  • 3
  • 26
  • 47
10
votes
2 answers

Overriding getPreferredSize() breaks LSP

I always see advices in this site of overriding getPreferredSize() instead of using setPreferredSize() as shown in these previous threads for example. Use of overriding getPreferredSize() instead of using setPreferredSize() for fixed size…
nachokk
  • 14,055
  • 4
  • 22
  • 47
10
votes
2 answers

What are the benefits to painting on a JPanel vs. JComponent?

So in a recent answer, someone commented this (in regards to painting): "This is probably some kind of illness of 90% of Swing Programmers: When they make their own component, they always extend JPanel instead of JComponent. Why?" I'm still fairly…
Paul Samsotha
  • 188,774
  • 31
  • 430
  • 651
1
2 3
49 50