Open browser on top of everything - java

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.

Related

Selenium WebDriver, TestNG with Java

I am unable to Handle the Print window/popup when I click on a Print button on the Web application. I need to be able to either Close this Window, Click on Print or Cancel. I am not sure whether this is a pop-up or a window.
Could some one help me?
see whether any web elements are visible if you hover the mouse over the popup.
if web elements are visible then its web application pop up
if no web elements are visible then its windows popup
However you can ignore the popup by sending escape key. the following code will work. i just tried, it worked.
public class demo extends parent {
WebDriver driver = new FirefoxDriver();
#Test
public void launch() throws InterruptedException {
driver.get("https://www.google.co.in");
Robot r = null;
try {
r = new Robot();
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
WebElement el = driver.findElement(By.xpath(".//*[#id='hplogo']"));
el.click();
Thread.sleep(10000);
el.sendKeys(Keys.CONTROL + "p"); // trying to invoke print pop up
Thread.sleep(10000);
r.keyPress(KeyEvent.VK_ESCAPE); //dismissing it by sending escape keys
}
}
hope you will get some idea here :)

Java sockets - weird response

I'm doing a simple server/client communication for my tic tac toe game. All it goes through actionPerformed where I first setIcon to the pressed button, then proceed my performTurn method (there is some win checking), then send the coordinates of the pressed button to the client and then i wait for the other player to send me data about his turn. all it goes well, but......... the first method that should be made (setIcon) is actually done after receiving data from the other player. it goes like this: Game.performTurn, Server.send, Server.receive, setIcon. do anyone know what java issue is it, that the setIcon method doesnt go first? thx lot
public void actionPerformed(ActionEvent e) {
if (clickable && Game.getTurn()) {
clickable = false;
if (Board.player) {
setIcon(X);
Game.performTurn(x, y, Value.FIRST);
try {
Server.send(new ObjToSend(x, y));
} catch (IOException ex) {
Logger.getLogger(XOButton.class.getName()).log(Level.SEVERE, null, ex);
}
try {
ObjToSend bla = Server.recieve();
Board.buttons[bla.getX()][bla.getY()].setIcon(O);
Game.performTurn(bla.getX(), bla.getY(), Value.SECOND);
Game.turn = true;
} catch (IOException | ClassNotFoundException ex) {
Logger.getLogger(XOButton.class.getName()).log(Level.SEVERE, null, ex);
}

why is my japplet displays gray block in browser after adding an image icon?

My japplet is able to run normally in NetBeans environment, but shows only a gray block screen in my browser (Chrome, IE). But when I removed all the image icons from my japplet, it runs normally in the browser and I have totally no idea why. Need some guidance for this.
My japplet class content
public void init()
{
try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); }
catch(Exception ex) { System.out.println(ex); }
try
{
java.awt.EventQueue.invokeAndWait(new Runnable() { public void run() { initComponents(); } });
}
catch (Exception ex) { ex.printStackTrace(); }
jlbReg.setIcon(new ImageIcon(getClass().getResource("/MyIcons/icon1.png")));
jlbAnn.setIcon(new ImageIcon(getClass().getResource("/MyIcons/icon2.png")));
jlbSubmit.setIcon(new ImageIcon(getClass().getResource("/MyIcons/icon3.png")));
jlbForum.setIcon(new ImageIcon(getClass().getResource("/MyIcons/icon4.png")));
}
My html content
<APPLET codebase="classes" code="MyJApplet.class" width=1150 height=1000></APPLET>
Your applet can't find the images in the jar file. Wait a minute - that's because you're not using a jar file. You need to use a jar file.

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 create a JTable cell containing Image hyperlink?

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.

Categories

Resources