Looking For Good Way to Turn HTML Table Into Grid Layout

In a schematic design for the layout of my program, I drew what I want it to look like in HTML, since I'm a lot better at that than Java. Anyway, this is the code to draw a table 500px tall and 100% wide (as wide as your screen):

<table width=100% height=500 border=1>

<tr height=10>

<th colspan=3>Menu</th>

</tr>

<tr>

<td>1</td>

<td>4</td>

<td>6</td>

</tr>

<tr>

<td>2</td>

<td rowspan=2>5</td>

<td>7</td>

</tr>

<tr>

<td>3</td>

<td>8</td>

</table>

I'm looking for a way to easily turn this into something Java can understand to lay out my components. I don't mean a program that converts one language to another, but instead things to look for that will help me to work between Java and the above code to interchangeably create my layout.

I have tried NetBeans and found it VERY difficult because the program makes so many assumptions about what it thinks I want it to look like and it ends up ruining the whole thing to the point that I can't even fix it. So I turned to my one reliable resource: HTML. I've read over the Java tutorials a lot and I found the GridLayout class, but I just can't grasp it. What I'm looking for is a clear way of saying "If you want a new table row (<tr>) then just use this Java code..." or "Enabling cell #5 to span 2 rows (<td rowspan=2>) in Java is this: ..."

The program's actual window size should be around 800x600, but the given HTML table dimensions were used so viewing the actual table in a browser doesn't bunch up all the cells together. Also, upon resizing, the numeric cells should be the only ones to change size and the Menu cell should only change size horizontally.

I'm assuming most of the people who help out here also understand HTML, provided my sample HTML code is very basic and simple, I don't think it'll be a problem. Thanks for any and all support.

[2150 byte] By [tk393a] at [2007-11-27 6:14:13]
# 1
There are specific GUI forums on this site.
jschella at 2007-7-12 17:23:31 > top of Java-index,Java Essentials,Java Programming...
# 2
> There are specific GUI forums on this site.Righty-o, just thought this was a general enough question in the general forum. Let it be moved if it needs to be.
tk393a at 2007-7-12 17:23:31 > top of Java-index,Java Essentials,Java Programming...
# 3
And HTML is not a programming language so there is no code involved.ps. there's an error in your markup.
dwga at 2007-7-12 17:23:31 > top of Java-index,Java Essentials,Java Programming...
# 4
> And HTML is not a programming language so there is no> code involved.> ps. there's an error in your markup.Semantics. My point still gets across.PS: If it can be formatted between the "code" tags, I'd call it code.
tk393a at 2007-7-12 17:23:31 > top of Java-index,Java Essentials,Java Programming...
# 5
So this is code?You can't easily do colspans or rowspans using a JTable
dwga at 2007-7-12 17:23:31 > top of Java-index,Java Essentials,Java Programming...
# 6
Yep.
tk393a at 2007-7-12 17:23:31 > top of Java-index,Java Essentials,Java Programming...
# 7
Cool I think I'll call it theEnglish code
dwga at 2007-7-12 17:23:31 > top of Java-index,Java Essentials,Java Programming...
# 8
Next would you like to argue with me the correct way of pronouncing "tomato"? Or could my initial question be addressed?
tk393a at 2007-7-12 17:23:31 > top of Java-index,Java Essentials,Java Programming...
# 9

> Righty-o, just thought this was a general enough question in the general forum.

Well, since its a general question I'll give you a general answer.

Use the appropriate Layout Manager (or combination of layout managers).

[url http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html]How to Use Layout Managers[/url]. The tutorial has working examples. Or you could search the web for a third part LayoutManager. I believe its called TableLayout.

Either way you are going to have to read the tutorial and documentation because whichever layout manger you choose if will be far more complicated than the GridLayout since you have to speciify constraints for each components.

camickra at 2007-7-12 17:23:31 > top of Java-index,Java Essentials,Java Programming...
# 10
GridBagLayout has all the capabilities leveraged in your particular table.
hwaitea at 2007-7-12 17:23:31 > top of Java-index,Java Essentials,Java Programming...
# 11

> > There are specific GUI forums on this site.

>

> Righty-o, just thought this was a general enough

> question in the general forum.

This is not the general forum. This is the classpath problems, the use equals when comparing Strings, the 1 urgent doubt and the do-your-own-homework forum. As you can see (and we hope) your question does not fit into any of these categories.

cotton.ma at 2007-7-12 17:23:31 > top of Java-index,Java Essentials,Java Programming...
# 12

