Witam, napisałem sobie komunikator ale mam problem. Gdy ktoś napisze wiadomość i wysle ją do kogoś to mam problemy z przesunięciem ScrollBara na sam dół JTextArea. Wkleje kod a linie zakomentowane to te którymi eksperymentowałem by uzyskać poprawny efekt.
package Gui.Priv;
// tutaj są wszyskie importy
public class Window extends JFrame
{
JTextArea textLog;
JTextArea textEdit;
JButton buttonExec;
String name;
String title;
static String TITLE_HEAD = "Rozmowa z ";
int newMsgs;
JScrollPane sp;
static JToggleButton buttonAutoEnter = new JToggleButton( new ImageIcon( "Gui"
+ File.separator + "Priv" + File.separator + "key_enter.png" ), true );
public Window( String nick )
{
buttonAutoEnter.setMargin( new Insets( 0, 1, 0, 1 ) );
name = nick;
newMsgs = 0;
this.title = TITLE_HEAD + nick;
textLog = new JTextArea( 14, 10 );
textLog.setEditable( false );
textLog.setLineWrap( true );
textEdit = new JTextArea( 4, 10 );
textEdit.addKeyListener( new TextEditKeyEvents( this ) );
buttonExec = new JButton( "Wy¶lij" );
buttonExec.addActionListener( new ButtonActionListener( this ) );
setTitle( title );
setSize( 350, 400 );
// panel.setResizable( false );
Box box = Box.createVerticalBox( );
add( box );
sp = new JScrollPane( textLog );
//sp.setAutoscrolls( true );
sp.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED );
box.add( sp );
box.add( Box.createVerticalStrut( 15 ) );
box.add( new JScrollPane( textEdit ) );
JPanel boxSend = new JPanel( );
boxSend.setLayout( new FlowLayout( FlowLayout.RIGHT ) );
boxSend.add( buttonExec );
boxSend.add( buttonAutoEnter );
box.add( boxSend );
setDefaultCloseOperation( JFrame.HIDE_ON_CLOSE );
setVisible( false );
}
public void addMessage( String nameFrom, String message )
{
String head = "<" + nameFrom + " " + getCurrentTime( ) + ">\n";
textLog.append( head + message + "\n" );
// newMsgs++;
if ( !isFocusable( ) )
generateTitle( );
setFocusable( true );
setVisible( true );
//sp.getVerticalScrollBar( ).setValue( sp.getVerticalScrollBar( ).getMaximum( ) );
}
public void addMessage( String message )
{
String head = "<" + name + " " + getCurrentTime( ) + ">\n";
textLog.append( head + message + "\n" );
newMsgs++;
generateTitle( );
setFocusable( true );
setVisible( true );
//sp.getVerticalScrollBar( ).setValue( sp.getVerticalScrollBar( ).getMaximum( ) );
}
void generateTitle( )
{
String title = "";
if ( newMsgs > 0 )
title += "[ " + newMsgs + " ] ";
if ( newMsgs < 1 )
title += "";
title += TITLE_HEAD + ' ' + name;
setTitle( title );
}
public static String getCurrentTime( )
{
String result = "";
Date d = new Date( );
int x;
x = d.getHours( );
if ( x < 10 )
result += "0";
result += new Integer( x ).toString( );
result += ":";
x = d.getMinutes( );
if ( x < 10 )
result += "0";
result += new Integer( x ).toString( );
result += ":";
x = d.getSeconds( );
if ( x < 10 )
result += "0";
result += new Integer( x ).toString( );
return result;
}
public static boolean isAutoEnter( )
{
return buttonAutoEnter.isSelected( );
}
}
Oraz eventy:
public class TextEditKeyEvents extends KeyAdapter implements KeyListener
{
Window parent;
boolean altPushed;
TextEditKeyEvents( Window parent )
{
this.parent = parent;
altPushed = false;
}
public void keyPressed( KeyEvent arg0 )
{
if ( arg0.getKeyCode( ) == KeyEvent.VK_ALT )
altPushed = true;
}
public void keyReleased( KeyEvent arg0 )
{
if ( arg0.getKeyCode( ) == KeyEvent.VK_ALT )
altPushed = false;
if ( arg0.getKeyChar( ) == '\n' )
{
if ( parent.textEdit.getText( ).length( ) == 1 )
{
parent.textEdit.setText( "" );
return;
}
if ( Window.isAutoEnter( ) )
sendMsg( );
else
{
if ( altPushed )
sendMsg( );
}
}
}
private void sendMsg( )
{
String s = Main.check( 0, parent.textEdit.getText( ), 0 );
if ( s.length( ) == 0 )
return;
ComBigPack pack = new ComBigPack( Flags.MA_SENDTO_ONE, 0 );
pack.write( s );
Main.WINDOW_MANAGER.send( parent.name, pack );
parent.newMsgs = 0;
parent.addMessage( Main.LOGIN, s );
parent.textEdit.setText( "" );
parent.generateTitle( );
//parent.sp.getVerticalScrollBar( ).setValue( parent.sp.getVerticalScrollBar( ).getMaximum( ) );
}
}
Zawsze nie przesuwa na sam dół, raz u nadawcy raz u odbiorcy,
Prosze o odpowiedzi bo męcze sie z tym i nie moge rozwiązania znaleźć
P.S: Mam wrażenie że tak jakby on najpierw wklejał wiadomośc a potem przesuwał, mimo iż kolejność instruckji w kodzie jest inna. Próbowałem nawet po dodaniu wiadomości zrobić currentThread.sleep(100) ale nic nie dało.
Z góry thx :-)