Java issues - changing sizes of components

Discussion in 'Computer Games and General Discussion' started by Skyline969, Apr 14, 2010.

Apr 14, 2010
  1. Skyline969
    OP

    Member Skyline969 MENUdo Afficionado

    Joined:
    Nov 18, 2008
    Messages:
    2,206
    Location:
    Saskatchewan
    Country:
    Canada
    Ok, well I've got a JFrame, and in that frame a JTextArea, a JList, a JTextField, and a JButton. The JTextArea is in its own panel with an EmptyBorder (for spacing), the JList is in its own panel with an EmptyBorder (to make some spacing between the JTextArea and JList), and the JTextField and JButton are in their own panel with an EmptyBorder and FlowLayout.
    The JTextArea is at CENTER, the JList is at EAST, and the JTextField and JButton are at SOUTH.
    The JTextArea and JList's EmptyBorders have the same top and bottom attributes.

    My problem is this:
    No matter what I do (setPreferredSize, etc), I can't get the JTextArea and JList to have the same height! It's not so noticeable on Windows, but on Linux it is. What can I change so the JTextArea and JList have the exact same height?

    Btw, it's a chat program I'm making, sort of a one-room IRC essentially. More or less it's for my classmates and I to use at school, but it's also my little project to keep me sane during the intense amount of work I have going on.

    If someone could give me a little help, I'd appreciate it.

    EDIT: Screenshots of the difference. Note the spacing between the JTextArea and the JTextField as opposed to the JList and JTextField (or JButton, as it's overtop that).

    Windows:
    [​IMG]

    Linux:
    [​IMG]
     
  2. Psyfira

    Member Psyfira Credit: 0ml. Insert tea to continue

    Joined:
    Dec 31, 2003
    Messages:
    3,887
    Location:
    England
    Country:
    United Kingdom
    It's been a really long time since I used this stuff so forgive me if I'm way off the mark, but here's a link to Sun's Java tutorial page on Swing layouts and solving layout issues: http://java.sun.com/docs/books/tutorial/ui...t/problems.html

    Specifically:
    Maybe do a bit of reading in there to check the different layout options Swing offers and their properties to see if the one you're using is the right one for the job?

    Apologies for the vague-ness, it's been a while :S
     
  3. Skyline969
    OP

    Member Skyline969 MENUdo Afficionado

    Joined:
    Nov 18, 2008
    Messages:
    2,206
    Location:
    Saskatchewan
    Country:
    Canada
    Unfortunately Psyfira, I've been up and down those tutorials and couldn't find something. I'm taking a couple classes in college (88'd my beginner one, currently 86 on my intermediate one) about Java, and I've looked at tutorials, but I can't seem to fix the problem no matter what I do.
     
  4. tj_cool

    Supervisor tj_cool Stuff

    Joined:
    Jan 7, 2009
    Messages:
    9,942
    Location:
    This planet
    Country:
    Belgium
    Maybe you should give both the same font and make them have an equal amount of rows
    Or is that like that already?

    edit: is it something like this you want?
    [​IMG]

    If so, here's the source: http://pastie.org/919052 (made with GridBagLayout)
     
  5. Skyline969
    OP

    Member Skyline969 MENUdo Afficionado

    Joined:
    Nov 18, 2008
    Messages:
    2,206
    Location:
    Saskatchewan
    Country:
    Canada
    That's exactly what I want. I've never used GridBagLayout in my courses, so I didn't use it now. However, I did fix the problem by placing the JTextArea and the JList in one single JPanel, then setting it to use... I forget which layout, but wasn't GridBagLayout... possibly FlowLayout or GridLayout. Anyways, after that was said and done, I used setPreferredSize and tweaked the widths a little. Works fine after that. My project continues!

    EDIT: Of course, mine's different in terms of sending. It first checks if the message is a command (IRC uses /, I use ./). If it's not, then it sends the message to the server. The server then sends the message to all clients connected. If the message IS a command, it executes the command which places text in the JTextArea. Either way, after that it does set the JTextField's text to "".

    Another thing I did was override the paintComponent of the JTextField, so although it's a 2 row tall field, the cursor is in the top left of the field instead of in the center like as you can see in your screenshot.
     

Share This Page