Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

implement grid in java.awt - cells span multiple columns

Posted on 2007-12-06
7
Medium Priority
?
897 Views
Last Modified: 2013-11-23
Hi,

I need to create a grid like component in java.awt (not using Swing) where cells span multiple rows.
Has anyone done this?

Thanks
0
Comment
Question by:SAJC
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 92

Expert Comment

by:objects
ID: 20418105
not that i'm aware of , not much is done using straigt awt these days.
0
 
LVL 13

Expert Comment

by:Bart Cremers
ID: 20418452
You can get far by using a GridBagLayout:
/*
 * Copyright (c) 2007, Your Corporation. All Rights Reserved.
 */
 
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
 
/**
 * @author Bart Cremers
 * @since 6-dec-2007
 */
public class GridExample extends Panel {
 
    private static final GridBagConstraints gbc = new GridBagConstraints();
 
    static {
        gbc.fill = GridBagConstraints.BOTH;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
    }
 
    public GridExample() {
        super(new GridBagLayout());
 
        add(new BorderedLabel("A"), 1, 1, 1, 1);
        add(new BorderedLabel("B"), 1, 2, 1, 1);
        add(new BorderedLabel("C"), 1, 3, 1, 1);
        add(new BorderedLabel("D"), 1, 4, 1, 1);
        add(new BorderedLabel("E"), 1, 5, 1, 1);
 
        add(new BorderedLabel("F"), 2, 1, 1, 3);
        add(new BorderedLabel("G"), 2, 4, 1, 1);
        add(new BorderedLabel("H"), 2, 5, 3, 1);
 
        add(new BorderedLabel("I"), 3, 1, 1, 1);
        add(new BorderedLabel("J"), 3, 2, 2, 2);
        add(new BorderedLabel("K"), 3, 4, 1, 1);
 
        add(new BorderedLabel("L"), 4, 1, 1, 1);
        add(new BorderedLabel("M"), 4, 4, 1, 1);
        add(new BorderedLabel("N"), 4, 5, 1, 1);
 
 
    }
 
    private void add(Component comp, int row, int col, int rowspan, int colspan) {
        gbc.gridx = col;
        gbc.gridy = row;
        gbc.gridwidth = colspan;
        gbc.gridheight = rowspan;
 
        add(comp, gbc);
    }
 
    public static void main(String[] args) {
        final Frame frame = new Frame();
        frame.addWindowListener(new WindowAdapter() {
 
            public void windowClosing(WindowEvent e) {
                frame.dispose();
            }
        });
        frame.add(new GridExample());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
 
    private static class BorderedLabel extends Label {
 
        private BorderedLabel(String text) {
            super(text, CENTER);
        }
 
        public void paint(Graphics g) {
            super.paint(g);
 
            g.setColor(getForeground());
            g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
        }
    }
 
}

Open in new window

0
 

Author Comment

by:SAJC
ID: 20440795
Thanks for the above Bart, however, my fault, I should have stated I can't use GridBagLayout.
Sorry for late reply.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 13

Expert Comment

by:Bart Cremers
ID: 20440818
What's the reason for not being able to use gridbaglayout? It's available in AWT.
0
 

Author Comment

by:SAJC
ID: 20441120
The reason for not using any of the AWT layouts is that the number of rows and columns are arbitrary.
Will not know these till runtime.

thanks
0
 
LVL 13

Accepted Solution

by:
Bart Cremers earned 1500 total points
ID: 20441232
GridBagLayout doesn't require you to create a grid with fixed rows and colums up front.

You can simply dynamically add new "cells" or remove them.
0
 

Author Comment

by:SAJC
ID: 20441682
ok, thanks Bart
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

664 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question