Bookmark Topic Watch Topic
  • New Topic

show/hide option in JTextPane with StyledDocument  RSS feed

 
Loganathan Subramani
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi,

this is my code to show/hide time stamp in my chat window but it doesn't work..
is there any way to solve this


package ChatterPanel;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;
import javax.swing.border.Border;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JToggleButton;
import javax.swing.plaf.DimensionUIResource;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class ChatPanel extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
boolean isOnline = true;
JTextPane textPane;
JTextPane messageText;
String title = null;
String sender = null;
String receiver = null;

StyledDocument doc;

private JButton sendButton;


public ChatPanel(String sender, String title,String jid,boolean isOnline) {
this.title = title;
this.sender = sender;
this.isOnline = isOnline;
this.receiver = jid;
initComponents();
}
public void actionPerformed(ActionEvent e) {
doAction();
requestFocus();
}
public void doAction ()
{
String text = messageText.getText ().trim ();
messageText.setText ("");
requestFocus ();
if (!text.equals (""))
{
editMessage (sender,text);
}
}
SimpleAttributeSet aset = new SimpleAttributeSet();
ArrayList<TimeStamp> timeStampList = new ArrayList<TimeStamp>();

public void setTimeStampVisible(boolean flag){
for (Iterator<TimeStamp> it = timeStampList.iterator(); it.hasNext();) {
TimeStamp timeStamp = it.next();
System.out.println(flag + "setting time stamp to " + timeStamp);
if(flag){
timeStamp.show();
}else{
timeStamp.hide();
}
System.out.println(flag + "setting time stamp to " + timeStamp);
}
try {
System.out.println(doc.getText(0, doc.getLength()));
} catch (BadLocationException ex) {
Logger.getLogger(ChatPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void editMessage(String name, String body) {

try {
if(name.equalsIgnoreCase(sender)){
StyleConstants.setForeground(aset, Color.BLUE);
}else{
StyleConstants.setForeground(aset, new Color(179,4,48));
}
StyleConstants.setBold(aset, true);
String curtime = getDate(new Date(), "dd/MM/yyyy HH:mm");
TimeStamp timeStamp = new TimeStamp(curtime);
timeStampList.add(timeStamp);
doc.insertString(doc.getLength(), timeStamp+name+": ", aset);
StyleConstants.setForeground(aset, Color.BLACK);
StyleConstants.setBold(aset, false);
doc.insertString(doc.getLength(), body+"\n", aset);
textPane.setCaretPosition(doc.getLength());

this.requestFocus();
} catch (Exception ex) {
Logger.getLogger(ChatPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}


JSplitPane sptop;
JToggleButton jToggleButton;
public void initComponents(){

jToggleButton = new JToggleButton("show/hide");
sptop = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
BorderLayout borderLayout = new BorderLayout();
setLayout(borderLayout);
Border border = BorderFactory.createLineBorder(Color.lightGray);
textPane = new JTextPane();
textPane.setEditable(false);
textPane.setBorder(border);
doc = textPane.getStyledDocument();
JScrollPane jp = new JScrollPane(textPane);
jp.setPreferredSize(new DimensionUIResource(200,200));
sptop.setTopComponent(jp);
sptop.setResizeWeight(0.85);
JLabel mesgLabel = new JLabel("Send Message");
mesgLabel.setForeground(Color.gray);
JPanel panel1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
panel1.add(mesgLabel);
panel1.add(jToggleButton);
JPanel panel2 = new JPanel(new BorderLayout());
messageText = new JTextPane();
messageText.setFocusable(true);
messageText.setEnabled(true);
KeyStroke enterStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
KeyStroke ctrlEnterStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.CTRL_DOWN_MASK);
Object enterActionKey = messageText.getInputMap().get(enterStroke);
messageText.getInputMap().put(ctrlEnterStroke, enterActionKey);
Action myEnterAction = new AbstractAction() {
private static final long serialVersionUID = 1L;

public void actionPerformed(ActionEvent ae) {
System.out.println("mouse entreed..............chat pannel");
doAction();
}
};
messageText.getInputMap().put(enterStroke, myEnterAction);
JScrollPane jp4 = new JScrollPane(messageText);
jp4.setPreferredSize(new Dimension(50,200));
sendButton = new JButton("Send");
sendButton.addActionListener(this);
panel2.add(sendButton,BorderLayout.EAST);
panel2.add(jp4,BorderLayout.CENTER);
JPanel panel3 = new JPanel(new BorderLayout());
panel3.add(panel1,BorderLayout.NORTH);
panel3.add(panel2,BorderLayout.CENTER);
sptop.setBottomComponent(panel3);
sptop.setContinuousLayout(true);
add(sptop,BorderLayout.CENTER);
setForeground(Color.ORANGE);
setSize(350, 380);
setLocation(700, 500);

jToggleButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("updated");
System.out.println("is selected " + jToggleButton.isSelected());
setTimeStampVisible(!jToggleButton.isSelected());
textPane.revalidate();
textPane.updateUI();
textPane.validate();
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static String getDate (Date date,String format)
{
SimpleDateFormat df = new SimpleDateFormat (format);
String dateStr = df.format (date);
return dateStr;
}

public boolean TIMESTAME = true;

class TimeStamp{
String time;
String returnString;

public TimeStamp(String time) {
this.time = "["+time+"]";
this.returnString = new String(this.time);
}
public String toString(){
return this.returnString;
}
public void show(){
this.returnString = time;
}
public void hide(){
this.returnString = "";
}
}
public static void main(String args[]){
ChatPanel panel = new ChatPanel("nathan", "me ", "you", true);
panel.setVisible(true);
}
}
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!