homer242
Messages postés13Date d'inscriptionmercredi 29 janvier 2003StatutMembreDernière intervention30 septembre 2007
-
7 sept. 2003 à 01:02
Syruis
Messages postés181Date d'inscriptionmardi 8 juin 2004StatutMembreDernière intervention28 avril 2009
-
16 juil. 2004 à 10:38
yop, me revoilà avec un problème assez énervant....
j'ai construit un messenger, avant j'utilisais un TextArea à la place du JTextPane, tout marchait niquel ... et voilou marche plus avec JTextPane.... le JScrollPane ne se met pas automatiquement en bas lorsqu'il y a un nouveau message donc j'ai trafiqué grace à duss et des sources sur le net mais c très aléatoire, ça peut marcher bien 10 messages puis le jscrollpane ne veut plus aller TOTALEMENT en bas, il se positionne 1 ligne au dessus...c énervant !!...voiçi le code de la classe qui permet d'afficher des "personals messages" :
thx pour tous ceux qui essayeront de m'aider.....
je suis pret à changer tout le code s'il le faut :)
la class socket est pour la transfere des messages en réseau...il y a 2 methodes se ressemblant pour afficher le message, un sert pour l'utilisateur qui est en local(couleur du pseudo rouge) et l'autre pour celui qui repond en réseau (couleur bleu)....
package Lmessenger;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Hashtable;
import java.util.TimeZone;
import java.util.Calendar;
import java.util.Vector;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
public class LmessengerSENDPMSG extends JFrame {
private int width = 440;
private int height = 440;
private Container contentpane;
private JScrollPane chatscroller;
private JScrollPane tf_area_scroller;
private JTextPane chat_area;
private StyledDocument doc;
private SimpleAttributeSet style;
private Hashtable styles;
private JTextArea tf_area;
private JPanel inputpanel;
private JLabel inputlabel;
private JButton sendboutton;
private String from_user;
private String to_user;
private LmessengerSOCKET Socket;
private LmessengerGUI gui_principal;
private insertmsg imsg;
String[] inttostring = { "heure",
"minute",
"secondes",
"jour",
"mois",
"annee" };
public LmessengerSENDPMSG(String L_to_user, String L_from_user, LmessengerSOCKET L_socket, LmessengerGUI L_g) {
super("Messages ("+L_to_user+")");
this.gui_principal = L_g;
this.Socket = L_socket;
this.from_user = L_from_user;
this.to_user = L_to_user;
Socket.SetDialogueSendMessage(this, to_user);
initGui();
}
public void initGui() {
try
{
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}
catch(Exception e)
{}
JMenuBar menubar = new JMenuBar();
menubar.add(createActionsMenu());
menubar.add(createHistoryMenu());
setJMenuBar(menubar);
doc = new DefaultStyledDocument();
initStyles();
chat_area = new JTextPane(doc);
chat_area.setContentType("text/plain");
chat_area.setEditable(false);
chat_area.setFont(new Font("Verdana",Font.PLAIN,10));
chat_area.setMargin(new Insets(2,4,2,4));
chatscroller = new JScrollPane(chat_area,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
tf_area = new JTextArea(4,0);
tf_area.setLineWrap(true);
tf_area.setFont(new Font("Verdana",Font.PLAIN,11));
tf_area.setBorder(BorderFactory.createEtchedBorder());
tf_area.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(KeyEvent ke) {
if(ke.getKeyCode() == KeyEvent.VK_ENTER) {
if(ke.isShiftDown()) {
getTf_area().append("\n");
} else {
ke.consume();
sendboutton.doClick();
getTf_area().requestFocus();
}
}
}
});
tf_area_scroller = new JScrollPane(tf_area,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
inputlabel = new JLabel("message :");
sendboutton = new JButton("Envoyer", new ImageIcon("send.gif"));
sendboutton.setMnemonic(KeyEvent.VK_E);
sendboutton.setFont(new Font("Verdana",Font.BOLD,11));
sendboutton.addActionListener( new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
if(!tf_area.getText().equals(""))
{
String temp_msg = tf_area.getText();
sendmessage(temp_msg);
}
}
});
sendboutton.setActionCommand("Send");
inputpanel = new JPanel();
inputpanel.setLayout(new BorderLayout());
inputpanel.add(inputlabel,BorderLayout.NORTH);
inputpanel.add(tf_area_scroller, BorderLayout.CENTER);
inputpanel.add(sendboutton, BorderLayout.SOUTH);
contentpane = getContentPane();
contentpane.setLayout(new BorderLayout());
contentpane.add(chatscroller, BorderLayout.CENTER);
contentpane.add(inputpanel, BorderLayout.SOUTH);
setContentPane(contentpane);
Toolkit tools = Toolkit.getDefaultToolkit();
Dimension screenSize = tools.getScreenSize();
setSize(width,height);
setLocation((screenSize.width-width)/2,(screenSize.height-height)/3);
this.addWindowListener( new WindowAdapter(){
public void windowClosing(WindowEvent e){
Vector vec_temp = Socket.getDialogueSendMessageVecteur();
int index = 0;
for (int i=0;i<vec_temp.size();i++) {
Object[] tab = (Object[]) vec_temp.elementAt(i);
String user_temp = (String) tab[1];
if (user_temp.equals(from_user)) { index=i; break; }
}
Socket.getDialogueSendMessageVecteur().removeElementAt(index);
}
});
setVisible(true);
tf_area.requestFocus();
chatscroller.getVerticalScrollBar().setMaximum(999999999);
}
private String getHeureEtDate() {
Calendar now = Calendar.getInstance();
int[] datec = { now.get(Calendar.HOUR_OF_DAY),
now.get(Calendar.MINUTE),
now.get(Calendar.SECOND),
now.get(Calendar.DAY_OF_MONTH),
now.get(Calendar.MONTH),
now.get(Calendar.YEAR) };
for (int i=0; i<6;i++) {
if (datec[i]<10) { inttostring[i] = "0"+datec[i]; }
else { inttostring[i] = ""+datec[i]; }
}
String datecomplete = new String ("( "+inttostring[3]+"-"+inttostring[4]+"-"+inttostring[5]+" "+inttostring[0]+":"+inttostring[1]+":"+inttostring[2]+" )");
return datecomplete;
}
public void sendmessage(String temp_msg) {
Socket.parseOutcomingLine(LmessengerVALUES.USERCMD_PMSG+" "+from_user+">"+to_user+" "+temp_msg);
appendChatText(from_user + " ", "nick_local");
appendChatText(getHeureEtDate()+"\n", "date");
appendChatText(temp_msg + "\n\n", "message");
this.adjustViewport();
this.clearTf_area();
tf_area.requestFocus();
}
public void receivemessage(String temp_msg) {
appendChatText(to_user + " ", "nick_com");
appendChatText(getHeureEtDate()+"\n", "date");
appendChatText(temp_msg + "\n\n", "message");
this.adjustViewport();
tf_area.requestFocus();
}
private void initStyles()
{
styles = new Hashtable();
String fontfamily = (new Font("Verdana",Font.PLAIN,10)).getFamily();
int fontsize = (new Font("Verdana",Font.PLAIN,10)).getSize();
style = new SimpleAttributeSet();
StyleConstants.setFontFamily(style, fontfamily);
StyleConstants.setFontSize(style, fontsize-1);
StyleConstants.setBackground(style, new Color(255,255,255));
StyleConstants.setForeground(style, new Color(130,130,130));
StyleConstants.setBold(style, false);
StyleConstants.setItalic(style, true);
styles.put("date",style);
style = new SimpleAttributeSet();
StyleConstants.setFontFamily(style, fontfamily);
StyleConstants.setFontSize(style, fontsize+1);
StyleConstants.setBackground(style, new Color(255,255,255));
StyleConstants.setForeground(style, Color.red);
StyleConstants.setBold(style, true);
StyleConstants.setItalic(style, false);
styles.put("nick_local",style);
style = new SimpleAttributeSet();
StyleConstants.setFontFamily(style, fontfamily);
StyleConstants.setFontSize(style, fontsize+1);
StyleConstants.setBackground(style, new Color(255,255,255));
StyleConstants.setForeground(style, Color.blue);
StyleConstants.setBold(style, true);
StyleConstants.setItalic(style, false);
styles.put("nick_com",style);
style = new SimpleAttributeSet();
StyleConstants.setFontFamily(style, fontfamily);
StyleConstants.setFontSize(style, fontsize);
StyleConstants.setBackground(style, new Color(255,255,255));
StyleConstants.setForeground(style, Color.black);
StyleConstants.setBold(style, false);
StyleConstants.setItalic(style, false);
Float indent = new Float(6.0);
StyleConstants.setLeftIndent(style, indent.floatValue());
styles.put("message",style);
style = new SimpleAttributeSet();
StyleConstants.setIcon(style, new ImageIcon("lock.gif"));
styles.put("icon",style);
}
public String getTf_areaText()
{
return tf_area.getText();
}
public JTextArea getTf_area()
{
return tf_area;
}
public JButton getSendButton()
{
return sendboutton;
}
public void clearTf_area()
{
tf_area.setText("");
}
public void clearChat_areaPane()
{
chat_area.setText("");
}
public void appendChatText(String text, String styletype)
{
doc = chat_area.getStyledDocument();
try
{
if(styletype.equals("message"))
{
int offset = doc.getLength();
int length = text.length();
style = (SimpleAttributeSet)styles.get(styletype);
doc.insertString(offset,text,style);
doc.setParagraphAttributes(offset,length,style,false);
}
else
{
style = (SimpleAttributeSet)styles.get(styletype);
doc.insertString(doc.getLength(),text,style);
}
}
catch(BadLocationException e)
{
}
this.toFront();
}
public void adjustViewport()
{
chatscroller.getViewport().setViewPosition(new Point(0, doc.getLength()));
}
private JMenu createActionsMenu()
{
JMenu actionsmenu = new JMenu(" Actions ");
actionsmenu.setMnemonic(KeyEvent.VK_A);
JMenuItem clearchat = new JMenuItem("nettoyer la fenêtre de Chat");;
clearchat.setActionCommand("clear_chat");
actionsmenu.add(clearchat);
actionsmenu.addSeparator();
//close
JMenuItem close = new JMenuItem("Close");
close.setActionCommand("Fermer fenêtre");
actionsmenu.add(close);
return actionsmenu;
}
/*
* Creates the OptionsMenu of this Gui.
*/
private JMenu createHistoryMenu()
{
JMenu historymenu = new JMenu(" Historique ");
historymenu.setMnemonic(KeyEvent.VK_H);
//history
JMenuItem showhistory = new JMenuItem("Voir Historique");
showhistory.setActionCommand("Show History");
historymenu.add(showhistory);
return historymenu;
}
}