> > > There are specific GUI forums on this site.

> >

> > Righty-o, just thought this was a general enough

> > question in the general forum.

>

> This is not the general forum. This is the classpath

> problems, the use equals when comparing Strings, the

> 1 urgent doubt and the do-your-own-homework forum. As

> you can see (and we hope) your question does not fit

> into any of these categories.

Java Essentials - Java Programming:

"Use this forum to discuss general topics related to the Java programming language."

Guys, if you don't want to answer my question or help solve it, just don't reply. It really doesn't do any good to have a pissing match to prove superior nerdiness.

tk393a at 2007-7-12 17:23:31 > top of Java-index,Java Essentials,Java Programming...
# 13
> Guys, if you don't want to answer my question or help solve it, Your question has been answered.So instead of wasting time with unnecessary comments use your time to solve the problem with the information you've been given. We are not going to write the code for you!!
camickra at 2007-7-12 17:23:31 > top of Java-index,Java Essentials,Java Programming...
# 14

> Guys, if you don't want to answer my question or help

> solve it, just don't reply. It really doesn't do any

> good to have a pissing match to prove superior

> nerdiness.

In case you haven't noticed, we don't get paid to answer peoples' questions. We volunteer our own time to help others. We really don't care if you get your answer or not, so being rude to us is not beneficial to you.

CaptainMorgan08a at 2007-7-12 17:23:31 > top of Java-index,Java Essentials,Java Programming...
# 15

> > Guys, if you don't want to answer my question or

> help solve it,

>

> Your question has been answered.

camickr (you) and hwaite were the only two to post anything relevant for a 15 post or so thread -- it would be wise to leave the discussion open to other suggestions.

>

> So instead of wasting time with unnecessary comments

> use your time to solve the problem with the

> information you've been given.

The information given was relevant but not really all that helpful, as I noted in my OP, I've already gone through the tutorials and have read up on the different layout types, I was just looking for some help by trying to use HTML as a medium for communication (trying to meet halfway).

We are not going to

> write the code for you!!

I don't want anyone to write me code, I'm just looking for an explanation.

tk393a at 2007-7-21 21:47:50 > top of Java-index,Java Essentials,Java Programming...
# 16

> In case you haven't noticed, we don't get paid to

> answer peoples' questions. We volunteer our own time

> to help others. We really don't care if you get your

> answer or not, so being rude to us is not beneficial

> to you.

I also give others my suggestions and answers where I could give them on these forums (just view my recent posts in my profile). I know that there are a lot of knowledgeable, smart people here looking to help people with questions and problems, but sometimes some people focus too much on petty details (whether HTML is "code", whether this is in the right forum, trivial closing tags, etc.) instead of the problem at hand, and I may come off as rude by trying to steer attention back to my initial problem, so I apologize. My intentions are just to try to get my problem solved or at least some help, most of the replies have just been off-topic.

tk393a at 2007-7-21 21:47:50 > top of Java-index,Java Essentials,Java Programming...
# 17

Here you go again wasting time with an unnecessary posting instead of reading the tutorials and solving your problem.

> most of the replies have just been off-topic.

Only because you made it so. You where told you posted in the wrong forum. So get over it and move on and post in the correct forum the next time.

> but sometimes some people focus too much on petty details whether this is in the right forum,

These are not petty details. The proper way to use the forum is to post the question in the correct forum. Why do you do that? Well, before ever posting the question you should be searching the forum to see if your question has been asked and answered previously. We hate wasting our time answering the same question over and over. And you have a better chance of finding an answer to your question if you search the appropriate forum. And then once you search the forum, if you don't find the answer you post your question there. Pretty simple really.

> already gone through the tutorials and have read up on the different layout types,

Well, you haven't spent enough time trying to understand or play with each layout manager. As was mentioned earlier, The GridbagLayout provides most of this functionality and the example in the turorial does much of what you need. The constraints are confusing but its the only layout manager that is close to what you tried to present with your HTML.

As I also mentioned the 3rd party TableLayout was designed to mimic HTML So search the forum.

Quit with all these unnecessary replies. You have not provided one more piece of usefull information from your original posting that would help solve the problem. Again, I repeat we are not going to write the code for you.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.

camickra at 2007-7-21 21:47:50 > top of Java-index,Java Essentials,Java Programming...
# 18

I accept your response, but disagree. If there's nothing more anyone wants to say off-topic, I'd appreciate if my original question could be the topic of conversation. I will try to present new information to help people in understanding my problem to try to clarify the issue, since it may not be clear as is.

