entercite
asked on
Java layout manager.
Hello all,
I have this lovely piece of code that I have been working on that I want to add 361 equally sized boxes (panels). In other words a 19 rows by 19 columns but I dont want to use a jTable or anything like that. I tried to use Jbuilder 7 to help with the layout and on the button click. You'll see my attempt to create this grid. I need each panel to touch each other at the edges and all of them to grow in the same way if the screen is enlarged or shrunk. Any ideas on how I should do this. Is the layout I chose no good for this? Code example using this stuff would be great. Thanks.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
public class MainCon extends JFrame {
private JPanel contentPane;
private JMenuBar jMenuBar = new JMenuBar();
private JMenu jMenuFile = new JMenu();
private JMenuItem jMenuFileExit = new JMenuItem();
private JMenu jMenuHelp = new JMenu();
private JMenuItem jMenuHelpAbout = new JMenuItem();
private BorderLayout borderLayout1 = new BorderLayout();
private JPanel jpnlLeft = new JPanel();
private JPanel jpnlBottom = new JPanel();
private JPanel jpnlRight = new JPanel();
private JPanel jpnlTop = new JPanel();
private JPanel jpnlGrid = new JPanel();
private JButton jbtnLoadGrid = new JButton();
private PaneLayout paneLayout1 = new PaneLayout();
//Construct the frame
public MainCon() {
enableEvents(AWTEvent.WIND OW_EVENT_M ASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
this.setSize(new Dimension(400, 300));
this.setTitle("Modango Main Menu");
jMenuFile.setText("File");
jMenuFileExit.setText("Exi t");
jMenuFileExit.addActionLis tener(new ActionListener() {
public void actionPerformed(ActionEven t e) {
jMenuFileExit_actionPerfor med(e);
}
});
jMenuHelp.setText("Help");
jMenuHelpAbout.setText("Ab out");
jMenuHelpAbout.addActionLi stener(new ActionListener() {
public void actionPerformed(ActionEven t e) {
jMenuHelpAbout_actionPerfo rmed(e);
}
});
contentPane.setLayout(bord erLayout1) ;
jpnlGrid.setBackground(Sys temColor.a ctiveCapti onBorder);
jpnlGrid.setBorder(BorderF actory.cre ateEtchedB order());
jpnlGrid.setLayout(paneLay out1);
jbtnLoadGrid.setText("Load Grid");
jbtnLoadGrid.addActionList ener(new java.awt.event.ActionListe ner() {
public void actionPerformed(ActionEven t e) {
jbtnLoadGrid_actionPerform ed(e);
}
});
jMenuFile.add(jMenuFileExi t);
jMenuHelp.add(jMenuHelpAbo ut);
jMenuBar.add(jMenuFile);
jMenuBar.add(jMenuHelp);
contentPane.add(jpnlLeft, BorderLayout.WEST);
contentPane.add(jpnlBottom , BorderLayout.SOUTH);
contentPane.add(jpnlRight, BorderLayout.EAST);
contentPane.add(jpnlTop, BorderLayout.NORTH);
contentPane.add(jpnlGrid, BorderLayout.CENTER);
jpnlLeft.add(jbtnLoadGrid, null);
this.setJMenuBar(jMenuBar) ;
}
//File | Exit action performed
public void jMenuFileExit_actionPerfor med(Action Event e) {
System.exit(0);
}
//Help | About action performed
public void jMenuHelpAbout_actionPerfo rmed(Actio nEvent e) {
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowE vent e) {
super.processWindowEvent(e );
if (e.getID() == WindowEvent.WINDOW_CLOSING ) {
jMenuFileExit_actionPerfor med(null);
}
}
public static void main(String[] args)
{
MainCon frame = new MainCon();
frame.setSize(600,400);
frame.setVisible(true);
}
void jbtnLoadGrid_actionPerform ed(ActionE vent e)
{
for(int i=0; i<361; i++)
{
JPanel pnl = new JPanel();
pnl.setBorder(BorderFactor y.createEt chedBorder ());
pnl.setBackground(SystemCo lor.green) ;
jpnlGrid.add(pnl, null);
}
}
}
I have this lovely piece of code that I have been working on that I want to add 361 equally sized boxes (panels). In other words a 19 rows by 19 columns but I dont want to use a jTable or anything like that. I tried to use Jbuilder 7 to help with the layout and on the button click. You'll see my attempt to create this grid. I need each panel to touch each other at the edges and all of them to grow in the same way if the screen is enlarged or shrunk. Any ideas on how I should do this. Is the layout I chose no good for this? Code example using this stuff would be great. Thanks.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
public class MainCon extends JFrame {
private JPanel contentPane;
private JMenuBar jMenuBar = new JMenuBar();
private JMenu jMenuFile = new JMenu();
private JMenuItem jMenuFileExit = new JMenuItem();
private JMenu jMenuHelp = new JMenu();
private JMenuItem jMenuHelpAbout = new JMenuItem();
private BorderLayout borderLayout1 = new BorderLayout();
private JPanel jpnlLeft = new JPanel();
private JPanel jpnlBottom = new JPanel();
private JPanel jpnlRight = new JPanel();
private JPanel jpnlTop = new JPanel();
private JPanel jpnlGrid = new JPanel();
private JButton jbtnLoadGrid = new JButton();
private PaneLayout paneLayout1 = new PaneLayout();
//Construct the frame
public MainCon() {
enableEvents(AWTEvent.WIND
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
this.setSize(new Dimension(400, 300));
this.setTitle("Modango Main Menu");
jMenuFile.setText("File");
jMenuFileExit.setText("Exi
jMenuFileExit.addActionLis
public void actionPerformed(ActionEven
jMenuFileExit_actionPerfor
}
});
jMenuHelp.setText("Help");
jMenuHelpAbout.setText("Ab
jMenuHelpAbout.addActionLi
public void actionPerformed(ActionEven
jMenuHelpAbout_actionPerfo
}
});
contentPane.setLayout(bord
jpnlGrid.setBackground(Sys
jpnlGrid.setBorder(BorderF
jpnlGrid.setLayout(paneLay
jbtnLoadGrid.setText("Load
jbtnLoadGrid.addActionList
public void actionPerformed(ActionEven
jbtnLoadGrid_actionPerform
}
});
jMenuFile.add(jMenuFileExi
jMenuHelp.add(jMenuHelpAbo
jMenuBar.add(jMenuFile);
jMenuBar.add(jMenuHelp);
contentPane.add(jpnlLeft, BorderLayout.WEST);
contentPane.add(jpnlBottom
contentPane.add(jpnlRight,
contentPane.add(jpnlTop, BorderLayout.NORTH);
contentPane.add(jpnlGrid, BorderLayout.CENTER);
jpnlLeft.add(jbtnLoadGrid,
this.setJMenuBar(jMenuBar)
}
//File | Exit action performed
public void jMenuFileExit_actionPerfor
System.exit(0);
}
//Help | About action performed
public void jMenuHelpAbout_actionPerfo
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowE
super.processWindowEvent(e
if (e.getID() == WindowEvent.WINDOW_CLOSING
jMenuFileExit_actionPerfor
}
}
public static void main(String[] args)
{
MainCon frame = new MainCon();
frame.setSize(600,400);
frame.setVisible(true);
}
void jbtnLoadGrid_actionPerform
{
for(int i=0; i<361; i++)
{
JPanel pnl = new JPanel();
pnl.setBorder(BorderFactor
pnl.setBackground(SystemCo
jpnlGrid.add(pnl, null);
}
}
}
>> In other words a 19 rows by 19 columns
Try implementing this GridLayout :
GridLayout x = new GridLayout(19, 19);
// Add your parts here and make sure
// that you implement 19, 19
x.add(YourPartHere); x.add(YourPartHere);
x.add(YourPartHere); x.add(YourPartHere);
x.add(YourPartHere); x.add(YourPartHere);
// Up to 19 . . .
Hope this helps . . .
JAVATM
Try implementing this GridLayout :
GridLayout x = new GridLayout(19, 19);
// Add your parts here and make sure
// that you implement 19, 19
x.add(YourPartHere); x.add(YourPartHere);
x.add(YourPartHere); x.add(YourPartHere);
x.add(YourPartHere); x.add(YourPartHere);
// Up to 19 . . .
Hope this helps . . .
JAVATM
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Anyone else like to implement my suggestion? ;-)
You seem to be a bit grumpy today CEHJ ;-)
We're just elaborating it more, to be unturstood well if it okey with you :) CEHJ
ASKER
Hello,
That seems to do the trick with a small modification:
jpnlGrid.setLayout(new GridLayout(19, 19, -1, -1));
for (int i = 0; i < 361; i++)
{
JButton btn = new JButton(""+i);
btn.setBorderPainted(false );
jpnlGrid.add( btn );
}
One more quick question to add to this. (If its really a hard thing then I will add more points on.)
How would I add to the surface of the buttons a jpg or gif with two lines intersecting like a + I will have to make a corner and edge image too but do you think its possible to make the lines from the image on the buttons intersect with each other seemlessly? I am trying to figure that out now.
thanks
That seems to do the trick with a small modification:
jpnlGrid.setLayout(new GridLayout(19, 19, -1, -1));
for (int i = 0; i < 361; i++)
{
JButton btn = new JButton(""+i);
btn.setBorderPainted(false
jpnlGrid.add( btn );
}
One more quick question to add to this. (If its really a hard thing then I will add more points on.)
How would I add to the surface of the buttons a jpg or gif with two lines intersecting like a + I will have to make a corner and edge image too but do you think its possible to make the lines from the image on the buttons intersect with each other seemlessly? I am trying to figure that out now.
thanks
Quite hard but I have a quick solution if you dont mind, can you create a gif or jpg image and try
to put this commands on a JButton.
JButton button = new JButton(createImageIcon("s ample1.gif "));
button.setPressedIcon(crea teImageIco n("sample2 .gif"));
button.setRolloverIcon(cre ateImageIc on("sample 3.gif"));
button.setDisabledIcon(cre ateImageIc on("sample 4.gif"));
button.setMargin(new Insets(0,0,0,0));
Note : sample1 up to 4 are gif images.
Hope it helps, God Speed . . .
JAVATM
to put this commands on a JButton.
JButton button = new JButton(createImageIcon("s
button.setPressedIcon(crea
button.setRolloverIcon(cre
button.setDisabledIcon(cre
button.setMargin(new Insets(0,0,0,0));
Note : sample1 up to 4 are gif images.
Hope it helps, God Speed . . .
JAVATM
It will not be a normal JButton, it will look like exactly your gif or jpg image.
If you want you can just create an image similar to your problem and put
it on the JButton and you'll see difference.
If you want you can just create an image similar to your problem and put
it on the JButton and you'll see difference.
>>You seem to be a bit grumpy today CEHJ ;-)
>>
We're just elaborating it more, to be unturstood well if it okey with you :) CEHJ
>>
That's OK - but not *too* many people elaborating as it can confuse questioner's ;-)
To draw the cross on your button, do:
class MyButton extends JButton {
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight());
g.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2);
}
}
>>
We're just elaborating it more, to be unturstood well if it okey with you :) CEHJ
>>
That's OK - but not *too* many people elaborating as it can confuse questioner's ;-)
To draw the cross on your button, do:
class MyButton extends JButton {
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight());
g.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2);
}
}
>>questioner's = questioners
LOL
LOL
ASKER
I think this problem is answered now.
jimmack I think you answered it best in example code. Also I feel as though I should make a seperate question for CHEJ Since he got the drawing portion to work best. I have a couple more questions on drawing the lines.
Thanks everyone.
jimmack I think you answered it best in example code. Also I feel as though I should make a seperate question for CHEJ Since he got the drawing portion to work best. I have a couple more questions on drawing the lines.
Thanks everyone.
I need each panel to touch each other at the edges and all of them to grow in the same way if the screen is enlarged or shrunk.
>>
A GridLayout would do that