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
9
votes
3 answers

Draw a car wheel

I have a very simple problem. I am learning Java, and was given an assignment to draw a car. I did this all in one class that extends JPanel, and did the drawing within paintComponent(). I realize this is poor object-oriented programming, and…
Josue Espinosa
  • 4,642
  • 14
  • 43
  • 80
8
votes
4 answers

Can I get the right JComponent size before it's shown?

When is the size of the JComponent is calculated? After being shown in the screen or before that? if I send .getSize() message before .setVisible(true), would it give me the right answer? Thanks
Man o War
  • 105
  • 1
  • 7
7
votes
1 answer

Manually position JComponent inside JPanel

I want to programmatically move my JLabel to a specific location inside my JPanel. I have tried setLocation(int x, int y), but it doesn't work. I am trying to not use any layout manager.
oletus
  • 79
  • 1
  • 1
  • 3
7
votes
3 answers

Creating a simple custom JComponent in Java?

I want to start building my own customized JComponent's for a project at work. I have a simple example below that should just create a ball on the screen. (I found most of it on the internet) but it does provide a decent starting point. My question…
user3376708
7
votes
1 answer

Java Swing - JPanel vs JComponent

I'm playing around with Java Swing and i'm really confused when comes to JPanel vs JComponent. According to CoreJava Vol 1 (cay horstmann): Instead of extending JComponent, some programmers prefer to extend the JPanel class. A JPanel is intended…
yapkm01
  • 2,905
  • 6
  • 34
  • 52
7
votes
1 answer

How to check whether JComponent inside JScrollPane is visible to a user?

Imagine you have a JScrollPane and many JLabels or any other JComponents added to it. How would you check if a certain component is visible/partially visible/not visible to user? (scrolling) I have tried to Google but could not find an easy…
Nikolay Kuznetsov
  • 8,896
  • 9
  • 49
  • 94
7
votes
2 answers

JComponent not Drawing to JPanel

I have a custom component that extends JComponent, which overrides the method paintComponent(Graphics g) but when I try to add it to my JPanel it just doesn't work, nothing is drawn. Here is my code: public class SimpleComponent extends…
lilroo
  • 2,631
  • 7
  • 22
  • 31
6
votes
1 answer

Change Font at runtime

Please is there another way how to change Font at runtime as using FontUIResource, for the whole AWT/Swing GUI, without any knowledge / interest about if there are local variables and type of JComponents import java.awt.*; import…
mKorbel
  • 108,320
  • 17
  • 126
  • 296
6
votes
1 answer

How do i recalculate preferred size of a JComponent?

I know the fact that when I create an instance of a JComponent, it has it's own preferred size. Now let's suppose that I setPreferredSize that JComponent manually with a dimension of 0 x 0. And i want that Component to "reset" its own preferredSize.…
William Wino
  • 2,879
  • 6
  • 34
  • 58
6
votes
2 answers

Aligning JComponents to left- and right-hand sides of a JPanel

I have a JPanel that contains two JComponents, say two JButtons, btnLeft and btnRight. I want these two buttons aligned horizontally and I want btnLeft to be on the left side of the JPanel and btnRight to be on the right side of the JPanel with…
Paul Reiners
  • 8,896
  • 30
  • 107
  • 178
6
votes
2 answers

File browser component for Java swing

Has anyone come across a fully customizable (compatible with all standard LnF), fast file browser component for Java Swing ? I should be able to place this component to Netbeans UI palette and drag and drop in to any JPanel while designing the UI.…
ring bearer
  • 18,848
  • 7
  • 53
  • 68
6
votes
3 answers

Make a simple GUI editor in Java Swing using Swing components

I'm currently working on a project in which I need a very simple editor for GUI-like objects. This editor would be a canvas on which well known GUI widgets can be placed. For example one can place a button and a textfield on there, move them around…
bobvdv
  • 65
  • 1
  • 5
5
votes
4 answers

The JPanel contentpane confusion

I am learning Java Swing and I appended a menuBar to the frame. By default this should call jframe.getContentPane().add(child). When I ran the script the menuBar didn't show up. But the button was at the very top "y=0" if that makes sense. Then I…
Lews Therin
  • 10,709
  • 4
  • 43
  • 69
5
votes
1 answer

TitledBorder problem with Substance UI and custom JComponent

I'm using BorderFactory to create a titled border with a Substance UI themed Swing user interface, and I'm coming across the exception shown below. I tried with LineBorder and it works perfectly, but it appears that Substance UI is interfering…
Chris Dennett
  • 21,465
  • 8
  • 52
  • 84
5
votes
3 answers

Java wait for component to be painted

I'm trying to create a program in Java that would display set of images one after another adjusting the size of the frame for each one. I'm extending JPanel to display an image like this: public class ImagePanel extends JPanel{ String…
Leonti
  • 9,212
  • 10
  • 35
  • 65
1
2
3
49 50