Apache Pivot: Frame inside window - java

I am new to Apache Pivot.
I am tryng to make a simple window with menu bar.
The code I used to load the main frame is:
public class MyApp implements Application {
private Frame frame;
#Override
public void startup(Display display, Map<String, String> strings) throws Exception {
BXMLSerializer bxmlSerializer = new BXMLSerializer();
frame = (Frame)bxmlSerializer.readObject(MyApp.class, "/gui/MainFrame.bxml");
frame.open(display);
}
#Override
public boolean shutdown(boolean b) throws Exception {
if(frame != null) {
frame.close();
}
return false;
}
#Override
public void suspend() throws Exception {
}
#Override
public void resume() throws Exception {
}
public static void main(String[] args) {
DesktopApplicationContext.main(MyApp.class, args);
}
}
The main frame BXML is like:
<root:MainFrame title="MyApp" maximized="true"
xmlns:bxml="http://pivot.apache.org/bxml"
xmlns="org.apache.pivot.wtk"
xmlns:root="com.myproject.client">
<menuBar>
<bxml:include src="wtk/menubar.bxml"/>
</menuBar>
</root:MainFrame>
The MainFrame.java is like:
public class MainFrame extends Frame implements Bindable {
public MainFrame() {
Action.getNamedActions().put("myaction1", new Action() {
#Override
public void perform(Component source) {
......
}
});
}
}
The result of this code is like the picture below:
As you can see there is an Mac window outside and a frame window inside.
My question is that how can I get rid of the system window OR get rid of the frame window so that only one window is shown?
Thank you very much.

I found a 2012 mailing list post that implies Pivot has no support for native menus, so your app will always live within the system window.
However, that mailing list post did suggest a hack using Java AWT Frame to get system menus.

Related

Blank JFrame when program is run

I am an absolute beginner in coding. I would like to know why is my Jframe blank when run, how do I fix it. From what I have research on the internet it seems that I should put the component inside the JFrame as it is empty but how do I do it
My Code
public class Video extends JFrame
{
public static void main(String[] args) throws URISyntaxException {
final URI uri = new URI("https://www.youtube.com/watch?v=rl0YiZjTqpw");
class OpenUrlAction implements ActionListener
{
#Override public void actionPerformed(ActionEvent e) {
open(uri);
}
}
JFrame frame = new JFrame("Links");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(410, 400);
Container container = frame.getContentPane();
container.setLayout(new GridBagLayout());
JButton btnclickHereTo = new JButton();
btnclickHereTo.setText("<HTML> <FONT color=\"#000099\"><U>Click Here To Watch Video</U></FONT>");
btnclickHereTo.setHorizontalAlignment(SwingConstants.LEFT);
btnclickHereTo.setBorderPainted(false);
btnclickHereTo.setOpaque(false);
btnclickHereTo.setBackground(Color.WHITE);
btnclickHereTo.setToolTipText(uri.toString());
btnclickHereTo.addActionListener(new OpenUrlAction());
container.add(btnclickHereTo);
frame.setVisible(true);
}
private static void open(URI uri)
{
if (Desktop.isDesktopSupported())
{
try
{
Desktop.getDesktop().browse(uri);
}
catch (IOException e)
{ /* TODO: error handling */ }
}
else
{ /* TODO: error handling */ }
}
}
public void setVisible(boolean b) {
Why would you override the setVisible(...) method of your frame? There is no reason to do that.
I am an absolute beginner in coding
Start with something basic, like the example from the Swing tutorial on How to Make Frames.
Keep a reference to the tutorial link handy since it contains information and examples for all Swing basics.

I have two classes and want to get a variable from one to the other

I have two classes. Draw and DrawGUI. In DrawGUI I have a JPanel. For my JUnit test I need to ask the class Draw for getWidth() and getHeight(). So my code is like the following:
public class Draw {
public static void main(String[] args) throws ColorException {new Draw();}
/** Application constructor: create an instance of our GUI class */
public Draw() throws ColorException { window = new DrawGUI(this); }
protected JFrame window;
public void getWidth(){
}
}
class DrawGUI extends JFrame {
JPanel drawPanel;
public DrawGUI(Draw application) throws ColorException {
super("Draw"); // Create the window
app = application;
drawPanel = new JPanel();
}
}
So how do I implement getWidth? getWidth should return the width from the JPanel drawPanel
One option is to change the weak type you're saving your window under:
public class Draw {
public static void main(String[] args) throws ColorException {new Draw();}
/** Application constructor: create an instance of our GUI class */
public Draw() throws ColorException { window = new DrawGUI(this); }
protected DrawGUI window; // <- is now a DrawGUI
public int getWidth(){
return window.getPanelWidth();
}
}
class DrawGUI extends JFrame {
JPanel drawPanel;
...
public DrawGUI(Draw application) throws ColorException {
super("Draw"); // Create the window
app = application;
drawPanel = new JPanel();
}
public int getPanelWidth() { // <- added method to get panel width
return drawPanel.getWidth();
}
}
There are other options. You could also just make a getter for the whole panel, but then you have less encapsulation.

close Javafx class application in java application

i have an application in java, and this have a one popup with javafx application (embed videos from Youtube). I see this correctly but when i close this popup, the javafx thread not close and javafx application running in background. This is my javafx class:
public class JavaFXClass extends Application {
#Override
public void start(final Stage stage) throws Exception {
final WebView webview = new WebView();
/*...*/
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
#Override
public void handle(WindowEvent event) {
Platform.runLater( new Runnable() {
#Override
public void run() {
//I need stop javafx when this class close.
}
});
}
});
stage.show();
}
public static void LoadClass(String Data) { //I use this function to load class
/*...*/
launch(); //return error when i re-call this function (already launch).
}
If i put webview.getEngine().load(null); Platform.exit(); code in the "OnCloseRequest" works fine but an exception is created ("Attempt to call defer when toolkit not running")
i need use webview.getEngine().load(null); or similar because if i not use this, the video in webview remain playing in background. And if i not use Platform.exit() the main frame crashes (lock).
Sorry for my bad english, tried to write the best I could
use this:
[...]
stage.setOnCloseRequest(this.getCloseSystemEvent());
}
public EventHandler<WindowEvent> getCloseSystemEvent() {
return new EventHandler<WindowEvent>() {
#Override
public void handle(WindowEvent event) {
Platform.exit();
}
};
}
Also, you should check the concurrency API. Your code prevents the runtime from closing the thread properly.

Is it possible to make JavaFX web applet?

I like old Java applets. But because I really like the way JFX works, I want write some games using it (or even game making system, who knows?), but I'd like to be able to post them on my website. How would one go about doing this?
Yes, you can embed a JavaFX GUI into the Swing-based JApplet. You can do this by using the JFXPanel - it is essentially an adaptor between Swing and JavaFX panels.
Complete example:
The FXApplet class that sets-up the JavaFX GUI:
public class FXApplet extends JApplet {
protected Scene scene;
protected Group root;
#Override
public final void init() { // This method is invoked when applet is loaded
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
initSwing();
}
});
}
private void initSwing() { // This method is invoked on Swing thread
final JFXPanel fxPanel = new JFXPanel();
add(fxPanel);
Platform.runLater(new Runnable() {
#Override
public void run() {
initFX(fxPanel);
initApplet();
}
});
}
private void initFX(JFXPanel fxPanel) { // This method is invoked on JavaFX thread
root = new Group();
scene = new Scene(root);
fxPanel.setScene(scene);
}
public void initApplet() {
// Add custom initialization code here
}
}
And a test implementation for it:
public class MyFXApplet extends FXApplet {
// protected fields scene & root are available
#Override
public void initApplet() {
// this method is called once applet has been loaded & JavaFX has been set-up
Label label = new Label("Hello World!");
root.getChildren().add(label);
Rectangle r = new Rectangle(25,25,250,250);
r.setFill(Color.BLUE);
root.getChildren().add(r);
}
}
Alternatively, you can use the FXApplet gist, which also includes some documentation.
Yes, you should be able to embed JavaFX in your web page:
http://docs.oracle.com/javase/8/docs/technotes/guides/deploy/deployment_toolkit.html#BABJHEJA
http://docs.oracle.com/javase/8/javase-clienttechnologies.htm

making java SingleFrameApplication to appear second

Sorry if this question will sound too chaotic, feel free to edit it.
I have an application made entirely in netbeans, which uses SingleFrameApplication and auto-generated the GUI code, named "MyApp", and FrameView, named "MyView". Now, the MyApp somehow has the main() function, but the MyView has all the graphic elements..
I don't entirely understand how that happens, so used it as black box (it somehow created the window, I didn't have to care why). But now, I need the window to be only a window, opened by another JFrame. I don't know, how to accomplish that.
MyApp, which is extending SingleFrameApplication, have these methods:
public class MyApp extends SingleFrameApplication {
#Override protected void startup() {
show(new MyView(this));
}
#Override protected void configureWindow(java.awt.Window root) {
}
public static MyApp getApplication() {
return Application.getInstance(MyApp.class);
}
public static void main(String[] args) {
launch(MyApp.class, args);
}
}
MyView has these methods:
public class MyView extends FrameView {
public MyView(SingleFrameApplication app) {
super(app);
initComponents();
}
private void initComponents() {
//all the GUI stuff is somehow defined here
}
}
Now, I have no clue how the two classes work, I just want this window, defined in MyView, to appear after another window, "ordinary" JFrame. How can I call this MyApp/MyView?
But now, I need the window to be only a window, opened by another JFrame. I don't know, how to accomplish that.
1.) It's not just a window - it's a
Swing Framework Application (Ah, the
perils of GUI builders...); and -
2.) You haven't specified how you want
it "opened by another JFrame";
but something like this should work if you're launching it via a JButton -
JButton launchMyApp = new JButton("launch");
launchMyApp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String[] args = {};
Application.launch(MyApp.class, args);
}
});

Categories