Hi all

I've programmed a relay chat system in Java. Just having two GUI-related problems with the chat client itself.

Problem #1 : I can't get the chat windows to scroll as new text appears in them. The scroll bar does appear when it needs to, but it remains at the top of the box (but grows smaller as more text is appended) ...so the newly appended text is hidden unless you scroll the scrollbar further down yourself.
This is what I'm doing:
I have a JTextArea for the text, then I have a JScrollPane to which I've added the JTextArea.
Then I've added the JScrollPane to (the contentPane of) the JFrame.
I have the vertical-scroll-bar policy of the JScrollPane set to ALWAYS.
I've also set the viewport of the JScrollPane to my JTextArea.

How do I get the scrollbar scrolling to keep up with text being added to the chat window?

Problem #2
I'm using JFrames for the chat client windows. I want to prevent the chat client user from re-sizing the windows. How do I do this??

I'm not sure about your first problem, I've been wondering that myself. But I do know how to solve your resizing problem...

I think that you just want to use this:

JFramename.setResizable(false);

-Nick Nisi

use javax.swing.JScrollPane and then just add your text area to this

stupidenator: Thanks for that! I searched the JFrame API for the word 'resize' and nothing came up, so I was at a loss. :cheesy:

paradox814: I did that, as I've mentioned up there. It just didn't work.

remember that JFrame is derived from Frame which derives from Component (or something like that), many core methods are inherited from a parent class.

yep. Thanks jtwenting. Remember you helped test my chat system? ... :cheesy:

Any idea on the scrollable chat windows? A chat client with non-scrollable windows is... pretty much a disaster.

I had exaclty the same problem when i made a chat system for uni using a JEditorPane in a scroll window, its one of those problems which is really annoying. Im not sure if I actually fixed it but i know you can do it as some of the other students ones worked (but a lot didnt). Have you tried anchoring it to the bottom of the scroll pane or something similar, might have something to do with setEditable aswell. if the text area is editable it might not scrol properly, it may work correctly if it isnt editable, just a few ideas.

From the top of my head, you should set the caret position after you update the text :
jta.append("Some text!");
jta.setCaretPosition(jta.getText().length()); Have fun.

I remember trying that in the app i wrote, i dont think it will work, as the caret position can be on the text area but not necessarily in the view. If it does work it would be interesting to know fo rthe future.

JScrollPane is your friend :)

From the top of my head, you should set the caret position after you update the text :
jta.append("Some text!");
jta.setCaretPosition(jta.getText().length()); Have fun.

hi...

I tried this. It seems to have helped just a little, because instead of never showing new text, the scroll pane now remains just one text line behind i.e. the newest line of text appended is always hidden.

I need the scrollpane to keep up with the newest line of text. What to do??

jwenting, spill it. :cry:

hi...
I tried this. It seems to have helped just a little, because instead of never showing new text, the scroll pane now remains just one text line behind i.e. the newest line of text appended is always hidden.

How about this kludge (not tested at all, so...):

if (jta.getText().length() > 0) {
jta.insert("Some text",jta.getText().length()-1); // add the new text before the linefeed.
} else {
jta.append("Some text!");
jta.append(System.getProperty("line.separator")); // efectively, add a linefeed after the first line.
}
jta.setCaretPosition(jta.getText().length());

Maybe this works. I'll test some code if i have some time this weekend to see if it can be done a bit nicer. (Should be possible.)

Sorry, my bad. I seem to have added the code to one append point and not the other. oops.

The point being... the original code worked! Thanks DeepZ. Much appreciated.

The original code as in...

jta.append("Some text!");
jta.setCaretPosition(jta.getText().length());

.. not my original code. :mrgreen:

Hi,

I have problems with showing the menu items. I wrote a kind of calculator program. When I open the file menu the first open item can be seen on the text field but the other ones I think goes under the panel and they can't be seen. :evil: :evil: :evil: :cry: :cry:

Thanks

don't hijack other peoples' threads, post your own.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.