About Java swing layout (noob question)

Hello,
since this topic is not related to game development i decided to post it here,
a friend asked me to develop a little application for him, he is opening a Driver’s education “school” and this app will help him to manage his students information.
I almost did every “core” thing, the program works but in console only, but now am having many problem with GUI creation, yes i admit it, i didn’t learn enough Java Swing when i started … i just wanted to jump in games,
anyway,
what i manly want to accomplish is something similar to this (build with NetBean GUI editor)

http://s20.postimg.org/5wvajibp9/Capture.png

but i don’t want to work with netBean neither with a GUI editor, i start making some labels and i created the JTable too, but my main problem is how to place them in the wanted places ?? i tried to use borderLayouts and this is the result i get :

http://s20.postimg.org/vgxkpxx31/Capture.png

so the whole trouble is, how to create a new label in a “new line” ??

thank you

Can’t you set the layout to being vertical rather than horizontal, considering it is horizontal by default? Using BoxLayout would be good, and using the option BoxLayout.PAGE_AXIS for the second argument would be ideal. Also, what you are trying accomplish doesn’t seem like using BorderLayout is good for. I believe you want 2 JPanels going left to right with a JTable in the left panel and the information in the right panel.

What have you got against GUI creators (the Netbeans one is called Matisse btw). GUI creation is tedious, that’s why you skipped over it in the tutorials so why not let a machine do the work for you. As programmers that is kind of what we are all about. If you want to do everything the hard way then why not play a board game? It will take you 20 times of trial and error before you get a GUI right in basic coding and it then breaks apart as soon as you add anything.

Anyway, I always find the simplest way to do any layout of GUIs is to divide the components up into panels (JPanel) each with either a horizontal (BoxLayout with LINE_AXIS) or vertical (BoxLayout with PAGE_AXIS) layout. It takes time (see above) but tends to be quite robust and work well with resizing etc.

TableLayout supports Swing. :slight_smile:
http://code.google.com/p/table-layout/

On the contrary, I never use visual GUI builders. I prefer to have some code that translates my model to the Swing layout which I have built myself so that I have complete control. I don’t code each button and text area, but I do write the code that generates the GUI elements based on my layout rules and my model.

For a clean and efficient layout (that is consistent across all your screens) avoid GUI builder and write the code.

thank you guys,
i used a JSplitPane and the result was just like i want it ;D

http://s20.postimg.org/gu7n598zh/Capture.png

now i should found how to add rows to the table using a JButton, you can help if you want ::slight_smile:

I’ll add another vote against GUI builders. They hide what’s really going on, so it can be pretty hard to debug a problem or to tell a novice how to achieve a certain result. Plus the code they generate is hideous.

Do whatever you want, but IMHO (and the opinion of most other GUI developers I know), gui builders hurt more than they help, especially if you’re a beginner with Swing.

I recommend JGoodies FormLayout.

:emo:

Please fix the spelling mistake, replace “Condidat” by “Candidat”.

xD
done ::slight_smile:

I recommend TableLayout. Seriously.
:-*

yup am going to try that, i really regret that i didn’t take time to correctly learn SWING & AWT am really lost when it’s time to deal with ActionListeners and more complicated stuff , so i decided to build this app as a “game” ::slight_smile:
am gonna use LibGdx to do that and tableLayout seems to be a nice solution,

Lol maybe you consider the candidates are ass holes, that’s why you mixed the French word “con” (which means more or less “ass hole” in English) with the word “candidate” ;D

Advice from a new guy; use the right tool for the job. The default Swing controls may take a bit of getting used to working with, but they offer consistency to the end user of your application, and a lot of built in, well tested conveniences that would require quite a lot of code to implement from scratch. While learning anything new seems intimidating, when it comes to ActionListeners, if you can write a function, you can write a listener. :smiley:

Looking at your desired interface, I would say the most challenging thing to implement would be a custom table model to manage the table data. When it comes down to it though, it’s just ArrayList manipulations under the covers. The default table model is pretty useless/inefficient for doing anything other than displaying static data. If you’re interested, I have some table models that I’ve written for my job including a result set bound model for DB queries that I can post along with a sample that I wrote for a colleague showing how to tie it all together (ActionListeners and all) if you like. :wink:

I agree with CodeHead. And you should be pretty comfortable with standard GUI, 2D drawing, and stuff like data structures and event handling before diving into a library like libGDX. Just my 2 cents, but I think you’ll regret rushing into unfamiliar territory before becoming more familiar with the basics.

thank you for the advice, and yes that was a stupid decision :stuck_out_tongue: am working with swing again now and am having some good advance by searching for code examples and use them in my application, i know this is a very bad way to code a program but i need to finish it the fast as possible then i’ll focus on serious learning .
and @CodeHead yes please that would be very helpful !!

thank you

Let’s give this a shot. I apologize in advance for the inconsistent comments. I couldn’t find the finalized version of my code at home, so I ended up rebuilding the table model code from a few different development snapshots I had. That being said, I tend not to write the most coherent comments at 4 in the morning. :slight_smile: If you have any questions, I am more than happy to shed some light on the code.

RSTableModel: http://pastebin.java-gaming.org/69fc4069b50
This is the custom table model. It contains methods to let you manipulate the structure and values of the data associated with the table. It can seem a bit overwhelming at first, but I tried to avoid any “cleverness” in the implementation.

Main: http://pastebin.java-gaming.org/9fc461b9054
The main application form. Contains a lot of NetBeans generated boiler plate code. The important bits are in the initTableModel function and the three action listener callback functions; btnAddRowActionPerformed, btnDeleteRowActionPerformed, and tblDemoMouseClicked.

And here is what it looks like when it’s all put together (running under OpenBox on an ArchLinux system):

http://deathofanalog.com/uploads/screenshot01.png

One final note. I haven’t attempted to make the table model thread safe. It wasn’t needed in the context that it was developed for, and I have no compelling need to do so. It should be trivial to implement a ReentrantLock or similar synchronization methods if desired.

I hope this helps you some. As mentioned before, if you have any questions I will try to answer them as best as I can. 8)

@CodeHead: Oh wow. Are you the guy from [icode]RSyntaxTextArea[/icode]? If yes, awesome! :slight_smile:

That looks like a damn nice control. :smiley: Unfortunately, I can neither claim any affiliation with, nor any credit for that project. Being an applications developer for an international car manufacturer, my code is rarely seen/utilized beyond the walls of the company I work for. Sorry if I bumped the awesome level down a few notches. :wink: