Gridlayout

I am using GridLayout(3,3) and added 9 components. Now can i display this in tabular format.
[99 byte] By [ArpanaKa] at [2007-11-27 0:57:35]
# 1
I searched in google but havent found anything useful. Or atleast if someone could tell me how to add a line separator between two components which are added in panel that uses GridBagLayout and GridBagConstraints.
ArpanaKa at 2007-7-11 23:31:08 > top of Java-index,Java Essentials,Java Programming...
# 2

This sounds like Suduko - is this what you want?

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class SudukoGrid extends JPanel

{

static class LocalButton extends JButton

{

public LocalButton(String value, int major, int minor)

{

super(value);

major_ = major;

minor_ = minor;

}

private int major_;

private int minor_;

}

public SudukoGrid() throws Exception

{

super(new BorderLayout());

setPreferredSize(new Dimension(600,600));

final ActionListener buttonListener = new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

LocalButton button = (LocalButton)e.getSource();

System.out.println(button.major_ + " " + button.minor_);

}

};

JPanel outerPanel = new JPanel(new GridLayout(0,3))

{

public Dimension getPreferredSize()

{

Dimension d = super.getPreferredSize();

int w = Math.max(d.height, d.width);

return new Dimension(w,w);

}

};

outerPanel.setBorder(BorderFactory.createLineBorder(Color.BLUE, 4));

for (int outer = 0; outer< 9; outer++)

{

JPanel innerPanel = new JPanel(new GridLayout(0,3));

innerPanel.setBorder(BorderFactory.createLineBorder(Color.BLUE, 2));

for (int inner = 0; inner < 9; inner++)

{

LocalButton button = new LocalButton(Integer.toString(inner+1), outer, inner);

button.addActionListener(buttonListener);

innerPanel.add(button);

}

outerPanel.add(innerPanel);

}

JPanel floatingPanel = new JPanel(new GridBagLayout());

floatingPanel.add(outerPanel);

add(floatingPanel);

}

}

with main program

import javax.swing.*;

public class SudukoMain

{

public static void main(String[] args) throws Exception

{

JFrame frame = new JFrame("Suduko Main");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setContentPane(new SudukoGrid());

frame.pack();

frame.setVisible(true);

}

}

P.S. This has been used as the basis for many many homeworks so make sure you disguise it well.

Message was edited by:

sabre150

sabre150a at 2007-7-11 23:31:08 > top of Java-index,Java Essentials,Java Programming...
# 3
Yeah, similar to suduko. Thanks for the reply and i will go through the code .
ArpanaKa at 2007-7-11 23:31:09 > top of Java-index,Java Essentials,Java Programming...