1

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?

5
  • Swing, AWT, SWT, other? We need some context here. Also have you gone through tutorials for whatever GUI library you're using? Commented Aug 5, 2012 at 18:11
  • I am using swing and awt. I'm a real beginner in java (however not so beginner in programming). Commented Aug 5, 2012 at 18:15
  • 1
    I think that your best bet is to first learn your tool set by going through much of the Swing tutorials. Commented Aug 5, 2012 at 18:27
  • So your suggestion is to use properly some swing components instead of creating one by myself. My starting idea was to extend a JPanel and then to draw on it but I really couldn't imagine how to let the user type text in it and display it. Commented Aug 5, 2012 at 18:31
  • Use a JTextArea with a fixed width Font. Commented Aug 5, 2012 at 22:50

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

5 Comments

Do we even know he is using Swing yet?
Yes I'm using swing. Since I'm going to make some cells to contains different objects (even draggable ones) maybe JTable isn't the best choice for me. I keep myself simple in the first post but the fact is that this custom component is gonna be more complex. The "text area" I told before is just a starting point to me :-)
@Thiezar: A JTable can handle holding different objects as long as your renderer and editor have been set properly.
In this case I will learn something on JTables :-) Meanwhile if someone could help me with more information on how to afford my problem, like "I would go this way" or something on the architecture of the solution I should follow, it would be appreciated :-)
... in my experience JTable isn't for beginners in swing (much less for beginners in Java). But if you're going for a Table (like Excel) - it's about the only built in option available. :/
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.