Solved

Java layout manager.

Posted on 2003-10-23
12
505 Views
Last Modified: 2013-12-03
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.WINDOW_EVENT_MASK);
        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("Exit");
        jMenuFileExit.addActionListener(new ActionListener()  {
            public void actionPerformed(ActionEvent e) {
                jMenuFileExit_actionPerformed(e);
            }
        });
        jMenuHelp.setText("Help");
        jMenuHelpAbout.setText("About");
        jMenuHelpAbout.addActionListener(new ActionListener()  {
            public void actionPerformed(ActionEvent e) {
                jMenuHelpAbout_actionPerformed(e);
            }
        });
        contentPane.setLayout(borderLayout1);
        jpnlGrid.setBackground(SystemColor.activeCaptionBorder);
        jpnlGrid.setBorder(BorderFactory.createEtchedBorder());
        jpnlGrid.setLayout(paneLayout1);
        jbtnLoadGrid.setText("Load Grid");
        jbtnLoadGrid.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jbtnLoadGrid_actionPerformed(e);
            }
        });
        jMenuFile.add(jMenuFileExit);
        jMenuHelp.add(jMenuHelpAbout);
        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_actionPerformed(ActionEvent e) {
        System.exit(0);
    }
    //Help | About action performed
    public void jMenuHelpAbout_actionPerformed(ActionEvent e) {
    }
    //Overridden so we can exit when window is closed
    protected void processWindowEvent(WindowEvent e) {
        super.processWindowEvent(e);
        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
            jMenuFileExit_actionPerformed(null);
        }
    }

    public static void main(String[] args)
    {
        MainCon frame = new MainCon();
        frame.setSize(600,400);
        frame.setVisible(true);
    }

    void jbtnLoadGrid_actionPerformed(ActionEvent e)
    {

        for(int i=0; i<361; i++)
        {
            JPanel pnl = new JPanel();
            pnl.setBorder(BorderFactory.createEtchedBorder());
            pnl.setBackground(SystemColor.green);
            jpnlGrid.add(pnl, null);
        }

    }
}
0
Comment
Question by:entercite
[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
  • 4
  • 4
  • 2
  • +1
12 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9610162
>>
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
0
 
LVL 15

Expert Comment

by:Javatm
ID: 9610501
>> 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
0
 
LVL 15

Accepted Solution

by:
jimmack earned 125 total points
ID: 9610608
Remove the borland import and the references to PaneLayout.

Then add the following into jbInit(), modifying it to suit your needs.

        ...
        contentPane.setLayout(borderLayout1);
        jpnlGrid.setBackground(SystemColor.activeCaptionBorder);
        jpnlGrid.setBorder(BorderFactory.createEtchedBorder());
       
// New code
        jpnlGrid.setLayout(new GridLayout(19, 19));
        for (int i = 0; i < 361; i++)
        {
            jpnlGrid.add(new JButton("" + i));
        }
// End of new code
       
        jbtnLoadGrid.setText("Load Grid");
        jbtnLoadGrid.addActionListener(new java.awt.event.ActionListener() {
        ...
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 9610966
Anyone else like to implement my suggestion? ;-)
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9611034
You seem to be a bit grumpy today CEHJ ;-)
0
 
LVL 15

Expert Comment

by:Javatm
ID: 9611111
We're just elaborating it more, to be unturstood well if it okey with you :) CEHJ
0
 

Author Comment

by:entercite
ID: 9611405
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
0
 
LVL 15

Expert Comment

by:Javatm
ID: 9611487
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("sample1.gif"));

button.setPressedIcon(createImageIcon("sample2.gif"));
button.setRolloverIcon(createImageIcon("sample3.gif"));
button.setDisabledIcon(createImageIcon("sample4.gif"));
button.setMargin(new Insets(0,0,0,0));


Note : sample1 up to 4 are gif images.
Hope it helps, God Speed . . .

JAVATM
0
 
LVL 15

Expert Comment

by:Javatm
ID: 9611511
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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9613553
>>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);
      }
}

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9613555
>>questioner's = questioners

LOL
0
 

Author Comment

by:entercite
ID: 9615934
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.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

740 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