Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

A very easy Java Swing question

Options
  • 20-04-2004 3:10pm
    #1
    Registered Users Posts: 2,102 ✭✭✭


    Hey all,

    I'm a CS graduate, and in the last year I'm happy to say I've forgotten EVERYTHING I learned! This is an easy one.

    I'm trying to refresh my Java, so I've started out VERY simple. I'm writing a text editor! Excitement eh. Basically my problem comes in with the JTextField i'm using. It's set up using:

    theTextField = new JTextArea("Enter your text here.", 20, 65);
    theTextField.setLineWrap(true);
    theTextField.setWrapStyleWord(true);
    theTextField.setEditable(true);
    theTextField.setFont(new Font("Times-Roman", Font.PLAIN, 14));

    scrollpane = new JScrollPane(theTextField);
    scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);


    I want the text field to fill the pane it's in no matter if the window is resized. I assumed there'd be some kind of getWidth() method in JPanel, which is initialised as follows:

    j = new JPanel();
    getContentPane().add(j);

    I cannot for the life of me figure this out, there's some SERIOUS gaps in my knowledge :confused:

    // THIS is the line i need fixed:
    theTextField = new JTextArea("Enter your text here.", 20, 65);


    Thanks in advance.

    Ross :ninja:


Comments

  • Registered Users Posts: 491 ✭✭Silent Bob


    Have you even considered looking at the Swing tutorial or the Java API documentation?


  • Closed Accounts Posts: 234 ✭✭MagicBusDriver


    Use JBUILDER to get available methods along with the Java api documentation.


  • Registered Users Posts: 2,102 ✭✭✭RossFixxxed


    No I just post here for the fun of it and not bother to google around at all. :rolleyes:

    Yes, yes I have. Gone through the tutorials, gone through the documentation. Just can't figure it out. I'm seriously missing a lot of basic knowledge.

    I wrote an image watermarking servlet a few months back, and now I can't even remember how to manipulate basic swing components.
    Sigh.

    Ross


  • Registered Users Posts: 491 ✭✭Silent Bob


    Originally posted by RossFixxxed
    I assumed there'd be some kind of getWidth() method in JPane
    Originally posted by RossFixxxed
    Yes, yes I have. Gone through the tutorials, gone through the documentation

    Had you read the API you would have known that there is a getWidth() method

    Similarly you would know that there is a getHeight() method and another method of all JComponents that allows you to setPreferredSize()


  • Registered Users Posts: 2,102 ✭✭✭RossFixxxed


    Well thanks for being both no help and ignorant. I'm gonna go somewhere else and ask this one. I know those methods are there now, must have missed them. I seem to make human mistakes, my bad. I still can't get them to work. Like I said I've forgotten a lot, and I've got huge gaps in my knowledge.

    See ya.
    Ross


  • Advertisement
  • Closed Accounts Posts: 2,951 ✭✭✭L5


    Theres a turorial on building a text editor on the java sun site if thats any help.


  • Registered Users Posts: 2,102 ✭✭✭RossFixxxed


    Oh. Dear. God. Well I'm a retard. LOL! Thanks L5. Work is killing
    my brain!

    :o


  • Registered Users Posts: 491 ✭✭Silent Bob




  • Registered Users Posts: 2,102 ✭✭✭RossFixxxed


    RTFM MOFO. j00 ist 133t. Well done. Hackers are so terribly lame it scares me. I thought this was a polite irish website, where I could ask a pretty straight forward question as best I can.

    Anyway, outta here, I'll go ask elsewhere. Lock the thread I say.


  • Registered Users Posts: 491 ✭✭Silent Bob


    Funny, the link I posted is from the forum FAQ.


  • Advertisement
  • Registered Users Posts: 2,426 ✭✭✭ressem


    From Java RAD dependent pre-newbie, so apologies if the jargon is wrong.

    Shouldn't you be relying on the layout to manage the resizing for you, assuming you're not adding anything too complex.

    getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

    I cheated and drew up the enclosed java file using NetBeans.


    Results in the jTextArea and jScrollPane filling the Frame anyways.

    Cheers,


  • Closed Accounts Posts: 1,322 ✭✭✭phobos


    Originally posted by ressem
    Shouldn't you be relying on the layout to manage the resizing for you
    The whole way down this thread I was saying that to myself. Constructing a GUI using Swing or even AWT is rediculous without implementing some form of layout management, especially if you give your users the freedom to resize the containers that host your various GUI widgets.

    Don't go off and find a tutorial on how to build a text editor. Since you're a CS graduate, I recommend you go back and revise properly, instead of relying on a hack, and not understanding fully what you're doing.

    Here's Sun's tutorial on Layout Management. I highly recommend you give it a read. If you understand that much, you will be able to tackle your problem with ease.

    Best of luck ;)

    ;-phobos-)


  • Registered Users Posts: 2,102 ✭✭✭RossFixxxed


    Thanks a million guys! All fixed up now :)

    I should just get some books out and revise from scratch. I just have these gaps in my knowledge that get in the way, it'd be like doing maths and being really good at complicated calculus but you can't multiply fractions right (or somesuch).

    I always prefer to go hack something together and look up everything as I go. However as I think I just demonstrated that it's a crap idea if you hit a wall with something simple. I totally missed the layout manager etc... :dunno:

    Thanks again! Can't believe I missed/forgot all the layout manager stuff.

    I'm going to sign up for a free java course in work and get my programming on!

    (deleted)

    Ross


  • Registered Users Posts: 19,396 ✭✭✭✭Karoma


    for somebody who keeps leaving you're here an awful lot. That FAQ meant "hack" in the same way you meant "hack" ... *sigh*
    Please be fluffier.


  • Registered Users Posts: 2,426 ✭✭✭ressem


    Ahh, go easy on 'im

    He sounds like he hasn't yet realised that those graduation caps are designed to remove all the university copyrighted notes from his head before going into employment and is still miffed at being reduced to fresher status, unable to defend himself from trolls and spoofers, lower in the eyes of his colleagues than the PointyHairedBoss as they suspect that he's a manager in disguise, with a mission to cheer on the introduction of instant coffee and plain biccies.

    I think the secret is that they turn the tinfoil lining inside out.
    :eek:


  • Registered Users Posts: 2,102 ✭✭✭RossFixxxed


    Yeah I came back, decided to stay for a bit. I'm quite happy to be reduced to fresher status here, like i said i've forgotten everything. Really really got turned off computers by college, so I've taken a year away from them, but need money so I've come crawling back.

    Trollers and flamers are NO problem. There's only 1 thing to remember:
    1) It's the Internet, it's not real.

    BTW if my question was badly put, or not 133t enough for j00, then just let it pass by. Just read it and say 'no, I have no valid contribution to that.' Rather than get the way everyone on computers gets these days "I have sooooo much knowledge, look at that rere asking such a stupid question. I @M 50 l337 pH3@r mY $kIlLz." When basically they don't know the answer to the question themselves.

    This is surprisingly common in businesses etc. Computer people need to seem so much more knowledgeable about these things, or else everyone will realise "hey, this stuff's really ****in easy!" And then we'll be put up against the wall.

    Anyhoo, I better get back to work.

    7@lk tO @Ll 0F U g3n75 L@73R, JoLlY G00d 5H0W
    R055 7h3 l337 hAcKeR h@h@h@h@


  • Registered Users Posts: 491 ✭✭Silent Bob


    Every post here provided something helpful, some required you to do more work on your own then others.

    You seem to be expecting people to drop everything and just give you a complete answer straight off. That doesn't help you and it doesn't help us (same reason that it's better to give a fishing rod to a hungry man than a fish).

    And the way you phrase a question is important.

    In your first post you pretty much say (whether you intended to or not):
    • I'm a CS graduate, that makes me great
    • I know this problem is easy, help me anyway
    • I have gaps in my knowledge but don't seem to have made any effort to address that

    What kind of reaction do you expect to get? All people are looking for is some kind of evidence that you at least tried to find a solution (which it turns out you did), but this wasn't evident from your question.


  • Registered Users Posts: 2,102 ✭✭✭RossFixxxed


    Ok what I meant was:



    I'm a CS graduate, that makes me a loser. But a loser with an understanding of programming, so the answer won't totally confuse me. Also a CS degree means sweet F.A. to me. I certainly wasn't trying to be cocky about this! Just giving background of what Java I might know.

    I know this problem is easy. It's easy but for the life of me I can't solve it. It seems like something small.

    I have gaps in my knowledge ... I've explained that I'm TRYING to address it.



    You can't tell my tone from what I write, just take the message, which I tried to make as polite as possible as it is without reading things into it.

    I just wanted my text box to work. I feel like a total retard trying to remember this stuff (it's not my bag tbh but I used to be ok at it and i needed it to work).


  • Registered Users Posts: 491 ✭✭Silent Bob


    Originally posted by RossFixxxed
    I certainly wasn't trying to be cocky about this! Just giving background of what Java I might know.
    I apologise for mis-interpreting :)


  • Registered Users Posts: 2,102 ✭✭✭RossFixxxed


    Why can't we all just... get along!

    :) Tis all good. I'll fight that Java. With Science!


  • Advertisement
Advertisement