We have an applet code below. where we have specified a button and when the button is clicked it should open a new website. Yahoo website in this case. The code for the applet is below
public class GotoLinkApplet extends Applet implements ActionListener{
public void init()
{
String link="yahoo";
Button b=new Button(link);
b.addActionListener(this);
add(b);
}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("1");
Button src= (Button)e.getSource();
String link="http://www."+src.getLabel()+".com";
try{
AppletContext a=getAppletContext();
URL url =new URL(link);
//a.showDocument(url, "_self");
a.showDocument(url, "_blank");
System.out.println("a");
}
catch(MalformedURLException ae)
{
System.out.println(ae.getMessage());
}
}
}
We executed the above code in eclipse,but when we click on the button, the yahoo link is not coming up. Request you to help. But the code that specifies showdocument runs fine.
Any help on the above is much appreciated.
This should be possible using Desktop.browse(uri);
You'll of course want to make sure the Desktop is supported first.
Related
I have few issues with my swing app which looks like this app preview
This Frame is defined in Form class.
Form class is building in main class, code below
public class Main {
public static void main(String[] args) {
Form form = new Form();
checkIfRunning();
}
"New doctor" button has it's own listener which check is this object is already exist. Listener is defined in Form class.
newDoctorButton.addActionListener(new ActionListener() {
private NDoctor dc = null;
#Override
public void actionPerformed(ActionEvent e) {
if (dc == null){
dc = new NDoctor();}
else dc.toFront();
}
});
Everything works fine, new window is opening as i want. second window preview
"Ok" button listener below
okButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
NDoctor.super.dispose();
}
});
When i close it i cant open it once more.
Im sure the problem is in the main method. The "New Doctor"listener works but in run only once, when i close window it wont start until i call main method again, but this will create next Frame.
I hope you understand my problem. I will be grateful for any advice.
i had designed two applets in eclipse and when I click a button I want to show second applet's design how can I do it with mouseClicked method?
this is my first code.
JButton btnReserveASeat = new JButton("RESERVE A SEAT NOW!");
btnReserveASeat.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Policies ();
}
});
this is my second code that i want to give link in button.
public class Policies extends JApplet {
/**
* Create the applet.
*/
public Policies() {
getContentPane().setBackground(Color.PINK);
getContentPane().setLayout(new FormLayout(new ColumnSpec[] {
ColumnSpec.decode("450px"),},
new RowSpec[] {
RowSpec.decode("29px"),
FormFactory.RELATED_GAP_ROWSPEC,
Goes like this.
public void actionPerformed(ActionEvent e) {
new Policies ();
}
Should be something like:
URL url = new URL(getDocumentBase(), "policies.html");
// ..
public void actionPerformed(ActionEvent e) {
ThisApplet.getAppletContext().showDocument(url);
}
i'm making a java application for mac. the application must have the hability of "Auto Hide" equals to "Command+H" shortcut. i'm trying to do it using setVisible(False) in the JFrame. but it doesn't work. how can i do it?
this is may code:
void hide(){
setNormalScreen(); //disable fullscreen mode
//this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
setVisible(false);
this.setState(JFrame.ICONIFIED);
}
and this is what i'm getting:
See the below example. You can hide it via Java code using setVisible(false) as you suggested, then appReOpened() event will be called when the user clicks the app in the dock. When this happens, you can just call setVisible(true). This should mimic the Command-H behavior.
See the commented code below also for an uglier solution.
public class Test extends JFrame implements ActionListener, com.apple.eawt.AppReOpenedListener {
public static void main(String[] args) {
Test frame = new Test();
JButton test = new JButton("test");
test.addActionListener(frame);
com.apple.eawt.Application app = com.apple.eawt.Application.getApplication();
app.addAppEventListener(frame);
frame.getContentPane().add(test);
frame.pack();
frame.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent arg0) {
setVisible(false);
// try {
// Robot robot = new Robot();
// robot.keyPress(KeyEvent.VK_META);
// robot.keyPress(KeyEvent.VK_H);
// robot.keyRelease(KeyEvent.VK_H);
// robot.keyRelease(KeyEvent.VK_META);
// } catch (AWTException ex) {
// // TODO Auto-generated catch block
// ex.printStackTrace();
// }
}
#Override
public void appReOpened(AppReOpenedEvent arg0) {
setVisible(true);
}
}
I wrote a pretty basic applet program in eclipse:
public class SwingAppletDemo extends JApplet {
private static final long serialVersionUID = -1935096480915162747L;
JLabel jl;
public void init() {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
new SwingAppletDemo().makeGUI();
}
});
}
catch (Exception e) {
e.printStackTrace();
}
}
private void makeGUI() {
jl = new JLabel("Press a button!");
setLayout(new FlowLayout());
add(jl);
}
}
It compiled fine and then I ran it as an applet by right-clicking on the SwingAppletDemo.java and then I selected Run As > Java Applet
The applet viewer opened but no label was displayed in it. Can anyone please tell me where did I went wrong, I referred to a few tutorials but couldn't find the necessary info.
I also tried running it in Google Chrome but there too only an empty applet was displayed.
Thanx in advance!
You are creating a new SwingAppletDemo in your init method which is never shown. You can simply replace:
new SwingAppletDemo().makeGUI();
with
makeGUI();
I am trying to listen tab-in tab-out action for my swing gui that is made by JFrame. I have a JTextField added to the JFrame that will be getting the user clipboard whenever the window is selected so the user may tab between programs, copy some url so when back to my program, this JTextField will be populated by the copied url string.
EDIT:
I have tried this:
frame.addFocusListener(
new FocusListener() {
public void focusGained(FocusEvent e) {
url= getClipboardData();
}
#Override
public void focusLost(FocusEvent arg0) {
// TODO Auto-generated method stub
}
}
);
it doesnt work
A frame doesn't recieve a focus event. A component on the frame gets the focus event.
If you want to know when a frame gets focus then use a WindowListener and handle the windowActivated event.
What you want is a FocusListener not an ActionListener. Check out the java Doc and you'll know how to use it. It's easy.
it looks like you are not setting the clipboard data onto the text field.
frame.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
getJTextField().setText(getClipboardData());
}
public void focusLost(FocusEvent e) {
//ignored
}
});
Something like that will likely solve your problem