I understand how to draw a table in HTML. This works reasonably well and the result is close enough to what I have in mind as a design template. The problem is, I can't understand how to make such a template using the Java Layouts. I have gone through the tutorials and have had no luck in comprehending how to use them. I have read through source code and gone over the diagrams, but it just doesn't click. What I'm looking for is someone to relate the structuring of a Java Layout (be it TableLayout, GridLayout, GridBagLayout, or what have you) to the structuring of an HTML table. Desired explanations would equate tags such as <tr> and<td> with the respective methods of a Java Layout.

If this is in the wrong forum, I apologize, but I lack the ability to close or move my own thread so a moderator would have to move it for me. I am continuing the discussion with the hope that it would eventually be moved.

Again, I appreciate all help in this matter.

tk393a at 2007-7-21 21:47:50 > top of Java-index,Java Essentials,Java Programming...
# 19

> What I'm looking for is someone to relate the structuring

> of a Java Layout (be it TableLayout, GridLayout,

> GridBagLayout, or what have you) to the structuring

> of an HTML table. Desired explanations would equate

> tags such as <tr> and<td> with the respective methods

> of a Java Layout.

OK, let's look at a simple table (this, and everything in this posting, was typed without compile or test, so may have typos)

<table>

<tr>

<td>Foo</td>

<td><input name="bar" type="text" size="20"/></td>

</tr>

</table>

Most of this is just markup, describing the table. Translating it to Java, you might have a JPanel representing the <table>, a JLabel for the first <td>, and a JTextField for the second <td>. It might look something like this (using GridLayout):

JPanel panel = new JPanel(new GridLayout(1, 2));

JTextField bar = new JTextField(20);

panel.add(new JLabel("Foo"));

panel.add(bar);

This will give two a 1x2 grid, much like the original table. The main difference is that the GridLayout gives you evenly-sized cells, while HTML tables fit their cells to their content (unless you explicitly style them). As camickr has noted, there are 3rd-party layout managers that simulate an HTML table.

One big difference between Java and HTML layout is that the latter consists of a single linear flow of markup. So, you can put two tables on the page, with some text between them.

You can't do this with Java: the LayoutManager controls the entire panel, so you have to nest panels to get the same effect. For example, one top-level panel with a BoxLayout, holding a child panel with GridLayout, a few labels, and another child panel with GridLayout.

kdgregorya at 2007-7-21 21:47:50 > top of Java-index,Java Essentials,Java Programming...
# 20

You raise a good point about nesting Layouts. The content of most of the "cells" is a single JTable or JList within a JScrollPane or a JTabbedPane. Should these be their own new JPanel but not a new Layout because they're a single-unit component? On the other hand, one "cell" (the main one) has a complicated grid with many moving parts (litterally D&D). Should this one be a new JPanel with its own Layout? Perhaps another TableLayout? Thanks for your help!

Message was edited by:

tk393

A lot of my trouble with using GUIs is understanding where to make a new Panel and how to arrange all the panels and how to arrange the content of each panel using the multitudes of Layouts.

tk393a at 2007-7-21 21:47:50 > top of Java-index,Java Essentials,Java Programming...
# 21

> so you have to nest panels to get the same effect.

You can use a GridbagLayout - if you take the time to understand the constraints. It supports a span feature.

I agree it may not work 100%. It is up to the OP to produce a SSCCE showing they have made some kind of effor. Then maybe we can help fine tune the layout.

camickra at 2007-7-21 21:47:51 > top of Java-index,Java Essentials,Java Programming...
# 22

> > so you have to nest panels to get the same effect.

>

>

> You can use a GridbagLayout - if you take the time to

> understand the constraints. It supports a span

> feature.

>

> I agree it may not work 100%. It is up to the OP to

> produce a SSCCE showing they have made some kind of

> effor. Then maybe we can help fine tune the layout.

Sure, I'll try to get something going and post it when done.

tk393a at 2007-7-21 21:47:51 > top of Java-index,Java Essentials,Java Programming...
# 23

> of my trouble with using GUIs is understanding where

> to make a new Panel and how to arrange all the panels

> and how to arrange the content of each panel using

> the multitudes of Layouts.

I think you'll probably get there more easily if you stop trying to think in terms of HTML. While it's certainly possible to do that and apply some rules for translation (eg, a DIV becomes a JPanel, a TABLE becomes a JPanel, inline elements use FlowLayout, etc), I think you'll have a much nicer GUI -- not to mention cleaner code -- if you start thinking in Java from the start.

