Results 1 to 6 of 6
Thread: Swing layout is done wrong
- 10-07-2010, 06:54 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 41
- Rep Power
- 0
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:
As you can see, I am using a GridBagLayout to layout the components.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); } }
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?
- 10-07-2010, 07:06 PM #2
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
Onra,
Just quickly looking through but it looks like you have forgot to set gridwidth for the textfield.
Regards.
-
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.
- 10-07-2010, 08:54 PM #4
Member
- Join Date
- Nov 2009
- Posts
- 41
- Rep Power
- 0
-
You'll usually place a JList into a JScrollPane. I suppose you could set the preferred size of the JScrollPane.
- 10-08-2010, 04:36 AM #6
Similar Threads
-
Swing Html Layout
By Kiruthigadhandapani in forum AWT / SwingReplies: 1Last Post: 05-03-2011, 03:39 PM -
Add Swing Layout.jar
By anilkumar_vist in forum New To JavaReplies: 0Last Post: 10-02-2010, 04:42 AM -
Edit layout Layout please help me
By manhtungtnk28@gmail.com in forum New To JavaReplies: 4Last Post: 11-23-2009, 08:41 AM -
Wrong output (well.. the one who's wrong is probably me ;) )
By shacht1 in forum New To JavaReplies: 2Last Post: 11-22-2009, 03:48 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks