Re: A couple of fairly basic questions to get my layout working well
From: ak (spam_at_imagero.com)
Date: 11/27/03
- Next message: ak: "Re: JComboBox problem, pse help!"
- Previous message: Xavier Tarrago: "Re: setLocation problem"
- In reply to: cfineman_at_yahoo.com: "A couple of fairly basic questions to get my layout working well"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 27 Nov 2003 10:31:46 +0100
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,
> 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));
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
<cfineman@yahoo.com> schrieb im Newsbeitrag
news:1069876215.314136@newshost03.voicenet.com...
> 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);
> }
> }
>
- Next message: ak: "Re: JComboBox problem, pse help!"
- Previous message: Xavier Tarrago: "Re: setLocation problem"
- In reply to: cfineman_at_yahoo.com: "A couple of fairly basic questions to get my layout working well"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]