How to create a JTable cell containing Image hyperlink? - java

I'm trying to find out how to create a JTable cell which contains Image, which should be clickable like an hyperlink. I'm able to load Image using default image renderer.
Can somebody explain me how to add hyperlink (mouse listener) for the each image (cell) in the last column of my table? so that, when the image link in jTable cell is clicked, I want it to open a pop-up with some message showing the error message.
Thanks,
Chandra

To launch link in machine's default browser:
URI uri = null;
try {
uri = new URI(urlToOpen);
} catch (URISyntaxException e1) {
System.out.println("Malformed URI: " + uri);
}
Desktop desktop = Desktop.getDesktop();
try {
desktop.browse(uri);
} catch (IOException e2) {
// If the user default browser is not found, or it fails
// to be launched, or the default handler application
// failed to be launched
JOptionPane.showMessageDialog(null,
"The application could not find any compatible browser.");
}
You can do this on click of Image.
Edit based on comments:
Add listener to image and then you can open a JOptionPane or JDialog on click of Image.

Related

Open browser on top of everything

I have a small code, which is used to open the standard browser of the users PC on a specific URL.
Here is the code:
if(Desktop.isDesktopSupported())
{
try {
try {
Desktop.getDesktop().browse(new URI("https://www.example.com/"));
} catch (IOException ex) {
Logger.getLogger(Mpame.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (URISyntaxException ex) {
Logger.getLogger(Mpame.class.getName()).log(Level.SEVERE, null, ex);
}
}
Only problem is that my application which consists of a Jframe window is always on top, because it has to. But then whenever I click on the button to open the browser, the application is overlapping the browser. I was then wondering whether there is a method to have the browser open on top, like whenever you set the Jframe property "alway on top". But instead it is on the browser, so that the browser would open on top of the application.

How to set Featured Image on WordPress post page by Selenium from java?

My purpose is post with a Featured Image.
This program can post Featured Image.
But dialog doesn't close!
So I can't publish.
public void uploadThumbnail(String imgPath) {
//parent current window
String currentWindow = driver.getWindowHandle();
System.out.println(imgPath);
try {
driver.findElement(By.id("set-post-thumbnail")).click();
sleep(2000);
click(By.linkText("Upload Files"));
//Select Files
sleep(1000);
driver.findElement(By.id("__wp-uploader-id-1")).click();
//upload
driver.findElement(By.xpath("//div[7]/input")).sendKeys(imgPath);
sleep(2000);
driver.findElement(By.xpath("//div[#id='__wp-uploader-id-0']/div[5]/div/div[2]/button")).click();
} catch (Exception e) {
e.printStackTrace();
uploadThumbnail(imgPath);
}
sleep(1000);
}
How to close the dialog or how to ignore the dialog and publish?
I stacked on the page.Finally I find plugin which is "Nelio External Featured Image".
it is can set picture by URL.So I don't need upload window and dialog.
Now I can set a picture on Featured Image on WordPress by Selenium from java!!!
Thank you Nelio External Featured Image developers!!

Access Client clipboard from server in java/jsf/primefaces

Problem description : user press print screen button and then click on paste button on application. That image will be store on server.
I googled and find answer on Stack over and used following code
public Image getImageFromClipboard()
{
Clipboard systemClipboard = (Clipboard) AccessController.doPrivileged(new PrivilegedAction() {
public Object run()
{
Clipboard tempClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
return tempClipboard;
}
});
// get the contents on the clipboard in a
// Transferable object
Transferable clipboardContents = systemClipboard.getContents(null);
// check if contents are empty, if so, return null
if (clipboardContents == null)
return null;
else
try
{
// make sure content on clipboard is
// falls under a format supported by the
// imageFlavor Flavor
if (clipboardContents.isDataFlavorSupported(DataFlavor.imageFlavor))
{
// convert the Transferable object
// to an Image object
Image image = (Image) clipboardContents.getTransferData(DataFlavor.imageFlavor);
return image;
}
} catch (UnsupportedFlavorException ufe)
{
ufe.printStackTrace();
} catch (IOException ioe)
{
ioe.printStackTrace();
}
/*try {
Robot robot;
robot = new Robot();
final GraphicsConfiguration config
= GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice().getDefaultConfiguration();
final BufferedImage screenshot = robot.createScreenCapture(config.getBounds());
return screenshot;
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
return null;
}
This code work well if application is running on my machine and I press Print Screen. Image is available and store.
My problem is that when I am deploying this application on separate server and run application on another machine. When user press Print screen and then click on button in application. Server won't find any image because it look on clipboard and on server clipboard no image is available. Image is available on Client desktop clipboard.
Kindly help me to access Client clipboard from server using JSF/primefaces. Or other alternative way.
I am using primefaces 3.4, server is weblogic 10.3.5.
If your application will be running on different browsers, you will find no 100% reliable way of doing it unless you implement some specific component with some other technology like Flash.
I would really use the approach of saving the image and uploading it to the server via a normal file upload form. Else you will be having headaches with Browser security issues.
Regards

How to implement DocumentFilter that inserts URLs as hyperlinks in JTextPane?

I have a JTable which uses JTextPane as editor and renderer. I added a keyListener to the editor, that listens for "space" character and checks if the last word is URL and if it is, adds it to the editor as a hyperlink using this attribute: attrs.addAttribute(HTML.Attribute.HREF, url);. I soon figured that this won't convert URLs to hyperlinks when I paste text so I decided I need to do this using DocumentFilter.
How can I create a DocumentFilter that checks if the text about to be inserted/replaced contains URLs and if it does inserts/replaces thoose URLs with the HTML.Attribute.HREF attribute and the rest of the text as it is?
See the example http://java-sl.com/tip_autocreate_links.html
It's not necessary to use a DocumentFilter. LIstener is enough.
Just mark inserted content with a dummy attribute and then replace it with hyperlink html.
// somewhere add text reformated as html link
setText("<HTML>Click the <FONT color=\"#000099\"><U>link</U></FONT>"
+ " to go to the Java website.</HTML>");
// somewhere add a listener for clicks
addActionListener(new OpenUrlAction());
// Define uri and open action
final URI uri = new URI("http://java.sun.com");
class OpenUrlAction implements ActionListener {
#Override public void actionPerformed(ActionEvent e) {
open(uri);
}
}
// Define open uri method
private static void open(URI uri) {
if (Desktop.isDesktopSupported()) {
try {
Desktop.getDesktop().browse(uri);
} catch (IOException e) { /* TODO: error handling */ }
} else { /* TODO: error handling */ }

How to show URL as a click-able URL in Table and allow them to open in default browser?

I have Java Desktop application that displays some information in a JTable that may contain URLs with some text in some cells. How can I make only the URL click-able and allow the user to open it in a default browser if he/she clicks on it.
You can use the approach shown here in a custom TableCellEditor. Once selected, you can browse() the URI.
Addendum: You can use JEditorPane for your editor component and addHyperlinkListener() to listen for events related to the link.
JEditorPane jep = new JEditorPane();
jep.addHyperlinkListener(new HyperlinkListener() {
#Override
public void hyperlinkUpdate(HyperlinkEvent e) {
HyperlinkEvent.EventType type = e.getEventType();
final URL url = e.getURL();
if (type == HyperlinkEvent.EventType.ENTERED) {
// do desired highlighting
} else if (type == HyperlinkEvent.EventType.ACTIVATED) {
// open browser
}
}
});
here is a sample about displaying text as hyperlink: HyperLink in JTable Cell

Categories

Resources