Results 1 to 5 of 5
  1. #1
    Markgm is offline Member
    Join Date
    Nov 2010
    Location
    VT, USA
    Posts
    31
    Rep Power
    0

    Default Left-aligned controls in a list

    Hi,
    I tried a BoxLayout (within another, within another) for an approach to what I want to do, and having some trouble. I do not know why some things do not want to left-align here. I would like it if I could rely on being able to pile on 6 or 8 controls per panel, where they would be on the left margin all the way down (but the I want to use emptyBorder to add indentation on the left end as well). But when I put a JSpinner in there, it would not line up on the left. I also see that when I put a control in a panel and put the panel in the panel, it is not the same left-right as if I just put the control in the (outer) panel. Here's a pic...


    And the code that made it. What should I do to get it on the left margin going down? (And add some indenting of radio buttons, check boxes, and spin controls, or a jlabel+spin control line as one line, with this left-margin-related behavior?)

    TIA -
    Mark
    Java Code:
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    
    import javax.swing.BorderFactory;
    import javax.swing.Box;
    import javax.swing.BoxLayout;
    import javax.swing.ButtonGroup;
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JComboBox;
    import javax.swing.JComponent;
    import javax.swing.JDialog;
    import javax.swing.JFormattedTextField;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.JSpinner;
    import javax.swing.JTextField;
    import javax.swing.SpinnerModel;
    import javax.swing.SpinnerNumberModel;
    import javax.swing.border.Border;
    
    public class CreateGameDialog extends JDialog implements ActionListener, KeyListener {
    
    	private static final long serialVersionUID = 1L;
    	private JFrame parentFrame;
    
    	public CreateGameDialog(JFrame parentFrame) {
    		super(parentFrame,true);
    		this.parentFrame = parentFrame;
    		this.setTitle("Create Game");
            //this.setPreferredSize(new Dimension(600,250));
    		//this.setLocationRelativeTo(parentFrame);
    		this.setLocation(parentFrame.getLocation().x+10, parentFrame.getLocation().y+100);
    		Container cp = getContentPane();
            
    		// NEW INTERFACE
    		JPanel gameAndDealPanel = new JPanel();
    		gameAndDealPanel.setLayout(new BoxLayout(gameAndDealPanel, BoxLayout.Y_AXIS));
    		//gameAndDealPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
    		//gameAndDealPanel.setAlignmentX(CENTER_ALIGNMENT);
    		//gameAndDealPanel.setAlignmentY(LEFT_ALIGNMENT);
    		
    		// GAME panel
    		JPanel gamePanel = new JPanel();
    		gamePanel.setLayout(new BoxLayout(gamePanel, BoxLayout.X_AXIS));
    	
    		// Game RULES panel
    		JPanel rulesPanel = new JPanel();
    		rulesPanel.setLayout(new BoxLayout(rulesPanel, BoxLayout.Y_AXIS));
    		rulesPanel.setAlignmentX(LEFT_ALIGNMENT);
    		
    		JRadioButton radioButton1 = new JRadioButton("Radio button 1");
    		JRadioButton radioButton2 = new JRadioButton("Radio button 2");
    		ButtonGroup rulesGroup = new ButtonGroup();
    		rulesGroup.add(radioButton1);
    		rulesGroup.add(radioButton2);
    		rulesPanel.add(radioButton1);
    		rulesPanel.add(radioButton2);
    
    		JCheckBox checkBox1 = new JCheckBox("Lower ascend");
    		JCheckBox checkBox2 = new JCheckBox("Upper ascend");
    		checkBox1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    		rulesPanel.add(checkBox1);
    		rulesPanel.add(checkBox2);
    		
    		//rulesCheckBoxes[5].setBorder(BorderFactory.createLineBorder(Color.BLACK));
    		JPanel panel5 = new JPanel();
    		panel5.setLayout(new BoxLayout(panel5, BoxLayout.X_AXIS));
    		panel5.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    		panel5.add(checkBox1);
    		rulesPanel.add(panel5);
    		
    		JPanel newPanel = new JPanel();
    		newPanel.setLayout(new BoxLayout(newPanel, BoxLayout.X_AXIS));
    		JCheckBox newBox = new JCheckBox("New one");
    		newPanel.add(newBox);
    		newPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    		rulesPanel.add(newPanel);
    
    		SpinnerModel spinModel2 = new SpinnerNumberModel(10,8,12,1); // value, min, max, step
    		JSpinner spinner2 = new JSpinner(spinModel2);
    		JComponent editor2 = new JSpinner.NumberEditor(spinner2, "##");
    		spinner2.setEditor(editor2);
    		spinner2.setMaximumSize(spinner2.getMinimumSize());
    		rulesPanel.add(spinner2);
    		
    		//rulesPanel.add(rulesPanel1);
    		gamePanel.add(rulesPanel);
    
    		cp.add(gamePanel);
    		cp.setPreferredSize(cp.getPreferredSize());
    	}

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    See the following link: Box (Java Platform SE 6)
    I recommend playing with some of the methods and nesting some of the layouts. For example, add checkBox1 to a horizontal Box, then add a horizontal glue to said box, and add that Box to the parent JPanel.

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    You will want to make sure all your components have the same alignment:

    Java Code:
    component.setAlignmentX(Component.LEFT_ALIGNMENT);
    See the section from the Swing tutorial on Fixing Alignment Problems for more information.
    Last edited by camickr; 04-19-2011 at 02:15 AM.

  4. #4
    Markgm is offline Member
    Join Date
    Nov 2010
    Location
    VT, USA
    Posts
    31
    Rep Power
    0

    Default

    I've tried a few things. I have added horizontal glue, for example, on either and on both ends.

    Who can post code, such as a dialog constructor, entirely comprised of a check box and a spinner, so that each control start on the left edge (aligned there)?

    We'll say a check box with the text "Hello world", and a spinner with the numbers (10,8,12,1) for value,min,max,step.

    I haven't got that, but would love to see it. Or maybe I'm just going about it the wrong way.

    TIA -
    Mark

    I'll try left-alignments for each control next (and panel?)
    Last edited by Markgm; 04-19-2011 at 02:24 AM. Reason: read next response

  5. #5
    Markgm is offline Member
    Join Date
    Nov 2010
    Location
    VT, USA
    Posts
    31
    Rep Power
    0

    Default

    I just had success by setting alignmentX to LEFT for all the controls and all of the panels. Thanks for that. (And the links)

    Cheers,
    Mark

Similar Threads

  1. square moves left and down but not up or left
    By natdizzle in forum AWT / Swing
    Replies: 3
    Last Post: 02-04-2011, 05:20 PM
  2. Activex Controls
    By vivek123 in forum SWT / JFace
    Replies: 2
    Last Post: 11-04-2009, 12:22 PM
  3. Controls
    By zzpprk in forum AWT / Swing
    Replies: 2
    Last Post: 11-04-2009, 02:32 AM
  4. Tabs Won't Work In Right Aligned JPanel
    By davidedwardgill in forum AWT / Swing
    Replies: 0
    Last Post: 08-08-2008, 07:45 PM

Posting Permissions

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