Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / December 2003

Tip: Looking for answers? Try searching our database.

A couple of fairly basic questions to get my layout working well

Thread view: 
cfineman@yahoo.com - 26 Nov 2003 19:31 GMT
It's been a loooong time since I've done any Swing work so I have a
couple of noob questions to ask about getting the following frame to
layout just right. The code was generated with jBuilder and then
massaged in eclipse to make it self contained (if you care :-)

#1) I had to create jPanel3 and jPanel4 to get the widgets in the two
tabbed panels to appear at the top of their respective panes.  If I do
not add panel4 (commented out below) then the the gridBag seems to end
up centered in the middle of the pane. See the difference between how
tab1 and tab2 display in this app? Is that the recommended way do do
this? I would have thought there would be a better way.

#2) I would like to have the LHS of the splitPane be at, say, 75% of
the width of the window. How is this best accomplished? I could set up
something to watch for resize events on the enclosing panel and set an
explicit preferred size. Again... is this the recommended way to do this?

Thanks for your help.

    Charlie

/*
* Created on Nov 26, 2003
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/

/**
* @author fineman
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class EditorFrame extends JFrame
{
   JPanel contentPane;
   JMenuBar jMenuBar1 = new JMenuBar();
   JMenu jMenuFile = new JMenu();
   JMenuItem jMenuFileExit = new JMenuItem();
   JMenu jMenuHelp = new JMenu();
   JMenuItem jMenuHelpAbout = new JMenuItem();
   JLabel statusBar = new JLabel();
   BorderLayout borderLayout1 = new BorderLayout();
   JSplitPane jSplitPane1 = new JSplitPane();
   JTextArea jTextArea1 = new JTextArea();
   ButtonGroup buttonGroup1 = new ButtonGroup();
   JLabel jLabel4 = new JLabel();
   JPanel jPanel1 = new JPanel();
   JLabel jLabel2 = new JLabel();
   JTextField jTextField2 = new JTextField();
   JLabel jLabel1 = new JLabel();
   JLabel jLabel3 = new JLabel();
   JTextField jTextField1 = new JTextField();
   JPanel jPanel2 = new JPanel();
   JTextField jTextField3 = new JTextField();
   JTabbedPane jTabbedPane1 = new JTabbedPane();
   JLabel jLabel5 = new JLabel();
   JSlider jSlider1 = new JSlider();
   GridBagLayout gridBagLayout1 = new GridBagLayout();
   GridBagLayout gridBagLayout2 = new GridBagLayout();
   JPanel jPanel3 = new JPanel();
   JPanel jPanel4 = new JPanel();

   //Construct the frame
   public EditorFrame()
   {
       enableEvents(AWTEvent.WINDOW_EVENT_MASK);

       try {
           jbInit();
       } catch (Exception e) {
           e.printStackTrace();
       }
   }

   //Component initialization
   private void jbInit()
                throws Exception
   {
       contentPane = (JPanel) this.getContentPane();
       contentPane.setLayout(borderLayout1);
       this.setSize(new Dimension(400, 300));
       this.setTitle("Configuration Property Editor");
       statusBar.setText(" xxxxx");
       jMenuFile.setText("File");
       jMenuFileExit.setText("Exit");
       jMenuFileExit.addActionListener(new EditorFrame_jMenuFileExit_ActionAdapter(this));
       jMenuHelp.setText("Help");
       jMenuHelpAbout.setText("About");
       jMenuHelpAbout.addActionListener(new EditorFrame_jMenuHelpAbout_ActionAdapter(this));
       jTextArea1.setText("jTextArea1");
       jLabel4.setText("jLabel4");
       jPanel1.setLayout(gridBagLayout2);
       jLabel2.setText("jLabel2");
       jTextField2.setText("jTextField2");
       jLabel1.setText("jLabel1");
       jLabel3.setText("jLabel3");
       jTextField1.setText("jTextField1");
       jPanel2.setLayout(gridBagLayout1);
       jTextField3.setText("jTextField3");
       jTextField3.addActionListener(new EditorFrame_jTextField3_actionAdapter(this));
       jLabel5.setText("jLabel5");
       jPanel1.setAlignmentX((float) 0.0);
       jPanel1.setAlignmentY((float) 0.0);
       jPanel2.setAlignmentX((float) 1.0);
       jPanel2.setAlignmentY((float) 1.0);
       jMenuFile.add(jMenuFileExit);
       jMenuHelp.add(jMenuHelpAbout);
       jMenuBar1.add(jMenuFile);
       jMenuBar1.add(jMenuHelp);
       this.setJMenuBar(jMenuBar1);
       contentPane.add(statusBar, BorderLayout.SOUTH);
       contentPane.add(jSplitPane1, BorderLayout.CENTER);
       jSplitPane1.add(jTextArea1, JSplitPane.RIGHT);
       jSplitPane1.add(jTabbedPane1, JSplitPane.LEFT);
       jPanel2.add(jLabel1,
                   new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
                                          GridBagConstraints.NORTHWEST,
                                          GridBagConstraints.HORIZONTAL,
                                          new Insets(0, 0, 0, 0), 4, 4));
       jPanel2.add(jTextField1,
                   new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0,
                                          GridBagConstraints.NORTHWEST,
                                          GridBagConstraints.HORIZONTAL,
                                          new Insets(0, 0, 0, 0), 4, 4));
       jPanel2.add(jLabel2,
                   new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
                                          GridBagConstraints.NORTHWEST,
                                          GridBagConstraints.HORIZONTAL,
                                          new Insets(0, 0, 0, 0), 4, 4));
       jPanel2.add(jTextField2,
                   new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0,
                                          GridBagConstraints.NORTHWEST,
                                          GridBagConstraints.HORIZONTAL,
                                          new Insets(0, 0, 0, 0), 4, 4));
       jPanel2.add(jLabel3,
                   new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0,
                                          GridBagConstraints.NORTHWEST,
                                          GridBagConstraints.HORIZONTAL,
                                          new Insets(0, 0, 0, 0), 4, 4));
       jPanel2.add(jSlider1,
                   new GridBagConstraints(1, 3, 1, 1, 1.0, 0.0,
                                          GridBagConstraints.NORTHWEST,
                                          GridBagConstraints.HORIZONTAL,
                                          new Insets(0, 0, 0, 0), 4, 4));
       jTabbedPane1.add(jPanel1, "jPanel1");
       jTabbedPane1.add(jPanel2, "jPanel2");
       jPanel1.add(jLabel4,
                   new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
                                          GridBagConstraints.NORTH,
                                          GridBagConstraints.NONE,
                                          new Insets(0, 0, 0, 0), 4, 4));
       jPanel1.add(jTextField3,
                   new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0,
                                          GridBagConstraints.NORTH,
                                          GridBagConstraints.HORIZONTAL,
                                          new Insets(0, 0, 0, 0), 4, 4));
       jPanel1.add(jLabel5,
                   new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
                                          GridBagConstraints.NORTH,
                                          GridBagConstraints.HORIZONTAL,
                                          new Insets(0, 0, 1, 0), 4, 4));
       jPanel2.add(jPanel3,
                   new GridBagConstraints(0, 5, 2,
                                          GridBagConstraints.REMAINDER, 0.0,
                                          1.0, GridBagConstraints.CENTER,
                                          GridBagConstraints.BOTH,
                                          new Insets(0, 0, 0, 0), 0, 0));
//        jPanel1.add(jPanel4,
//                    new GridBagConstraints(0, 5, 2, 1, 0.0, 1.0,
//                                           GridBagConstraints.CENTER,
//                                           GridBagConstraints.BOTH,
//                                           new Insets(0, 0, 0, 0), 0, 0));
       jSplitPane1.setDividerLocation(222);
   }

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

   void jTextField3_actionPerformed(ActionEvent e)
   {
   }

   public static void main(String[] args)
   {
       try {
           UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
       } catch (Exception e) {
           e.printStackTrace();
       }

       Frame frame = new EditorFrame();
       frame.setVisible(true);
   }
}

class EditorFrame_jMenuFileExit_ActionAdapter implements ActionListener
{
   EditorFrame adaptee;

   EditorFrame_jMenuFileExit_ActionAdapter(EditorFrame adaptee)
   {
       this.adaptee = adaptee;
   }

   public void actionPerformed(ActionEvent e)
   {
       adaptee.jMenuFileExit_actionPerformed(e);
   }
}

class EditorFrame_jMenuHelpAbout_ActionAdapter implements ActionListener
{
   EditorFrame adaptee;

   EditorFrame_jMenuHelpAbout_ActionAdapter(EditorFrame adaptee)
   {
       this.adaptee = adaptee;
   }

   public void actionPerformed(ActionEvent e)
   {
       adaptee.jMenuHelpAbout_actionPerformed(e);
   }
}

class EditorFrame_jTextField3_actionAdapter
   implements java.awt.event.ActionListener
{
   EditorFrame adaptee;

   EditorFrame_jTextField3_actionAdapter(EditorFrame adaptee)
   {
       this.adaptee = adaptee;
   }

   public void actionPerformed(ActionEvent e)
   {
       adaptee.jTextField3_actionPerformed(e);
   }
}
ak - 27 Nov 2003 09:31 GMT
Charlie,

this is really BAD thing, to make GUI with JBuilder,
who can understand in 3 month what jLabel79 is and why is it here?

One tip about GridBagConstrains - no need to create every time a new one -
you can reuse old and only set changed props:

instead of

>         jPanel2.add(jLabel1,
>                     new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
[quoted text clipped - 6 lines]
>                                            GridBagConstraints.HORIZONTAL,
>                                            new Insets(0, 0, 0, 0), 4, 4));

you can write:

GridBagConstraints gbc = new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
                                           GridBagConstraints.NORTHWEST,
                                           GridBagConstraints.HORIZONTAL,
                                           new Insets(0, 0, 0, 0), 4, 4));

jPanel2.add(jLabel1, gbc);
gbc.grixX = 1;
gbc.weightx = 1.0;
jPanel2.add(jTextField1, gbc);

so it is clearer whats going on

> It's been a loooong time since I've done any Swing work so I have a
> couple of noob questions to ask about getting the following frame to
[quoted text clipped - 214 lines]
>     {
>         try {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
>         } catch (Exception e) {
>             e.printStackTrace();
[quoted text clipped - 50 lines]
>     }
> }
cfineman@yahoo.com - 02 Dec 2003 14:09 GMT
With all due respect, I don't need a lesson in basic coding
style thank you very much. I just used Jbuilder to quickly
play around with this stuff; I have no intention of leaving
this as it stands.

Perhaps you could answer the questions I actually asked?

> Charlie,
>
[quoted text clipped - 321 lines]
>>    }
>>}
brougham5@yahoo.com - 03 Dec 2003 02:04 GMT
>Perhaps you could answer the questions I actually asked?

Here's a hint.  When you're asking other people to give freely of their time
to help you with nothing in it for them, you might not want to be
antagonistic.  :)
ak - 04 Dec 2003 03:20 GMT
> >>#2) I would like to have the LHS of the splitPane be at, say, 75% of
> >>the width of the window.

JSplitPane.setResizeWeight(0.25);

--

____________

http://reader.imagero.com the best java image reader.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.