Start here to understand what the different layout managers do: http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html (this may have been posted earlier, I didn't look).

Then write some programs that use the layout managers. Just create arbitrary components, put them into a panel, and see how the managers arrange them. I have a friend who claims that 90% of the work in a Java GUI is getting the layout right, and there's really no substitute for experience.

kdgregorya at 2007-7-21 21:47:51 > top of Java-index,Java Essentials,Java Programming...
# 24

package gui;

import java.awt.*;

import javax.swing.*;

public class LayoutTest

{

private JFrame frame;

private Container pane;

private GridBagLayout layout;

private GridBagConstraints constr;

public LayoutTest()

{

frame = new JFrame("Layout Test");

pane = frame.getContentPane();

layout = new GridBagLayout();

pane.setLayout(layout);

constr = new GridBagConstraints();

constr.gridx = 0;

constr.gridy = 0;

pane.add(new JLabel("1"), constr);

constr.gridx = 1;

constr.gridy = 0;

pane.add(new JLabel("4"), constr);

constr.gridx = 2;

constr.gridy = 0;

pane.add(new JLabel("6"), constr);

constr.gridx = 0;

constr.gridy = 1;

pane.add(new JLabel("2"), constr);

constr.gridx = 1;

constr.gridy = 1;

constr.gridheight = 2;

pane.add(new JLabel("5"), constr);

constr.gridheight = 1;

constr.gridx = 2;

constr.gridy = 1;

pane.add(new JLabel("7"), constr);

constr.gridx = 0;

constr.gridy = 2;

pane.add(new JLabel("3"), constr);

constr.gridx = 2;

constr.gridy = 2;

pane.add(new JLabel("8"), constr);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.pack();

frame.setVisible(true);

}

public static void main(String[] args)

{

new LayoutTest();

}

}

That's what I came up with so far. I guess the padding and everything needs to be redone, but the end components will not be JLabels but rather panes (tabbed and scroll). It was made on the fly and it runs on my machine.

tk393a at 2007-7-21 21:47:51 > top of Java-index,Java Essentials,Java Programming...
# 25

If anyone who knows the proper way to make a GUI is reading this, I'd like to just make sure that my above class is a good way to do the GUI. I've heard something about making a new job for the constructor call or putting it in the queue or something (in the main method) but not sure how practical that is for a little demonstration like the one above. If anyone wants to nitpick, by all means go ahead, I'm looking to improve my GUI code-writing.

tk393a at 2007-7-21 21:47:51 > top of Java-index,Java Essentials,Java Programming...
# 26
> If anyone who knows the proper way to make a GUI is> reading thisYou are not going to find anyone better than camickr.
cotton.ma at 2007-7-21 21:47:51 > top of Java-index,Java Essentials,Java Programming...
# 27

> > If anyone who knows the proper way to make a GUI

> is

> > reading this

>

> You are not going to find anyone better than camickr.

Ah ok, well then I hope he comes around this thread and lends his support.

I was just editing my last post to say one last thing but cotton's new reply to it prevented me from doing so, but here it is anyway:

A couple of things I'm unsure about are when to extend JFrame or simply make an instance of it, and when to use JPanels - because my code apparently didn't need one, so now I'm not sure what they're needed for. Just to group a bunch of components? I took a semester of Java and did so little GUI programming that I feel lost.

tk393a at 2007-7-21 21:47:51 > top of Java-index,Java Essentials,Java Programming...
# 28

> > > If anyone who knows the proper way to make a GUI

> > is

> > > reading this

> >

> > You are not going to find anyone better than

> camickr.

>

> Ah ok, well then I hope he comes around this thread

> and lends his support.

I'd say he's lent you quite a bit of support already!

cotton.ma at 2007-7-21 21:47:51 > top of Java-index,Java Essentials,Java Programming...
# 29

> I'd say he's lent you quite a bit of support already!

Well I took his advise to use GridBagLayout and its constraints, and I posted up a code sample of my progress at his request, so his support has definitely been taken seriously and is appreciated. I'm just hoping that he could help "fine tune" it, as he said.

tk393a at 2007-7-21 21:47:51 > top of Java-index,Java Essentials,Java Programming...
# 30

> I'm unsure about are when to extend JFrame or simply make an instance of it,

Try to follow the discussion in this posting:

http://forum.java.sun.com/thread.jspa?threadID=5145907&start=0&tstart=0

> I'm just hoping that he could help "fine tune" it,

I'm no GridBagLayout expert since I hate using it because of all the constraints. But your requirement was to build things in a row/column fashion.

Well, the code you posted works. I don't know what your problems are. You haven't posted your SSCCE that shows the problems you are encountering.

camickra at 2007-7-21 21:47:56 > top of Java-index,Java Essentials,Java Programming...
# 31

> I also give others my suggestions and answers where I

> could give them on these forums (just view my recent

> posts in my profile). I know that there are a lot of

> knowledgeable, smart people here looking to help

> people with questions and problems, but sometimes

> some people focus too much on petty details (whether

> HTML is "code", whether this is in the right forum,

> trivial closing tags, etc.) i

I was focusing on the "petty" detail of noting that there are specific GUI forums because I suspect that people who are knowledgable about GUIs and willing to answer questions about those are probably likely to frequent those forums. It might even be the case that those are the only forums that they spend time in.

Thus you might have gotten the answer you were looking for in there.

> My intentions are just to try to get my problem

> solved or at least some help, most of the replies

> have just been off-topic.

Sorry that my "petty" suggestion didn't help you.

jschella at 2007-7-21 21:47:56 > top of Java-index,Java Essentials,Java Programming...
# 32

> Well, the code you posted works. I don't know what

> your problems are. You haven't posted your SSCCE that

> shows the problems you are encountering.

Well, I thought the idea was to get my own code going so that it runs and works and then people could help turn it into "standardly" formatted code. I thought that's what you meant by fine tune. I guess my only remaining questions are about the JPanel usage (or why I didn't need it in my code) and how I could make my posted code better, even though it's technically correct. I lost 5 points on my final project in my Java class (and got a 95) because the anal TA didn't like the way NetBeans sloppily made my GUI code look. I would have done it myself, but I have a lot of trouble making GUIs in Java, that's what I'm looking for help with in this topic.

