Results 1 to 6 of 6
  1. #1
    Onra is offline Member
    Join Date
    Nov 2009
    Posts
    41
    Rep Power
    0

    Default Swing layout is done wrong

    Hello,

    I have a custom JPanel which represents a simple chat user interface.
    With userlist, messages and input.
    Here is the code so far:

    Java Code:
    public class ChatPanel extends JPanel{
    	
    	private JTextArea ta_chat;
    	private JList l_users;
    	private JScrollPane sp_chatscroll, sp_listscroll;
    	private JTextField tf_chatenter;
    	private Vector<String> m_userlist;
    	
    	public ChatPanel(){
    		ta_chat = new JTextArea(20,50);
    		m_userlist = new Vector<String>();
    		l_users = new JList(m_userlist);
    		tf_chatenter = new JTextField();
    		initView();
    	}
    	
    	private void initView(){
    		this.setLayout(new GridBagLayout());
    		ta_chat.setAutoscrolls(true);
    		ta_chat.setLineWrap(true);
    		ta_chat.setEditable(false);
    		ta_chat.setFocusable(false);
    		sp_chatscroll = new JScrollPane(ta_chat);
    		GridBagConstraints formatter = new GridBagConstraints();
    		formatter.anchor = GridBagConstraints.FIRST_LINE_START;
    		formatter.gridx = 0;
    		formatter.gridy = 0;
    		this.add(sp_chatscroll, formatter);
    		l_users.setVisibleRowCount(20);
    		l_users.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    		sp_listscroll = new JScrollPane(l_users);
    		formatter.gridx = 1;
    		this.add(sp_listscroll, formatter);
    		tf_chatenter.setColumns(65);
    		formatter.gridx = 0;
    		formatter.gridy = 1;
    		formatter.fill = GridBagConstraints.HORIZONTAL;
    		this.add(tf_chatenter, formatter);
    	}
    }
    As you can see, I am using a GridBagLayout to layout the components.
    However, they are not being lain out as I want them to.

    This is how it looks like:


    I want the list, which is on the right, to be next to the textarea and additionally over the right part of the textfield, like you have it in generic chat UIs.

    Can anyone help me?

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default

    Onra,

    Just quickly looking through but it looks like you have forgot to set gridwidth for the textfield.

    Regards.

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

    Default

    Myself, I'd skip using the complex GridBagLayout but instead nest JPanels, creating an innter JPanel using BorderLayout and having the JTextArea BorderLayout.CENTER and the list BorderLayout.EAST. Then the outer one can also use BorderLayout and place the inner JPanel BorderLayout.CENTER and the JTextField BorderLayout.SOUTH.

  4. #4
    Onra is offline Member
    Join Date
    Nov 2009
    Posts
    41
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Myself, I'd skip using the complex GridBagLayout but instead nest JPanels, creating an innter JPanel using BorderLayout and having the JTextArea BorderLayout.CENTER and the list BorderLayout.EAST. Then the outer one can also use BorderLayout and place the inner JPanel BorderLayout.CENTER and the JTextField BorderLayout.SOUTH.
    This works.

    Any tip on how I can make a JList thinner? For a text area I can set less columns, but I can't find a useful method for JList.

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

    Default

    You'll usually place a JList into a JScrollPane. I suppose you could set the preferred size of the JScrollPane.

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

    Default

    Quote Originally Posted by Onra View Post
    As you can see, I am using a GridBagLayout to layout the components.
    However, they are not being lain out as I want them to.

    I want the list, which is on the right, to be next to the textarea and additionally over the right part of the textfield, like you have it in generic chat UIs.
    Set colspan=2 for the constraint of the text field.

    You may want to set weightx and weighty, which determine how any extra space is distributed when the GUI is resized.

    db

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 02:37 AM
  2. Swing Html Layout
    By Kiruthigadhandapani in forum AWT / Swing
    Replies: 1
    Last Post: 05-03-2011, 04:39 PM
  3. Add Swing Layout.jar
    By anilkumar_vist in forum New To Java
    Replies: 0
    Last Post: 10-02-2010, 05:42 AM
  4. Edit layout Layout please help me
    By manhtungtnk28@gmail.com in forum New To Java
    Replies: 4
    Last Post: 11-23-2009, 09:41 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
  •