Youtube video on JFrame? - java

I'm trying to use the DJ Native Swing API to open a WebBrowser on my code:
public class YoutubeViewer {
public static void main(String[] args) {
NativeInterface.open();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("YouTube Viewer");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(getBrowserPanel(null), BorderLayout.CENTER);
frame.setSize(800, 600);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
});
NativeInterface.runEventPump();
// don't forget to properly close native components
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
#Override
public void run() {
NativeInterface.close();
}
}));
}
public static JPanel getBrowserPanel(String trailer) {
trailer = "https://www.youtube.com/watch?feature=player_detailpage&v=6kw1UVovByw";
JPanel webBrowserPanel = new JPanel(new BorderLayout());
JWebBrowser webBrowser = new JWebBrowser();
webBrowserPanel.add(webBrowser, BorderLayout.CENTER);
webBrowser.setBarsVisible(false);
webBrowser.navigate(trailer);
return webBrowserPanel;
}
}
But I keep getting this error:
Could anyone help me?

This kind of exception is caused when the class loader (module of the interpreter) do not locate the respective class (SWTNativeInterface, in your case) in the class path. You should place the respective library in the project's classpath.
You set this classpath in the project properties.

Related

opening a frame by clicking on the button

In my project I am trying to open LoginFrame from WelcomeFrame by clicking a button and I want myWelcomeFrameto be closed as well.
I have successfully opened theLoginFrameby usingsetVisible(true).
To close theWelcomeFrameI have writtenframe.SetVisible(false)where frame is the object ofWelcomeFrame` but this line shows an error: frame cannot be resolved ....
Here's my code.. Please help
public class WelcomeFrame extends JFrame{
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
WelcomeFrame frame = new WelcomeFrame(); //object of WelcomeFrame
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
JButton btnNewButton = new JButton("Librarian Portal\r\n");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
LoginFrame l=new LoginFrame();
l.setVisible(true);
frame.setVisible(false); //error:frame cannot be resolved
}
});
Because object of WelcomeFrame is not accessible.
You need to create another object before trying to call setVisible method.
WelcomeFrame closing_frame = new WelcomeFrame();
closing_frame.setVisible(false);
public class WelcomeFrame extends JFrame{
private JPanel contentPane;
private WelcomeFrame frame;
after
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame = new WelcomeFrame();
I think it'll help you
a CardLayout is more adapted to your needs; the general idea is to create a jPanel instead of a JFrame, the CardLayout enables you to switch between them by showing only one at a time. if this is what you need have alook here

Adding jpanels created from the same class to a cardlayout frame

I have a class that reads an excelfile and creates frame with a graph inside a jpanel. I invoke this class through an actionlistener in a jmenuitem. Then I have another jmenuitem that invokes the same class that opens the same file, but reads a different excel sheet, and gives a different graph (thats the only string that changes in the class). The jmenubar that has these jmenitems belong to a jframe from which the program starts. I would like to know if it is possible every time I click the jmenuitems that create the graphs to be added in a new jframe cardlayout, so that I can roll on them. Thanks in advance
This is the code I am currently using to open a graph in a jframe when the jmenuitem is clicked:
public class startup extends JFrame { // creates a jframe with some stuff and the jmenubar
public void menu() {
...
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event2) {
new Thread(new Runnable() {
#Override
public void run() {
new ReadExcel();
ReadExcel.excel(".xls", 0); // this jmenuitem invokes the class to read the excelfile sheet 0
graphgen.main(null);
}
}).start();
}
});
subsubmenu1.add(menuItem);
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event2) {
new Thread(new Runnable() {
#Override
public void run() {
new ReadExcel();
ReadExcel.excel(".xls", 1); // this jmenuitem invokes the class to read the excelfile sheet 1
graphgen.main(null);
}
}).start();
}
});
subsubmenu1.add(menuItem);
....
}
public static void main(String[] args)
{
GUIquery frame = new GUIquery();
p.add(graphComponent, BorderLayout.CENTER);
frame.setLayout(new BorderLayout());
frame.add(p, BorderLayout.CENTER);
frame.setJMenuBar(GUIquery.createMenuBar());
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.setSize(1600, 1200);
frame.setVisible(true);
}
}
The readexcel class just reads an excelsheet of an excelfile and returns some arraylist which are processed in the graphgen class.
public class graphgen extends JFrame {
public graphgen() {
super("Results");
gen();
}
public void gen(){
//creates the graphcomponent
getContentPane().add(graphComponent);
add(graphComponent);
}
public static void main(String[] args)
{
graphgen frame = new graphgen();
p2.add(graphComponent, BorderLayout.CENTER);
frame.add(p2, BorderLayout.CENTER);
frame.pack();
frame.setResizable(true);
frame.setSize(1600, 1200);
frame.setVisible(true);
}
Use Action to encapsulate the target component, file and sheet, as shown here and here. Add a method to update state of the class based on the chosen sheet. Examples of navigating among cards are seen here and here. See also Card Layout Actions, cited here.

How to have a JFrame Maximise icon

How do i create a JFrame window in eclipse on a Mac that has an icon that makes the window full screen like the double arrow icon on most windows at the top right??
Take a look at
Fullscreen feature for Java Apps on OSX Lion
And Java Runtime System Properties, which may be of interested
or How can I do full screen in Java on OSX if those were the wrong feature you wanted
UPDATE
Lucky for you JFrame extends Window via Frame...
public class TestMacFullScreen {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setLayout(new BorderLayout());
JLabel label = new JLabel("Look ma, no hands");
frame.add(label);
enableOSXFullscreen(frame);
frame.setVisible(true);
}
});
}
public static void enableOSXFullscreen(Window window) {
try {
Class util = Class.forName("com.apple.eawt.FullScreenUtilities");
Class params[] = new Class[]{Window.class, Boolean.TYPE};
Method method = util.getMethod("setWindowCanFullScreen", params);
method.invoke(util, window, true);
} catch (ClassNotFoundException exp) {
exp.printStackTrace();
} catch (Exception exp) {
exp.printStackTrace();
}
}
}

Internal JFrames

I want to know how to show an internal frame in swing. That means,when a JFrame is needed, normally what I do is,
new MyJFrame().setVisible(true);
Let's say the previous form should be displayed as well. And when this new frame is displayed,another new icon is displayed on the task bar.(it sounds like two separate applications run in one application) I want to avoid showing that icon and display both frames as they are in one application. Thank you
..want to avoid showing that icon and display both frames as they are in one application.
Another solution is to put the 2nd and subsequent free floating elements in a JDialog.
E.G. of using both a frame and dialog to hold extra content.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class FrameTest {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
initGui();
}
});
}
public static void initGui() {
final JFrame f = new JFrame("Frame Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel gui = new JPanel(new GridLayout(0,1,5,5));
final Content c = new Content();
JButton frame = new JButton("Frame");
frame.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JFrame f2 = new JFrame("Content");
f2.add(c.getContent());
f2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f2.pack();
f2.setLocationByPlatform(true);
f2.setVisible(true);
}
});
gui.add(frame);
JButton dialog = new JButton("Dialog");
dialog.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JDialog d = new JDialog(f);
d.add(new Content().getContent());
d.pack();
d.setLocationByPlatform(true);
d.setVisible(true);
}
});
gui.add(dialog);
f.add(gui);
f.pack();
f.setVisible(true);
}
}
class Content {
public Component getContent() {
JPanel p = new JPanel();
p.add(new JLabel("Hello World!"));
return p;
}
}
You have one JFrame for an application.
You can display multiple JPanels within a JFrame.
Or, as trashgod pointed out, you can have multiple JInternalFrames within a JDesktopFrame.

Components not shifting when removing from BoxLayout

I'm using a BoxLayout and removing components from it dynamically, something like this:
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
final JLabel l = new JLabel("remove");
frame.add(l);
frame.add(new JLabel("Hello2"));
frame.add(new JLabel("Hello3"));
frame.pack();
frame.setVisible(true);
new Thread() {
public void run() {
Utils.sleep(1000);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
frame.remove(l);
frame.repaint();
}
});
}
}.start();
}
});
}
However, when doing so, even though the label in question is removed from the layout, the other components don't shift up to cover its space until I resize the frame. I tried repainting the frame after removing the component, but no luck - the label no longer displays but there's still the gap where it used to be.
Apart from the obviously horrible bodge of resizing the window automatically every time the component is removed, how do I get the desired behaviour?
You need to invoke validate() on frame as well.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
frame.remove(l);
frame.validate();
frame.repaint();
}
});
1/ put revalidate(); before repaint();
2/ better would be invoke Thread from Runnable not from invokeLater()

Categories