Results 1 to 7 of 7
  1. #1
    Khan05 is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default How to create Multiple Combobox Vertically.

    Hi Guys,
    I am trying to create multiple combobox in java Swing Vertically Each label start with new line. But I can only print them horizontal in one line. Here is my code please if you have any advice please reply to me.

    import javax.swing.*;

    import java.awt.FlowLayout;
    import java.awt.Frame;
    import java.awt.event.*;
    public class ComboTest extends JFrame {

    public static void main(String[] args) {
    JFrame frame = new JFrame("COM1");
    JPanel panel = (JPanel) frame.getContentPane();
    FlowLayout layout = new FlowLayout();
    panel.setLayout(layout);

    JLabel label1 = new JLabel("Baud");///creating label for Baud rate
    panel.add(label1);//Adding label
    String baud[] = { "2400", "4800", "9600", "19200", "38400", "57600", "115200", "31250", };// baud rate.
    //Create ComboBox
    JComboBox comboBox1 = new JComboBox(baud);// Creating combo Box
    panel.add(comboBox1);

    JLabel label2 = new JLabel("STOP");
    panel.add(label2);//Adding label
    String stop[] = {"1","1.5","2"};// Stop
    JComboBox comboBox2 = new JComboBox(stop);// Creating Combo Box
    panel.add(comboBox2);

    JLabel label3 = new JLabel("Parity");
    panel.add(label3);//Adding label
    String parity[] ={"None","Even","Odd","Mark","Space"};// /declaring Parity
    JComboBox comboBox3 = new JComboBox(parity);//Creating Combo Box
    panel.add(comboBox3);

    JLabel label4 = new JLabel("DATA");
    panel.add(label4);//Adding label
    String data[] ={"5","6","7","8"};// declaring Data
    JComboBox comboBox4 = new JComboBox(data);//Creating Combo Box
    panel.add(comboBox4);

    JLabel label5 = new JLabel("Flow Control");
    panel.add(label5);//Adding label
    String flowControl []= {"None","Xon","Xoff"};
    JComboBox comboBox5 = new JComboBox(flowControl);//Creating Combo Box
    panel.add(comboBox5);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setVisible(true);
    frame.pack();
    frame.setSize(550,250);

    }

    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    FlowLayout only lays things out on a line, so you'll need to use a different layout such as BoxLayout. The tutorials will show you how to use this.

  3. #3
    Khan05 is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default

    Thanks for replying my post. I made some change and I am using BoxLayout. But I want print first show label and then show ComboBox. but label is comming on Top and ComboBox next line.
    I want like this Baud 2400 // Select one Baud rate
    Stop 1 //
    Parity None//
    Each line start with label and then show ComboBox option. Please reply to me.

    Thanks

    Khan

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Or SpringLayout.

    db

  6. #6
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    I threw this together recently. It arranges JPanels vertically in a BoxLayout. Each JPanel uses FlowLayout, and the width of each component is adjusted to simulate columns. Just tell the constructor how many columns you want, and start adding Components.

    Java Code:
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.util.LinkedList;
    
    import javax.swing.BoxLayout;
    import javax.swing.JPanel;
    
    public class SimpleColumnPanel extends JPanel {
    	private static final long serialVersionUID = 1L;
    	
    	protected Object[] columns;
    	protected int column;
    	protected JPanel row;
    
    
    	public SimpleColumnPanel(int columns) {
    		if(columns < 1) throw new IllegalArgumentException("columns must be > 0");
    		this.columns = new Object[columns];
    		for(int i = 0; i < columns; ++i) {
    			this.columns[i] = new LinkedList<Component>();
    		}
    		column = 0;
    		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    	}
    	
    	@SuppressWarnings("unchecked")
    	@Override
    	public Component add(Component component) {
    		if(column == 0) {
    			row = new JPanel(new FlowLayout(FlowLayout.LEFT));
    			super.add(row);
    		}
    		row.add(component);
    		((LinkedList<Component>)columns[column]).add(component);
    		
    		// align this column unless it's the last one
    		if(column != columns.length - 1) {
    			int widest = 0;
    			for(Component comp : (LinkedList<Component>)columns[column]) {
    				int width = comp.getPreferredSize().width;
    				if(width > widest) widest = width;
    			}
    			for(Component comp : (LinkedList<Component>)columns[column]) {
    				comp.setPreferredSize(new Dimension(widest, comp.getPreferredSize().height));
    			}
    		}	
    		
    		++column;
    		column %= columns.length;
    		
    		return component;
    	}
    }
    It looks like this. The labels and input widgets are in the SimpleColumnPanel. The buttons are separate.

    Get in the habit of using standard Java naming conventions!

  7. #7
    gauravfzd's Avatar
    gauravfzd is offline Member
    Join Date
    Oct 2010
    Location
    India
    Posts
    72
    Rep Power
    0

    Default

    You can also try GroupLayout to keep the components in place as you want horizontally and vertically.
    For more detail check the GroupLayout API documentation.....

Similar Threads

  1. Replies: 14
    Last Post: 06-29-2011, 04:49 PM
  2. layout struts 2 forms components vertically
    By saleh_neu in forum Web Frameworks
    Replies: 1
    Last Post: 01-12-2011, 10:30 AM
  3. create multiple receiver
    By bhatti in forum Eclipse
    Replies: 0
    Last Post: 11-30-2010, 10:01 AM
  4. Create jar from multiple jars
    By rummy in forum New To Java
    Replies: 2
    Last Post: 01-06-2010, 04:03 PM
  5. Multiple ComboBox - Reading Text
    By jorgelex008 in forum AWT / Swing
    Replies: 6
    Last Post: 01-05-2010, 12:27 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
  •