how to remove AWT label in window frame - java

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.

Related

How to add line breaks to prompt text in JavaFX?

I have a TextArea that has some prompt text that I want to be split onto a few different lines, however, line breaks don't work in prompt text for some reason.
Code:
TextArea paragraph = new TextArea();
paragraph.setWrapText(true);
paragraph.setPromptText(
"Stuff done today:\n"
+ "\n"
+ "- Went to the grocery store\n"
+ "- Ate some cookies\n"
+ "- Watched a tv show"
);
Result:
As you can see, the text does not line break properly. Does anyone know how to fix this?
The prompt is internally shown by a node of type Text which can handle line breaks. So the interesting question is why don't they show? Reason is revealed by looking at the promptText property: it is silently replacing all \n with empty strings:
private StringProperty promptText = new SimpleStringProperty(this, "promptText", "") {
#Override protected void invalidated() {
// Strip out newlines
String txt = get();
if (txt != null && txt.contains("\n")) {
txt = txt.replace("\n", "");
set(txt);
}
}
};
A way around (not sure if it is working on all platforms - does on my win) is to use the \r instead:
paragraph.setPromptText(
"Stuff done today:\r"
+ "\r"
+ "- Went to the grocery store\r"
+ "- Ate some cookies\r"
+ "- Watched a tv show"
);

How can i show a Queue in a JLabel?

I have a question. I have a queue and I want to show it in JLabel text, but I dont know how put the complete data from the queue in just one JLabel, I mean. I know the method JLabel.setText(), but each time I show a new data from the queue the JLabel refresh and then the data that I put before of that dissapear, and I want to show the complete Queue in a JLabel spacing the data, like this but in a JLabel...
for(int i=0;i<Queue.length;i++);{
{ System.out.print(Queue.push()+" ");}
and my problem is when I try to show the another data pushing the Queue, the JLabel refresh the text...
for(int i=0;i<Queue.length;i++);{
{ JLabel.setText(Queue.push()+" ");}
there is a method to show it correctly? thank you!.
You need to collect all values in a string variable and then set it.
String text = "";
for(int i=0;i<Queue.length;i++){
text += Queue.push()+" ";
}
JLabel.setText(text.trim());
Probably it would be better to show it as HTML. So you can make the line-break
String text = "<html>";
for(int i=0;i<Queue.length;i++){
text += Queue.push()+"<br>";
}
text += "</html>"
JLabel.setText(text);
You have to add the text to the existing text:
for(int i=0;i<Queue.length;i++){
JLabel.setText(JLabel.getText() + Queue.push()+ " ");
}
Or you can store the data and set it to the JLabel at the end:
String s = "";
for(int i=0;i<Queue.length;i++){
s = (s + Queue.push() + " ")
}
JLabel.setText(s);
Hope it helps.

How do i append a new text at JLabel?

I read, some StackOverflow questions that I need to use HTML stuffs.
But what would be the easiest it without any of HTML stuff.
Here's the code
label.setText(label.getText() + (String)boxTimes.getSelectedItem() + input);
This code will produce this
What I want is:
You must know a bit of basic String format:
\n line break
\t tab
So your code will be like:
String myLabel =
// 4
label.getText() + "\n\n" +
// 7:00
(String)boxTimes.getSelectedItem() + "\t" +
// - Going out....
"- " + input;
label.setText(myLabel);
But as long as JLabel does not accept \n as Abishek Manoharan pointed, you must use <br>.
String myLabel =
"<html>" +
label.getText() +
"<br/><br/>" +
(String)boxTimes.getSelectedItem() + " - " + input +
"</html>;
label.setText(myLabel);
I was faced with the same problem too and couldn't find a viable solution.
So I went ahead and used a JTextArea instead of JLabel.
JTextArea label = new JTextArea();
label.setEditable(false);
label.setBackground(null);
It gives the same look and feel of a JLabel
You can use '\n' and '\t' as you like,
and what more, the text is selectable which is not possible in JLabel.

jTextPane color with some exception in chat

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);

How to save information from JTable to a File?

Here is my case:
So far my group and I, managed to read information from an external file and place it in a JTable. But we need an update button. So we guess we should take all the information from JTable after editting something inside it, and replace it with the current information in the same file. So we kind of think we have to overwrite the old file.
So far we got this: (for int i... is a part of the code but can't get it inside the grey area :P)
for(int i = 0; i < model.getRowCount(); i++) {
p += model.getValueAt(i, 0) + " "
+ model.getValueAt(i, 1) + " "
+ (Integer) model.getValueAt(i, 2) + " "
+ model.getValueAt(i, 3) + " "
+ (Integer)model.getValueAt(i, 4) + " "
+ model.getValueAt(i, 5) + " "
+ model.getValueAt(i, 6) + " "
+ model.getValueAt(i, 7) + " "
+ (Integer)model.getValueAt(i, 8) + "\n";
}
// Update File
SaveMember sm = new SaveMember();
sm.update(p);
Inside our SaveMember.java we got:
public void update(String x) throws Exception {
File f = new File("Members/Members.txt");
PrintStream output = new PrintStream(f);
output.print(x);
So by now when we go and change the data and press the button update, it doesn't do anything at all, and doesn't replace the old data with the new.. Thanks for reading! :)
I'm not sure. If you have double checked that your code is executed at all (maybe you forgot to attach the ActionListener to your button - we all do that from time to time...) try flush the output stream and close the stream afterwards.
First check if your code in the for loop is executed at all. Set a breakpoint after the for loop and inspect the string p. If you are not familiar with debugging, print the string to the console with System.out.println(p).
If your code is NOT executed: Check why the method your code is in is not called. Perhaps you forgot to attach an action listener to your update button or the action listener has an early return under some circumstances.
If your code is executed: What do you do with the exception that is thrown by the method update? Make sure to log it with your logger or print it to the console (again via System.out.println(exc)). If you get a FileNotFoundException the path to the file is not correct.

Categories