Layout suggestions - table layout

Discussion in 'Java' started by sso, Apr 29, 2009.

  1. sso

    sso Guest

    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?
    sso, Apr 29, 2009
    #1
    1. Advertising

  2. sso wrote:
    > 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.

    --

    Knute Johnson
    email s/nospam/knute2009/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
    Knute Johnson, Apr 29, 2009
    #2
    1. Advertising

  3. sso

    sso Guest

    On Apr 29, 6:26 pm, Knute Johnson <>
    wrote:
    > sso wrote:
    > > 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.
    >
    > --
    >
    > Knute Johnson
    > email s/nospam/knute2009/
    >
    > --
    > Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    >              ------->>>>>>http://www.NewsDemon.com<<<<<<------
    > Unlimited Access, Anonymous Accounts, Uncensored Broadband Access


    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?
    sso, Apr 30, 2009
    #3
  4. sso wrote:
    > On Apr 29, 6:26 pm, Knute Johnson <>
    > wrote:
    >> sso wrote:
    >>> 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.
    >>
    >> --
    >>
    >> Knute Johnson
    >> email s/nospam/knute2009/
    >>
    >> --
    >> Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    >> ------->>>>>>http://www.NewsDemon.com<<<<<<------
    >> Unlimited Access, Anonymous Accounts, Uncensored Broadband Access

    >
    > 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?

    --

    Knute Johnson
    email s/nospam/knute2009/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
    Knute Johnson, Apr 30, 2009
    #4
  5. Knute Johnson wrote:
    > sso wrote:
    >> On Apr 29, 6:26 pm, Knute Johnson <>
    >> wrote:
    >>> sso wrote:
    >>>> 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.
    >>>
    >>> --
    >>>
    >>> Knute Johnson
    >>> email s/nospam/knute2009/
    >>>
    >>> --
    >>> Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    >>> ------->>>>>>http://www.NewsDemon.com<<<<<<------
    >>> Unlimited Access, Anonymous Accounts, Uncensored Broadband Access

    >>
    >> 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?
    >


    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);
    }
    });
    }
    }

    --

    Knute Johnson
    email s/nospam/knute2009/


    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
    Knute Johnson, Apr 30, 2009
    #5
  6. In article
    <>,
    sso <> wrote:

    > On Apr 29, 6:26 pm, Knute Johnson <>
    > wrote:
    > > sso wrote:
    > > > 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.


    [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.]
    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
    John B. Matthews, Apr 30, 2009
    #6
  7. sso

    Roedy Green Guest

    On Wed, 29 Apr 2009 14:23:04 -0700 (PDT), sso
    <> wrote, quoted or indirectly quoted someone
    who said :

    >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
    Roedy Green, Apr 30, 2009
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric
    Replies:
    4
    Views:
    698
    clintonG
    Dec 24, 2004
  2. Nik Coughlin
    Replies:
    12
    Views:
    619
    Nik Coughlin
    Dec 1, 2005
  3. Guybrush Threepwood

    Table-based layout to CSS layout

    Guybrush Threepwood, Jun 6, 2006, in forum: HTML
    Replies:
    20
    Views:
    979
  4. Replies:
    1
    Views:
    562
    John Timney \(MVP\)
    Jun 19, 2006
  5. Habib

    Css-Layout vs Table-Layout

    Habib, Jun 19, 2006, in forum: HTML
    Replies:
    15
    Views:
    985
    Wÿrm
    Jun 20, 2006
Loading...

Share This Page