Layout suggestions - table layout

S

sso

Hi,
I need to layout data in a tabular fashion. In html a table layout
would work perfectly. In swing, it does not. Firstly I need
multiline cells - there doesn't seem to be a plain easy way to do this
in swing. Is there a work around? Other suggestions?
 
K

Knute Johnson

sso said:
Hi,
I need to layout data in a tabular fashion. In html a table layout
would work perfectly. In swing, it does not. Firstly I need
multiline cells - there doesn't seem to be a plain easy way to do this
in swing. Is there a work around? Other suggestions?

What's wrong with a JLabel? That will do multi-line text and even some
simple HTML if you want.
 
S

sso

What's wrong with a JLabel?  That will do multi-line text and even some
simple HTML if you want.

The problem I have with using html is that I have no means of auto-
sizing the label (or table cell) I just tried this all out and its
not quite doing what I want. Am I missing something?
 
K

Knute Johnson

sso said:
The problem I have with using html is that I have no means of auto-
sizing the label (or table cell) I just tried this all out and its
not quite doing what I want. Am I missing something?

If you need to duplicate an HTML table with the auto sizing of the cells
then you are going to have write it your self or find a commercial one.
HTML tables are really complicated things.

What exactly are you trying to display?
 
K

Knute Johnson

Knute said:
If you need to duplicate an HTML table with the auto sizing of the cells
then you are going to have write it your self or find a commercial one.
HTML tables are really complicated things.

What exactly are you trying to display?

The other option is to display the whole HTML table in a JLabel.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test4 extends JPanel {
String HTMLTable = "<html><table width=500 border=1>" +
"<tr><td>Row 1 column 1" +
" <td>Row 1 column 2" +
"<tr><td>Row 2 column 1" +
" <td>Row 2 column 2" +
"<tr><td colspan=2>This is a lot of text that is displayed in " +
" two columns. ;laskj asl;dkj adj as asld a l;aksd aslk;dj" +
" ;las ewl;k2 23 qwer 23 qwef asdl;fkoio asdf;ajs ";

public test4() {
JLabel l = new JLabel(HTMLTable);
add(l);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test4 t4 = new test4();
f.add(t4,BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
});
}
}
 
J

John B. Matthews

[Please trim signatures when responding.]

Many Swing components can render HTML 3.2, including tables[1].
The problem I have with using html is that I have no means of auto-
sizing the label (or table cell).

Use a suitable layout to size the labels[1].
I just tried this all out and its not quite doing what I want.

What did you try? You should create a short, compilable example to show
what you're doing and where you're having trouble[2].
Am I missing something?

Sun's tutorial is good place to start[1].

Here's another example:

<code>
package news;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/** @author John B. Matthews */
public class GridTest {

private static final int ROWS = 5;
private static final int COLS = 4;

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@ Override
public void run() {
new GridTest().create();
}
});
}

void create() {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(ROWS, COLS));
for (int i = 0; i < ROWS * COLS; i++) {
panel.add(new LabelPanel());
}
JFrame f = new JFrame("GridTest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(panel);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}

/**
* A LabelPanel has a label showing its current size.
*/
class LabelPanel extends JPanel {

private static final String FORE =
"<html><body align=center>Size: <b>";
private static final String AFT =
"</b><br><i>ad astra<br>per aspera</i></body></html>";
private final JLabel sizeLabel = new JLabel();

public LabelPanel() {
this.setPreferredSize(new Dimension(150, 75));
this.add(sizeLabel);
this.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
int w = e.getComponent().getWidth();
int h = e.getComponent().getHeight();
sizeLabel.setText(FORE + w + "\u00d7" + h + AFT);
}
});
}
}
</code>

[1]<http://java.sun.com/docs/books/tutorial/uiswing/components/html.html>
[2]<http://pscode.org/sscce.html>
[Please trim signatures when responding.]
 
R

Roedy Green

I need to layout data in a tabular fashion. In html a table layout
would work perfectly. In swing, it does not. Firstly I need
multiline cells - there doesn't seem to be a plain easy way to do this
in swing. Is there a work around? Other suggestions?

Tables with variable numbers of lines are most easily handled with
JTables. See http://mindprod.com/jgloss/jtable.html

For sample code see
http://mindprod.com/products1.html#VERCHECK


For very simple tables, you can use arrays of Components or arrays of
Row objects containing components. Have a look at the source code for
ComparatorCutter to see how you might do that.

see http://mindprod.com/products1.html#COMPARATORCUTTER
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Nationalism is an infantile disease. It is the measles of mankind."
~ Albert Einstein
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top