I am using jTextPane to use sender and receiver chat color. All works fine but javax.swing.text.DefaultStyledDocument#123456with every chat message.
here Jhon is revceiver and peter is sender
here peter is revceiver and Jhon is sender
may be I m doing some mistake in code.
Here is the code for Sender
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss\n\t dd/MM/yyyy ");
Date date = new Date();
StyledDocument doc = GUI.jTextPane1.getStyledDocument();
Style style = GUI.jTextPane1.addStyle("a style", null);
StyleConstants.setForeground(style, Color.red);
try {
doc.insertString(doc.getLength(), "\t " + "You" + " : " + GUI.getSendMessage() +("\n \t "+dateFormat.format(date)) ,style);
GUI.appendReceivedMessages(""+doc);
}
catch (BadLocationException e){}
Here is the code for Receiver
DateFormat dateFormate = new SimpleDateFormat("HH:mm:ss\ndd/MM/yyyy ");
Date datee = new Date();
StyledDocument doc1 = GUI.jTextPane1.getStyledDocument();
Style styler = GUI.jTextPane1.addStyle("a style", null);
StyleConstants.setForeground(styler, Color.blue);
try { doc1.insertString(doc1.getLength(),"recevier" ,styler);
GUI.appendReceivedMessages(fromHeader.getAddress().getDisplayName() + " : "
+ new String(request.getRawContent()) +("\n"+dateFormate.format(datee)));
}
catch (BadLocationException e){}
here is Main GUI where I get these
public void appendReceivedMessages(String s) {
try {
Document doce = jTextPane1.getDocument();
doce.insertString(doce.getLength(), s+"\n", null);
} catch(BadLocationException exc) {
}
}
This is so obvious - not sure if qualifies for an answer. Anyway
Why are you doing GUI.appendReceivedMessages(""+doc); ? that is causing the doc object's default toString to appear. Hope that helps
EDIT:
so what can I do here
I guess you can do it like this :
Note that StyledDocument's insertString API updates the view. Meaning it provides you the output you need on JTextPane so:
doc.insertString(doc.getLength(), "\t " + "You" + " : " + GUI.getSendMessage() +("\n \t "+dateFormat.format(date)) ,style);
Is sufficient to bring the output on to the text pane. Remove the call to GUI.appendReceivedMessages(""+doc);
I believe your aim is to display the message text on the text pane component - jTextPane1. you just need to update property of jTextPane1 for that. You do not need to update anything else. If you need to send the text data around, just get the text from that object and pass it around to methods that expects the value : example :
String text = jTextPane1.getDocument()
.getText(0, jTextPane1.getDocument()
.getLength());
aMethodThatExpectsAString(text);
Related
public void itemStateChanged(ItemEvent e)
{
text += "Language : ";
text += "Hindi: " + Hindi.getState();
text += " English: " + English.getState();
text += "Maths: " + Maths.getState();
label.setText("");
label.setText(text);
}
this code show new result with previous one i want updated result only not the previous one so how can i remove previous AWT label output from the frame.
just remove the + in the first line of the method.
text = "Language : ";
Please note that the way you are appending Strings is not efficient. Try using String.format() instead.
I cannot write in TextArea and I don't know what to do in this case.
Here is my code from class "Server" that is used in order to put text in TA:
public class Server implements Runnable {
.
.
.
#Override
public void run() {
while (true) {
try {
if (queue.size() !=0) {
Task task = queue.take();
String text = "Server" + Integer.toString(getIdServer()) + " is processing the task: " + Integer.toString(task.getIdTask()) + " with proc time " + Integer.toString(task.getProcessingTime());
InputShop.textArea.setText(text);
Thread.sleep(task.getProcessingTime() * 1000);
waitingTime.addAndGet(-task.getProcessingTime());
}
} catch (InterruptedException e) {
}
}
}
public void addTask(Task newTask) {
String text2 = "Task "+ Integer.toString(newTask.getIdTask()) + " arrives at s " + Integer.toString(newTask.getArrivalTime())+ " on server "+ Integer.toString(getIdServer());
InputShop.textArea.setText(text2);
queue.add(newTask);
waitingTime.addAndGet(newTask.getProcessingTime());
}
And my UI Class looks at that part with TextArea like this:
TextArea textArea = new TextArea();
textArea.setPrefWidth(300);
textArea.setPrefHeight(300);
GridPane.setConstraints(textArea, 8, 8);
gridPane.getChildren().add(textArea);
textArea.setEditable(false);
If I write "System.out.println(...);" instead of the part with "InputShot.textArea.setText(...);" everything works fine, and it prints everything on the output log of the screen.
I have been searching a lot and struggling with this problem for hours and I do not know how to manage it, so I would appreciate every help!
If I missed an important part of code, or information, please let me know in order to edit the question!
How can I automatically print without poping up dialog box or automatically accept print dialog? Here is some of my code:
if ("OUT".equals(rs.getString("empattendance"))) {
String date = dft.format(dNow);
String time = tft.format(dNow);
textArea.setText(date + "\n" + "\n" +
fullname +"\n" +
"Time In: " + time + "\n" +
"Status: "+ statusin +
"\n" +
"\n" +
"____________________\n" +
" Sign by Supervisor");
try {
//printing
Boolean complete = textArea.print();
if(complete){
}
else{
}
} catch (PrinterException ex) {
Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
}
and here's the screenshot of the current behaviour.
thanks
When I look at your code I have few thoughts before answer.
1) Do not use String. Better for comparing stuff is Enumerators I believe.
2) If you would like to set text to textArea previously create some method using StringBuilder for example which will be creating the String you would like to set. Joshua Bloch says
Item 15: minimize mutability (...) If a client requires performing expensive multi-stage operations on your class, expose them as primitive methods, or provide a mutable companion class (like StringBuilder for String).
And take a look at this topic for more.
3) To print data from textArea if I were you I would try to use this.
I believe that would help you
I get text from http://m.wol.jw.org/en/wol/dt/r1/lp-e/2014/6/26 via jsoup in my android app.
It looks like:
public static void refreshFromNetwork(Context context) {
Document document;
Elements dateElement;
Elements textElement;
Elements commentElement;
try {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
sDayURL = sURL + "/" + year + "/" + month + "/" + day;
document = Jsoup.connect(sDayURL).get();
if (document.hasText()) {
dateElement = document.select(".ss");
textElement = document.select(".sa");
commentElement = document.select(".sb");
sDate = dateElement.text();
sText = textElement.text();
sComment = commentElement.html();
sSavedForCheckingDate = sLocalDate;
savePrefs(context);
sDayURL = null;
} else {
Toast.makeText(mContext,
mContext.getString(R.string.warning_unstable_connection),
Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
System.out.println("error");
e.printStackTrace();
}
}
But there are some hrefs in text. When the cursor is on them, pops up with text frame.
I can't post images, so see it there: http://habrastorage.org/files/45e/b09/17f/45eb0917f3644bbd9e5ea2b79d98363d.png
But when I try to get text from that href (I get it from sComment with html), it returns me all the text (which displays when I click on href), not part of it, like in popup. I'm not a web developer, so I don't understand, how to get only the desired text. How can I do it?
Follow the snapshot below to get only the text on pop-up
Click the pop-up href
See the text the popup text is on the this page also, to extract only the text shown on popup simply use this class and display the contents
When you click on the link href, a new page open with the same text with red font this is the text you need as it is the pop-up text, now you have just use
String Href=Scomment.attr("href");
Document doc=Jsoup.connect(Href).get();
Element element= doc.getElementById("p101");
String dialogtext=element.text();
This is the solution to you question.
Hope it'll help you
Use sComment = commentElement.text(); instead.
I want to make a student data that saved into textfile by bufferedwriter (i've finish this part) but i also want to make update part so i make the other JForm
here is the code :
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){
String SID = atf.getText();
String FN = btf.getText();
String LN = ltf.getText();
String CN = ctf.getText();
String EM = etf.getText();
String CarNum = cartf.getText();
Calendar cal =Calendar.getInstance();
try{
FileWriter file = new FileWriter("asd.txt");
BufferedWriter buffer = new BufferedWriter(file);
buffer.write("Student ID : " + SID);
buffer.newLine();
buffer.write("First Name : " + FN);
buffer.newLine();
buffer.write("Last Name : " + LN);
buffer.newLine();
buffer.write("Contact Number : " + CN);
buffer.newLine();
buffer.write("Email : " + EM);
buffer.newLine();
buffer.write("Car Number : " + CarNum);
buffer.newLine();
buffer.write("Date Of Registration : " + cal.getTime());
buffer.close();
}
catch(IOException e){
// TODO add your handling code here:
}
JOptionPane.showMessageDialog(null, "Data has been saved");
dispose();
}
and after all , i want to make another method with JTextField
here is the code
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String NSID=NSIDx.getText();
String NFN=NFNx.getText();
String NLN=NLNx.getText();
String NC=NCNx.getText();
String NE=NEx.getText();
String NCarNum=NCarNumx.getText();}
i want to input the second code to update the first code that means i must change the value of SID become NSID
i dont know how to use private method in other private method . is it possible ?
best you use xml file to store the data.
so you can construct a structure for your data and using parsers can easily update,insert, delete also possible.
Yes, it is possible to call a private method from another private method. The notion of privacy is only for the users of the object and is not applicable for methods in the object itself. By the way, since the code is similar in both these methods, you should separate it out in another private method and call that method from these two event handlers.