Results 1 to 16 of 16
  1. #1
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default [newbie] bracketing problem

    Any ideas?

    :confused:

    [CODE]
    FontFrame.java
    package homenetwork.bkr.training;
    /** A frame that uses a group layout to arrange font selection arguments.
    * @author Administrator
    */

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class FontFrame {
    setTitle("GroupLayout Test");
    setSize (DEFAULT_WIDTH, DEFAULT_HEIGHT);

    ActionListener listener = new FontAction();

    //construct components
    JLabel faceLabel = new JLabel("Face: ");

    face = new JComboBox(new String[] {"Serif", "SansSerif", "Monospaced", "Dialog", "DialogInput"});

    face.addActionListener(listener);

    JLabel sizeLabel = new JLabel("Size: ");

    size = new JComboBox(new String[] {"8", "10", "12", "15", "18", "24", "36", "48"});

    size.addActionListener(listener);

    bold = new JCheckBox("Bold");
    bold.addActionListener(listener);

    italic = new JCheckBox("Italic");
    italic.addActionLIstener(listener);

    sample = new JTextArea();
    sample.setText("The quick brown fox jumps over the lazy dog.");
    sample.setEditable(false);
    sample.setLineWrap(true);
    sample.setBorder(BorderFactory.createEtchedBorder( ));

    pane = new JScrollPane(sample);

    GroupLayout layout = new GroupLayout(getContentPanel());
    setLayout(layout);
    layout.setHorizontalGroup(layout.createParallelGro up(GroupLayout.Alignment.LEADING)
    .addGroup(
    layout.createSequentialGroup().addContainerGap().a ddGroup(layout.createParallelGroup(GroupLayout.Ali gnment.LEADING)
    .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addGroup(layout.cre ateParallelGroup(GroupLayout.Alignment.TRAILING))) )
    .addComponent(faceLabel).addComponent(sizeLabel))
    .addPreferredGap(LayoutStyle.ComponentPlacement.RE LATED)
    .addGroup ( layout.createParallelGroup(GroupLayout.Alignment.L EADING, false)
    .addComponent(size).addComponent(face)))
    .addComponent(italic).addComponent(bold).addPrefer redGap(LayoutStyle.ComponentPlacement.RELATED)
    .addComponent(pane)
    .addContainerGap(); //FIX?
    layout.linkSize(SwingConstants.HORIZONTAL, new java.awt.Component[] {face, size});

    layout.setVerticalGroup(layout.createParallelGroup (GroupLayout.Alignment.LEADING)
    .addGroup( //FIX?
    .layout.createParallelGroup(GroupLayout.Alignment. LEADING)
    .addComponent(pane, GroupLayout.Alignment.TRAILING)
    .addGroup(layout.createSequentialGroup()
    .addGroup(layout.createParallelGroup(GroupLayout.A lignment.BASELINE)
    .addComponent(face)
    .addComponent(faceLabel))) //FIX?
    .addPreferredGap(LayoutStyle.ComponentPlacement.RE LATED)
    .addGroup(
    layout.createParallelGroup(GroupLayout.Alignment.B ASELINE).addComponent(size)
    .addComponent(sizeLabel))
    .addPreferredGap(LayoutStyle.ComponentPlacement.RE LATED).addComponent(italic, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    .addPreferredGap(LayoutStyle.ComponentPlacement.RE LATED)
    .addComponent(bold, GroupLayout.DEFAULT_SIZE,GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) //FIX?
    .addContainerGap()); //FIX?

    public static final int DEFAULT_WIDTH = 300;
    public static final int DEFAULT_HEIGHT = 200;

    private JComboBox face;
    private JComboBox size;
    private JCheckBox bold;
    private JCheckBox italic;
    private JScrollPane pane;
    private JTextArea sample;


    }

    [CODE]

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  3. #3
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    lol...I think I have a bracketing problem, but I'm not sure where.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    I can see lots of problems in your code from the beginning. I don't know what you exactly talking about.

    What's going to do here?

    Java Code:
    setTitle("GroupLayout Test");
    setSize (DEFAULT_WIDTH, DEFAULT_HEIGHT);

  5. #5
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    indeed there seem to be a lot...

    That should set the JFrame caption and size, in other similar code snippets it works.

    I'm hunting about for typos etc etc in the meantime.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    If you want to do those things in a JFrame, where you refer it in the code? Suppose to extend into your class first of all.

  7. #7
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    yeah that's what I figured out :)

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  9. #9
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    I'll post back shortly :)

  10. #10
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    Haven't figured out anything after scrutinizing the code for a bit :S I've created a demo app using a UI Builder (NetBeans), and it is working that way.

    Wish me luck :)
    Java Code:
    [B]FontFrame.java[/B]
    package homenetwork.bkr.training;
    /** A frame that uses a group layout to arrange font selection arguments.
     * @author Administrator
     */
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class FontFrame extends JFrame {
    	
    	public FontFrame()
    	{
    	setTitle("GroupLayout Test");
    	setSize (DEFAULT_WIDTH, DEFAULT_HEIGHT);
    	
    	ActionListener listener = new FontAction();
    	
    	//construct components
    	JLabel faceLabel = new JLabel("Face: ");
    	
    	face = new JComboBox(new String[] {"Serif", "SansSerif", "Monospaced", "Dialog", "DialogInput"});
    	
    	face.addActionListener(listener);
    	
    	JLabel sizeLabel = new JLabel("Size: ");
    	
    	size = new JComboBox(new String[] {"8", "10", "12", "15", "18", "24", "36", "48"});
    	
    	size.addActionListener(listener);
    	
    	bold = new JCheckBox("Bold");
    	bold.addActionListener(listener);
    	
    	italic = new JCheckBox("Italic");
    	italic.addActionListener(listener);
    	
    	sample = new JTextArea();
    	sample.setText("The quick brown fox jumps over the lazy dog.");
    	sample.setEditable(false);
    	sample.setLineWrap(true);
    	sample.setBorder(BorderFactory.createEtchedBorder());
    	
    	pane = new JScrollPane(sample);
    	
    	GroupLayout layout = new GroupLayout(getContentPane());
    	getContentPane().setLayout(layout); //fixed
    	layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
    			.addGroup(
    					layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
    							.addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING))))
    								.addComponent(faceLabel).addComponent(sizeLabel))
    							.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) //FIX? Suggestion to Add cast to method receiver does not make sense in this context.
    							.addGroup ( layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
    										.addComponent(size).addComponent(face)))
    							.addComponent(italic)
    							.addComponent(bold)
    							.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE) //no documentation?  what do these parameters mean? 
    								.addComponent(pane)
    							.addContainerGap(); //FIX? some bracketing problem?
    	
    					layout.linkSize(SwingConstants.HORIZONTAL, new java.awt.Component[] {face, size});
    					
    					layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
    							.addGroup( //FIX?  Error: The method addGroup(GroupLayout.Group) in the type GroupLayout.ParallelGroup is not applicable for the arguments ()
    									.layout.createSequentialGroup
    									.addContainerGap()
    									.addGroup (
    											layout.createParallelGroup(
    													GroupLayout.Alignment.BASELINE)
    													.addComponent(face)
    													.addComponent(faceLabel))
    													.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
    													.addGroup (
    															layout.createParallelGroup(
    																	GroupLayout.Alignment.BASELINE
    																	.addComponent(size)
    																	.addComponent(sizeLabel))
    																	.addPreferredGap(
    																			LayoutStyle.ComponentPlacement.RELATED)
    																			.addComponent(sizeLabel)
    																			.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
    																			.addComponent(italic, GroupLayout.DEFAULT_SIZE,
    																							GroupLayout.DEFAULT_SIZE,
    																							GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    																			.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
    																			.addComponent(bold, GroupLayout.DEFAULT_SIZE,GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) //FIX ...some bracketing problem?
    																			.addContainerGap()));
    															)
    											)
    	}
    	
    	private class FontAction implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    			String fontFace = (String) face.getSelectedItem();
    			int fontStyle = (bold.isSelected() ? Font.BOLD : 0)
    			+ (italic.isSelected() ? Font.ITALIC : 0);
    			
    			int fontSize = Integer.parseInt((String) size.getSelectedItem());
    			Font font = new Font(fontFace, fontStyle, fontSize);
    			sample.setFont(font);
    			sample.repaint();
    		}
    	}
    									
    	public static final int DEFAULT_WIDTH = 300;
    	public static final int DEFAULT_HEIGHT = 200;
    	
    	private JComboBox face;
    	private JComboBox size;
    	private JCheckBox bold;
    	private JCheckBox italic;
    	private JScrollPane pane;
    	private JTextArea sample;
    												
    									
    }
    The Netbeans code which is not exactly the same, but creates a few controls. The UI looks like this.
    Java Code:
    package homenetwork.bkr.training;
    
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /*
     * StyleSelection.java
     *
     * Created on May 28, 2009, 11:20:08 AM
     */
    
    /**
     *
     * @author Administrator
     */
    public class StyleSelection extends javax.swing.JFrame {
    
        /** Creates new form StyleSelection */
        public StyleSelection() {
            initComponents();
        }
    
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            jLabel1 = new javax.swing.JLabel();
            jComboBox1 = new javax.swing.JComboBox();
            jLabel2 = new javax.swing.JLabel();
            jComboBox2 = new javax.swing.JComboBox();
            jCheckBox1 = new javax.swing.JCheckBox();
            jCheckBox2 = new javax.swing.JCheckBox();
            sampleText = new javax.swing.JLabel();
            jLabel3 = new javax.swing.JLabel();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jLabel1.setText("Face: ");
    
            jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Serif", "Sans Serif", "Monospaced", "Dialog", "DialogInput" }));
    
            jLabel2.setText("Size:");
    
            jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "8", "10", "12", "15", "18", "24", "36", "48" }));
    
            jCheckBox1.setText("bold");
    
            jCheckBox2.setText("italic");
    
            sampleText.setText("The quick brown fox jumps over the lazy dog.");
    
            jLabel3.setText("Sample:");
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel3)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1)
                                .addComponent(jLabel2))
                            .addGap(58, 58, 58)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jComboBox2, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jCheckBox2)
                                    .addComponent(jCheckBox1))))
                        .addComponent(sampleText))
                    .addGap(157, 157, 157))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jCheckBox1)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jCheckBox2)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel3)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(sampleText)
                    .addGap(76, 76, 76))
            );
    
            pack();
        }// </editor-fold>
    
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new StyleSelection().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify
        private javax.swing.JCheckBox jCheckBox1;
        private javax.swing.JCheckBox jCheckBox2;
        private javax.swing.JComboBox jComboBox1;
        private javax.swing.JComboBox jComboBox2;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JLabel sampleText;
        // End of variables declaration
    
    }
    Last edited by jon80; 05-28-2009 at 05:58 PM. Reason: update

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    The code you've initially posted here are generate using the NetBeans code builder I guess, but anyhow you've misses part of the code.

  12. #12
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    Well, to be on the safe side I've uploaded a copy of my source code:

    The NetBeans Project

    The code that won't work

    Any comments of a technical nature would be appreciated.

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  14. #14
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    Good luck..

  15. #15
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Just a suggestion to jon... in the future, use an IDE for everything, thereby avoiding typos. If you currently use one that doesn't catch them, try eclipse, it's very good about catching things that will cause compiler errors.

    As for the rest of it... I have no idea...
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Newbie Help
    By mattkid in forum New To Java
    Replies: 4
    Last Post: 03-25-2009, 05:55 AM
  2. I am newbie
    By Seoplanner in forum Introductions
    Replies: 0
    Last Post: 11-11-2008, 02:22 PM
  3. :) newbie...........
    By Somitesh Chakraborty in forum Introductions
    Replies: 1
    Last Post: 08-19-2008, 10:00 AM
  4. Simple newbie problem: import 3rd party jar
    By jodyflorian in forum Eclipse
    Replies: 5
    Last Post: 05-30-2008, 05:02 AM
  5. Newbie
    By CSnoob87 in forum Introductions
    Replies: 2
    Last Post: 02-18-2008, 09:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •