Thiezar
Thiezar

Reputation: 1153

How to create a custom GUI component in Java? (Need starting hints)

I would like to create a custom component in Java. It is like a text area but it is composed by grid and, as you write, every character goes in a grid cell. I have no idea where to start from. I imagine a Cell class with a value attribute and a Sheet class with a Cell array attribute and a pointer that points at the current cell while writing. I have not much problems in programming such a thing. My big deal is how to show it graphically to the user.

Any suggestions or references?

EDIT: I read your excellent answers but it's not exactly what I'm looking for. When I say "a text area composed by a grid and, every character goes in a grid cell" is just a simplification of what I'm going to do. I'll give you further explanations:

There is a text area. Every row is composed by two (or more) rows (upper and lower row). Every row contains cells. In every cell there is a character or a different object (represented by a character or an image or something). Charachters in the upper row is binded/follows the corresponding lower cell. Like this.

_ _ _ _ _ _ _ _ _ _ _ _ _
_|_|_|_|_|_|X|_|_|_|_|_|_|
H|E|L|L|O|_|W|O|R|L|D|,|_|
_ _ _ _ _ _ _ _ _ _ _ _ _
_|_|_|_|Y|_|_|_|_|_|_|_|_|
H|O|W|_|A|R|E|_|Y|O|U|?|_|
_ _ _ _ _ _ _ _ _ _ _ _ _
_|_|_|_|_|_|_|_|_|_|_|_|_|
_|_|_|_|_|_|_|_|_|_|_|_|_|

You can see the X is binded to the lower W of WORLD and the Y is binded to the lower A of ARE. I hope now it'is a little more clear :-p What is the correct approach in building, designing, programming such a custom tool/component/editor?

Upvotes: 1

Views: 741

Answers (2)

Thiezar
Thiezar

Reputation: 1153

I found a good solution that works great for my purposes: The way I'm gonna approach the creation of a custom gui component like the one I explained in the first post is to extend a JPanel and implement a KeyboardListener (and a MouseListener if needed). I create a Row class and a Pointer class too of whic my main component class is composed. The graphic, based on wathever I need is done by overloading the JPanel's paintComponent method. Obviously in order to handle keyboard and mouse actions you have just to implement both KeyboardListener and MouseListener methods (they're both interfaces).

If someone is interested and asks for further explanation I can provide a class diagram or something.

Upvotes: 0

adamoldak
adamoldak

Reputation: 644

How about creating a custom JTable? Here you can find some information to start with.

Upvotes: 4

Related Questions