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

moveable resizable UI components in Java Swing  RSS feed

 
irene
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

I am a complete newbie here. I am developing a GUI builder using Java Swing. As for the purpose, I'd like to be able to put components (such as JTextfield, JTextarea, etc) in a fix position on JPanel. And later on the user can drag and move the components. In short, I'm wondering how to make a Visual Basic or Jigloo like user interface.

The questions:
1) I've searched for a while and found out about ComponentListener. Am I on the right track by implementing this interface for my components?
However, the component which implements this cannot be moved or resized at all.

2) Since I want to put the components on fix positions, I use:

setLayout(null); // for the containing panel
....
JTextfield tf = new JTextfield("test");
tf.setBounds(40,50,60,25);
...

The problem is it displays strangely as if there are a shadow textfield and a real textfield. The "shadow" is a non-editable textfield, and not so far away from it lies an editable textfield which can only be revealed after I click on it (at first, it doesn't show at all).

Has anyone any idea to solve these problems?

Many thanks before..
 
Mark Spritzler
ranger
Sheriff
Posts: 17309
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Irene"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark
 
Mark Spritzler
ranger
Sheriff
Posts: 17309
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, unfortunately Jave Swing is far from being a Visual Basic form. in VB positions are done with left right and height and width. However, in Java you really need to use Layout Managers to layout the Components. Which leaves the Layout Manager to be in control. Now for drag and drop capablilities, the Component Listener isn't the one you want to use. You need to use things like the Interface DragSourceListener.

Here are some sites you can go to for tutorials for Swing and Drag and Drop.

Hope they help

List of articles at the Swing Connection at Sun's website
Drag N Drop article from the above list.
The Swing Trail at Sun's website.

Good Luck

Mark
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm moving this to our Swing forum...
 
Rob Spoor
Sheriff
Posts: 21047
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Spritzler:
in VB positions are done with left right and height and width. However, in Java you really need to use Layout Managers to layout the Components. Which leaves the Layout Manager to be in control.
You can also use left, right, width and height in Java - simply set the LayoutManager to be null and then use setLocation, setSize and setBounds.

I've written a Visio Studio kind of thing where objects can be dragged (not dropped though), and I used a special LayoutManager (written it myself) - one that does not place components (it lets other code handle that) but does compute the minimum and preferred sizes. Really useful for use in scroll panes.


Another note on the listeners you need: apart from DragSourceListener you may also look at MouseMotionListener for dragging inside the panel itself (this is the one I used a lot).


And a final note: only use hard coded positions and sizes if you really really need them. Remember: components do not get resized without a LayoutManager.
[ November 15, 2005: Message edited by: Rob Spoor ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!