0

I know that using null Layout is not a good way to make a GUI application but I think this is a case where I should use it.

I have a JPanel managed by a LayoutManager and in this JPanel there are severals JPanels. In particular, in a specific JPanel I've to draw lines and add at the end of these lines some JComponent I've created.

In a nutshell, what I'm doing is something like this

JPanel p=new JPanel();
p.setLayout(null);
.....
@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    ......
    g.drawLine(0,0,50,50);
    JComponent s=new myJComponent();
    s.setBounds(50,50,10,10);
    this.add(s);
    .......
}

My question, maybe a bit philosophical, is this: is using null layout correct in this specific context? I've not found any substitute for it because I add my component at the end of the line and this task must be done in paintComponent method.

Thank you in advance for your tips.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user2896152
  • 762
  • 1
  • 9
  • 32
  • 2
    A bad thing about the nulllayout, is that you set the exact amount of pixels. It'll look great on your screen, no doubt, but what if the user has either a way older screen, with a much smaller resolution? The screen will become too big for his screen. Or what about the guy who bought the latest of the latest? In his case, the screen will be too small to read. – Stultuske Aug 07 '15 at 08:49
  • *"..because I add my component at the end of the line and this task must be done in paintComponent method."* Adding components within a `paint..(Graphics)` method will cause a `repaint()` which will cause a call to `paint..(Graphics)` which will cause.. – Andrew Thompson Aug 07 '15 at 08:52
  • The best approach in this case is possibly a **custom layout**. BTW - what are the components, and how does the user need to interact with them? – Andrew Thompson Aug 07 '15 at 08:54
  • Thank you for your answers. I know that null layout has disadvantages. The fact is lines' position is configurable by user so I don't know where to add my components until line is drawn for this reason I choose to use it. -Stultuske in fact to prevent that I've inserted my JPanel in a JScrollPane. -Andrew Thompson my components are JLabels which show information about line. – user2896152 Aug 07 '15 at 09:07
  • You can still draw lines between components while those components are managed by a layout manager. You should never change the state of the UI, directly or indirectly, from within any paint method, this is simply asking for trouble – MadProgrammer Aug 07 '15 at 09:18
  • 1
    Also consider a graph library, for [example](http://stackoverflow.com/q/15697697/230513). – trashgod Aug 07 '15 at 09:33
  • @MadProgrammer please could you tell me how I can do that? For example, two lines drawLine(0,0,10,10); and drawLine(0,40,50,10). How can I insert my Jcomponents at the end of the lines ((10,10) e (50,10) more an extra spacing-space, of course) without forcing absolute positioning? – user2896152 Aug 07 '15 at 09:36
  • 1
    I think @trashgod got the point here: The `null` layout may be "appropriate" here to solve your particular problem, **BUT** it sounds like your particular problem was caused by your approach of (ab)using `JComponents` as "sprites". A `JComponent` is a GUI component, and not simply "something that can be rendered on the screen at an arbitrary location" (although I know that it's somtimes tempting to use it like that). You might use a `JComponent` as some sort of renderer, but without adding it to the parent (thus, making the question about layouts obsolete). – Marco13 Aug 07 '15 at 17:50
  • @user2896152 No, think about it the other way around, you want to draw a line from one component to another – MadProgrammer Aug 07 '15 at 23:19

1 Answers1

0

If you have no components within your panel then having a null layout makes sense, you have nothing to layout so no layout manager.

If you are adding components though then you should use a layout manager to lay them out, even if that means writing your own.

What you should certainly never be doing is adding a new component in paintComponent.

I think you are confused, either you want to override paintComponent and draw lines, or you want to add controls that draw lines and not have paintComponent overridden at all.

Tim B
  • 40,716
  • 16
  • 83
  • 128