I want to add data from JTextField but if I pressed the process again, the data that I write is missing. Is there any solution for this code?
try{
txttujuan.removeAll();
int xxx=0;
String XNilai,Nilai;
for (xxx=0;xxx <jtable.getRowCount();xxx++){
if (jtable.getValueAt(xxx,4).equals("1")){
Nilai ="0"+ (String) jtable.getValueAt(xxx,2);
XNilai+=Nilai+",";
} else continue;
System.out.print(XNilai.substring(0,XNilai.lastIndexOf(",")));
jtextfield.setText();
jtextfield.setText(XNilai.substring(0,XNilai.lastIndexOf(",")));
XNilai="";
}
} catch (Exception e){
System.out.print(e);
}
my data i've already wrote is missing
That is what the setText() method does. It replaces the existing text with the new text.
Instead of using a JTextField you can try using a JTextArea, then you can use the following to add text to the end:
textArea.append( "some text" );
If you really want to use a JTextField then you can use:
Document doc = textField.getDocument();
doc.insertString("some text", doc.getLength(), null);
to append text to the text field.
Related
I have a function to place my text to the document into something like a table.
private static void addCenteredParagraph(Document document, float width, String text) {
PdfFont timesNewRomanBold = null;
try {
timesNewRomanBold = PdfFontFactory.createFont(StandardFonts.TIMES_BOLD);
} catch (IOException e) {
LOGGER.error("Failed to create Times New Roman Bold font.");
LOGGER.error(e);
}
List<TabStop> tabStops = new ArrayList<>();
// Create a TabStop at the middle of the page
tabStops.add(new TabStop(width / 2, TabAlignment.CENTER));
// Create a TabStop at the end of the page
tabStops.add(new TabStop(width, TabAlignment.LEFT));
Paragraph p = new Paragraph().addTabStops(tabStops).setFontSize(14);
if (timesNewRomanBold != null) {
p.setFont(timesNewRomanBold);
}
p.add(new Tab()).add(text).add(new Tab());
document.add(p);
}
But my problem is it shows empty characters in the exported PDF instead of the letters ő,Ő,ű,Ű.
The Times New Roman supports these characters, so I think I need to set it to UTF8, but I couldn't find a workaround on Google to do it.
Can someone please explain how to make these characters appear properly on the pdf?
Tried these, but some of them are deprecated functions, or not applicable with my arguments I'm trying to give them, or I don't use Chunk.
Itext PDF writer, Is there any way to allow unicode subscript symbol in the pdf? (Without setTextRise)
How can I set encoding for iText when I insert value to placeholder in pdf form?
Edit: I figured out, that timesNewRomanBold is null, even if I set it to HELVETICA, or COURIER.
I am building a tradebot, and I created my own UI. I gather data about my account via the API, and setText into a text field (as image shows). What I want to do, is be able to create a new line using the commas as a break, or be able to make some sort of excel table to separate the data and make it easy to use.
When I call for the data, it just dumps everything into a long, line of text.
Any suggestions on how to achieve this?
thanks,
Arthur
#FXML
void onPositionClick(ActionEvent event)
{
Context ctx = new Context("https://api-fxpractice.oanda.com", "XXXXXXXXXXXXXXXXXXXXXX-YYYYYYYYYYYYYYYYYYYYYYYY");
try
{
AccountSummary summary = ctx.account.summary(
new AccountID("101-XXX-XXXXXX-XXX")).getAccount();
String summaryString = String.valueOf(summary);
displayResults.setText(summaryString);
} //end try
catch (Exception e)
{
e.printStackTrace();
}//end catch
I would use System.lineSeparator() because its system-dependent:
String text = "abc,de,fghi,jklmn,o";
String newText = text.replaceAll(",", System.lineSeparator());
System.out.println(newText);
Output:
abc
de
fghi
jklmn
o
So everytime someone clicks sign up on my program, I call a method that opens file and adds record.
This is to open the file:
try {
l = new Formatter("chineses.txt");
System.out.println("Did create");
} catch (Exception e){
System.out.println("Did not create");
}
public void addRecord(){ //This is how i add the record
l.format("%s", nameField.getText());
}
Everytime I put in a name in the name field and click sign up in my gui, it always replaces whatever is on the first line in the text file.
How can I make it go to the second line while retaining what is on the first line each time someone else puts their name and clicks sign up?
You just need to create the object of FileWriter and pass it to the Formatter. It will append your text in the File.
Try this code:
FileWriter writer = new FileWriter("chineses.txt",true);
l = new Formatter(writer);
public void addRecord(){ //This is how i add the record
l.format("%s\n", nameField.getText());
}
This question already has answers here:
JTextField in which the predefined text in not editable but other text can be appended to it?
(5 answers)
Closed 7 years ago.
I am trying to create a TextArea using java that contains text that cannot be deleted/updated. That being said, the user can still add to the TextArea and delete their own text.
I've tried to play around with getting the length of the TextArea and listening for the 'backspace' keypress however I can still select all the text in the TextArea and replace it.
Example Code
(TA is the TextArea)
TA.setText(cmdx);
cmdLength = TA.getDocument().getLength();
TA.getKeymap().addActionForKeyStroke(
KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, 0), new AbstractAction() {
public void actionPerformed(ActionEvent e) {
TA.disable();
try {
backSpace();
} catch (BadLocationException ex) {
Logger.getLogger(CmdFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
AND
public void backSpace() throws BadLocationException {
System.out.println("Backspace is pressed");
int currentLength = TA.getDocument().getLength();
if(currentLength > cmdLength)
{
Document doc = TA.getDocument();
doc.remove(doc.getLength() - 1, 1);
TA.enable();
}
}
I apologize if this question has already been asked.
Thank you in Advance!
Don't let the user directly type in text but catch the respective event and manually set the text to the textField. Then you can implement a check (for example if the text is starting with your sticky text) and if not add it at the beginning again. If it does then simply add the typed key at the end of the text.
Look here for more solutions
I am new to java.
I have a project from college where I have to make entries to txt file through 2 JTextField boxes and 1 JButton (save) which will display the entries in JTextArea. I am able to make entries in txt file successfully. But how to refresh JTextArea at run-time to display the new entries I recently made?
Thanks for helps:
below is my code:
try {
//use buffering, reading one line at a time
//FileReader always assumes default encoding is OK!
BufferedReader input = new BufferedReader(new FileReader("RokFile.txt"));
try {
String line = null; //not declared within while loop
while (( line = input.readLine()) != null){
jTextArea1.append(line+"\n");
}
}
finally {
input.close();
}
}
catch (IOException ex){
ex.printStackTrace();
}
Let me know if its correct?
Thanks
JTextArea.append ought to suffice. This method is thread-safe and will update the text area's content automatically.
This answer assumes that you already have the EventListeners configured.
You can use two methods,
If you want to display the content as soon as you write in jTextField(fairly attainable), you can do it this way, in the FocusLost event of jTextField, give something like jTextArea.setText(jTextField.getText())
Note, that this is fairly near to what you want.(also,NOT perfect code)
If you want to display the contents when you click save , the above code, jTextArea.setText(jTextField.getText()) may be given in the event handler of the save button.