TLDR: Insert an image in a JTextArea that will be generated afterwards.
I'm new on using Java, doing my first real project. I am trying to make an application that creates documents based on input variables that represent the keywords in a "template". Basically I created on the left of the GUI a few JTextFields that I print out in the JTextArea with append and then I generate the document.
What I am trying to do atm is to insert an image in that append, image that will be in the generated document.
The code so far for what I specified below.
JTextArea append, where I would like to insert the image:
private void ADDActionPerformed(java.awt.event.ActionEvent evt) {
txtReceipt.append(
"\t\t CONTRACT DETAILS \n\n" +
"LEASE NUMBER:\t\t\t" + txtContract.getText() +
"insert text template here"
);
}
Between the lines of text I would like to add the image. The txtContract variable is a JTextField and the txtReceipt is the JTextArea.
Generate button:
private void GENERATEActionPerformed(java.awt.event.ActionEvent evt) {
try {
txtReceipt.print();
} catch (PrinterException ex) {
Logger.getLogger(GeneratorContracte.class.getName()).log(Level.SEVERE, null, ex);
}
}
I found another threads on this subject but they were adding images in an already created document or generating a doc. The problem is that I didn't figure out what line of the codes does print the image. Also, I don't know if JTextArea supports images.
I am using NetBeans if that matters.
Sorry for the wall of text, but I tried to specify the issue without posting the whole code. I believe this way makes it easier to find a solution.
Thanks for your time guys!
Related
I'm trying to make an interface for a login/register app and I have, in the email box (jTextField) an example as text (example#gmail.com) but when I run my program when I click that box to write my email on it, I have to delete my set text to write what I want.
What I thought to do was to create 2 jTextFields, the one behind not editable and the one forward where I'd put my text. So there are two things I don't know how to do:
put the forward jTextField invisible so we can see the behind
one
make the text on the behind jTextField disappear when I click the front one
Thanks for trying the help.
Can easily done with FocusGained and focuseLost events
private void txtEmailFocusGained(java.awt.event.FocusEvent evt) {
if (txtEmail.getText().equals("example#example.com")) {
txtEmail.setText(null);
}
}
private void txtEmailFocusLost(java.awt.event.FocusEvent evt) {
if ( txtEmail.getText().equals("")) {
txtEmail.setText("example#example.com");
}
}
Is there any way to connect google search to jtextfield, i want to create a software that can search directly through a jtextfield and the results would show up on a different JPanel. i found a code that will make it search on google directly but you need an existing browser to do it, i just want the results to appear on a different panel, for example when you search something on the textfield the results will popup on another panel.
this is the code that i found but it needs an existing browser i want the software to be standalone thats all thank you:
try {
String search = "#q="+jTextField.getText().toString().trim();
search = search.replaceAll(" ","+");
String url = "http:////www.google.com//"+search;
java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
}
catch (java.io.IOException e) {
System.out.println(e.getMessage());
}
If you're not interested in writing your own browser you could use this.
I'm trying to programatically fill in a template that can be moved around and then the file uploaded to my program. I've been searching for a while and there seems to be only old answers where the old API allowed a simple setText like this:
designHandle.findElement("ValueName").setText("text");
I also found out that ITextItemInstance is able to setText like I want, however it only seems to be used when creating new instances of labels, not editing existing ones.
I'm using Birt 4.5.0
From the book Integrating and Extending BIRT page 296:
//Access the label by name.
LabelHandle headerLabel = (LabelHandle) design.findElement("Header Label");
try {
headerLabel.setText("Updated " + headerLabel.getText());
} catch (Exception e) {
// Handle the exception
}
design being a ReportDesignHandle object.
In the project I am currently working on I have several pieces of information that I would like to make visible via Jlabel. There are a few buttons and textfields elsewhere in the GUI that allow for altering said information I would like to update the JLabel but the text never changes, or updates upon startup.
I have attempted to use concurrency to update the labels, as suggested in other questions on this site, but I have had no luck with the labels updating. The concurrency does work with updating textfields and comboBoxes as needed.
The current iteration of my code looks as follows,
The JFrame
//// This is a snippet from the JFrame
public void start()
{
this.setSize(900, 700);
this.setVisible(true);
devicePanel.populateDeviceDefinitions();
updateServiceInfo();
updateCommandInfo();
startUpdateTimer();
}
public void updateServiceInfo()
{
EventService service = JetstreamApp.getService();
generalPanel.updateServiceInfo(service.getBaseUri(),
service.getAccessKey(), String.valueOf(service.getWindowTime()));
}
public void updateCommandInfo()
{
JetstreamServiceClient client = JetstreamApp.getClient();
generalPanel.updateCommandInfo(client.getBaseUri(), client.getAccessKey());
}
The JPanel named generalPanel
//// This is a snippet from the generalPanel
//// All of the variables in the following code are JLabels
public void updateServiceInfo(String baseUrl, String accessKey,
String windowTime)
{
serviceUrl.setText(baseUrl);
serviceAccessKey.setText(accessKey);
serviceWindowTime.setText(windowTime);
}
public void updateCommandInfo(String baseUrl, String accessKey)
{
commandUrl.setText(baseUrl);
commandAccessKey.setText(accessKey);
}
The labels start with an Empty string for their text and upon window start it is intended that they be updated by grabbing the information from the relevant sources. Can I please have some insight as to why the JLabels never update and display their information?
How did you create the JLabel? If the text starts out as "", and you've created it with new JLabel(""), the width of the JLabel may be initialized to 0 and then none of your text would show up when you update it. I believe I've had that sort of problem in the past. As a test, try using new JLabel("aaaaaaaaaa") or some longer string to create the label, then setText(""); then later, when you setText(somethingElse), see if that causes text to show up. If it does, then the width is probably the problem and you can work on it from there. – ajb 19 mins ago
This comment is the actual answer, when creating a JLabel with an empty string as the text the label's dimensions do not get set properly when using WindowBuilderPro. My labels did exist, and were being updated with the code provided in my question but the labels were not visible.
Starting with a label that has text in it, then setting the text to an empty string works properly.
The method paintImmediately() can be used to cause a Swing component to get updated immediately. after setText(), you should call paintImmediately() like below.
jLabel.setText("new text")
jLabel.paintImmediately(jLabel.getVisibleRect());
You should try to call revalidate() or repaint() on the component that contains your JLabels.
Cheers
I want to create a special Password Dialog for my eclipse product, which is used with an on screen keyboard.
It would be very nice, if i could use a component like the IPhone Password field. In this field, the added character is shown for a second and after the second it is converted into the '*' character for hiding the complete password.
Did a jar/library exists, this is implemented in AWT or SWT?
Edit:
I could trying to implement it from scratch (SWT), but for these i would have to create a very special and complicated KeyListener for the password Text component. I would have to catch the keyReleased event and set the characters manually into the field.
So far i was not able to find any libraries in the web. Suggestion how this can be implemented are welcome too.
This is not really a full answer, rather than a discussion starter and I don't know of any out-of-the-box widgets which can do that.
My first idea was to inheriting the swt Text widget and overriding setEchoChar et al., but after looking at the code this doesn't really seem feasible, because this method is merely a wrapper around:
OS.SendMessage (handle, OS.EM_SETPASSWORDCHAR, echo, 0);
If anyone would know the OS specific low-level implementation, that might be helpful.
Anyway, on to a different approach. I would avoid the KeyListener and use a ModifyListener on the Text-Widget.
void addModifyListener(ModifyListener listener)
You could then build a wrapper which catches the entered text using this listener, appends it to a locally held string/stringbuffer (or e.g. the Eclipse Preferencestore) and send a modified full text to the Text widget using setText(String s), replacing all characters except the last by an echo character (e.g. *).
myText.setText((s.substring(0, s.length()-1)).replaceAll("[\\s\\S]","*")+s.charAt(s.length()-1));
This is a bit of a kludge, but it should work.
The not so straightforward bit is the 1 second timing, without stalling the whole view...
Depending on what Jules said the following code is some kind of working.
The code is quick and fast and i would like to have a more thread safe solution.
originalString = new StringBuffer();
passwordField.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
synchronized (passwordField) {
String s = passwordField.getText();
String newS = s.replaceAll("[\\s\\S]", "*");
if (newS.equals(s)) {
while (originalString.length() > s.length()) {
originalString = originalString.deleteCharAt(originalString.length() - 1);
}
usernameField.setText(originalString.toString());
return;
}
if (originalString.length() < s.length()) {
originalString.append(s.charAt(s.length() - 1));
}
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
}
passwordField.setText(newS);
}
passwordField.redraw();
passwordField.setSelection(passwordField.getText().length());
}
});
Key Events are cached, so you can add more characters, also when the Thread is waiting.
Another Problem is the Cursor handling. the Cursor always moves to the first position, when you set the Text.
I think when this is working it is very near to the iphone solution.