Even if your strong points are not in GridBag, I'd really like to hear what you have to say about the other layouts. Maybe you could tell me how to achieve the same look (having cell 5 span two rows) with a different layout type, one that you are more accustomed to? I'm just looking for some exposure to the different types of layouts so I can start applying them on my own. Thanks for your help.

tk393a at 2007-7-21 21:47:56 > top of Java-index,Java Essentials,Java Programming...
# 33

As I've already told you twice before, if you really want to specify you layout in an HTML style then you should be using a TableLayout. So practice your forum searching skills and seach the forum or the web to find the layout an download it and play with it.

Also as you've also been told you can mix and match layout managers. Nowhere does it say you can only use a single layout manager. That why you have to read the tutorial and understand how each individual layout manager works so you can combine them to meet your requirements. So maybe do something like this.

a) Create a main panel and use a verical BoxLayout.

b) then you add a menu label to the main panel

c) then create a panel using a flow layout. Then add labels 1, 4, 6 to that panel and then add the panel to the main panel

d) Then create a "grid" panel using a GridLayout with 3 columns and add that panel to the main panel

e) Then create a panel using a GridLayout with two rows for labels 2, 3 and add that to the grid panel

f) then add label 5 to the grid panel

g) then create a panel using a GridLayout with two rows for labels 7, 8 and add that to the grid panel.

Thats how you nest layout to get your desired effect. There are other options as well. You could use a BorderLayout instead of a GridLayout and then add components to the North and South. Experiment and decide which layout managers you like the best and you find the easiest to use.

camickra at 2007-7-21 21:47:56 > top of Java-index,Java Essentials,Java Programming...
# 34

I understand what you're saying about researching the layout types like TableLayout, and trust me when I tell you that I've been doing so thoroughly. I meant only to ask you to for your interpretation of the HTML table, which you just provided me much to my gratitude.

I gave a GridBag layout only because that's what you most recently suggested and that's probably the one closest to HTML (with it's "coordinate" placing through GridBagConstraints), so it was easiest for me to use. I will use that code to weave in other layouts that I'm researching and learning about from this topic, and hopefully if I get anything worth noting I'll be sure to show it off here.

What's probably most troubling to me is the fact that the Java tutorials on Layouts depict the individual layout types and give examples using solely that layout type. With the exception of CardLayout, most of them put swing components like buttons and text fields in the layouts instead of other layouts, so that's most likely where my confusion and frustration started by thinking that they can't be interwoven.

Thanks very much for your assistance and tolerance. After I finally master all this and make a huge software breakthrough, I'll include your username in my "Special Thanks" consortium.

tk393a at 2007-7-21 21:47:56 > top of Java-index,Java Essentials,Java Programming...