Add a Java file in a Jpanel of another file - java

I have 2 Java files, the first one is the main java code behind the program. And second is a jfx.Webview. I've been trying for forever to include the jfx.Webview in a JPanel that I have on the first Java file. I'm new to Java, and it's definitely not as easy as i thought. Please, if someone with a better understanding could explain to me the proper way to get this done, that would be of great help.
Here are the 2 Java files after some cleaning up:
public class Xzibit07 {
public static void main(String[] args) {
generateUI();
}
private static void generateUI(){
XzibitUI program = new XzibitUI();
program.setVisible(true);
program.setTitle("Xzibit");
ImageIcon logoIcon = new ImageIcon(new ImageIcon("Data/Images/Logo.jpg").getImage().getScaledInstance(program.logoLabel.getWidth(), program.logoLabel.getHeight(), Image.SCALE_DEFAULT));
program.logoLabel.setIcon(logoIcon);
program.logoLabel.setBounds((program.logoPanel.getWidth()/2 - program.logoLabel.getWidth()/2), 0, 0, 0);
ImageIcon settingsIcon = new ImageIcon(new ImageIcon("Data/Images/Settings.png").getImage().getScaledInstance((program.logoPanel.getHeight()/4), (program.logoPanel.getHeight()/4), Image.SCALE_DEFAULT));
program.settingsLabel.setIcon(settingsIcon);
program.getContentPane().setBackground(Color.WHITE);
program.logoPanel.setBackground(Color.WHITE);
}
}
And the second:
public class XzibitWeb implements Runnable {
public String webPage = "http://www.example.com";
public static void main(String[] args) {
SwingUtilities.invokeLater(new XzibitWeb());
}
#Override
public void run() {
JFXPanel jfxPanel = new JFXPanel();
Platform.runLater(() -> {
WebView view = new WebView();
jfxPanel.setScene(new Scene(view, 1024, 400));
view.getEngine().load(webPage);
});
}
}

I think you are trying to do this
https://docs.oracle.com/javase/8/javafx/interoperability-tutorial/swing-fx-interoperability.htm
From your code i think you are not adding the jfxPanel to the frame.

Related

Can't Use System Menubar Javafx

I want to add a JavaFX Menu Bar to a Stage, but have it use the System Menubar for Mac.
My problem is that using:
menuBar.useSystemMenuBarProperty().set(true);
does not work. I believe that the problem is because my main method isn't part of a JavaFX Application. My main method looks like this:
public class SURPG_Launcher {
public static com.apple.eawt.Application macApp;
public static void main(String[] args) {
if(Toolbox.isMac()) {
initMac(args);
} else {
Application.launch(SURPG_Main.class, args);
}
}
private static void initMac(String[] args) {
System.out.println("MacOS System detected!");
macApp = com.apple.eawt.Application.getApplication();
macApp.setPreferencesHandler(new PreferencesHandler(){
#Override
public void handlePreferences(PreferencesEvent arg0) {
Platform.runLater(() -> {
Stage prefs = new Stage();
prefs.setMinHeight(200);
prefs.setMinWidth(200);
prefs.show();
});
}
});
Application.launch(SURPG_Mac.class, args);
}
}
SURPG_Mac.class and SURPG_Main.class are classes that extend the JavaFX Application.
I have another class that sets the GUI, a stage with a BorderPane. I have another class with public static methods that can be called to set the Menubars, as such:
public class MenuControl {
public static MenuBar menuBar;
public static Menu menuFile;
public static Menu menuGame;
public static Menu menuTools;
public static MenuItem save;
public static void initMenusMac() {
menuBar = new MenuBar();
Platform.runLater(() -> {
menuBar.useSystemMenuBarProperty().set(true);
});
menuFile = new Menu("File");
menuGame = new Menu("Game");
menuTools = new Menu("Tools");
save = new MenuItem("Save");
menuFile.getItems().add(save);
menuBar.getMenus().addAll(menuFile, menuGame, menuTools);
GUI_Main.totalLay.setTop(menuBar);
}
public static void initMenus() {
menuBar = new MenuBar();
menuFile = new Menu("File");
menuGame = new Menu("Game");
menuTools = new Menu("Tools");
save = new MenuItem("Save");
menuFile.getItems().add(save);
menuBar.getMenus().addAll(menuFile, menuGame, menuTools);
GUI_Main.totalLay.setTop(menuBar);
}
}
My final point is that I CANNOT change it so the main method is in either SURPG_Mac or SURPG_Main, due to a different compatibility problem with Mac integration.
Can anyone help me with this?
Thank you so much in advance!
Have a look at this probject: https://github.com/codecentric/NSMenuFX It allows you to have a more mac-like menu bar. But you will probably have to clean up your somewhat strange project setup as well before you can use it.

JFrame, trying to make button run another class

Pretty much what I'm trying to do is make a custom installer, I have buttons and that working fine but I want to run another class called CopyDir.java when I click on the button so that it copies the necessary files to the correct directory. Problem is, is that I'm a bit stumped on how to do this.
public class Frame extends JFrame implements ActionListener {
JPanel pane = new JPanel();
JButton PC = new JButton("Install Mod (PC)");
JButton Steam = new JButton("Install Mod (Steam)");
JLabel Text = new JLabel("Welcome to the BTD 5 Mod Installer");
JLabel Text2 = new JLabel("Click on the button that matches your version of BTD 5");
JLabel Text3 = new JLabel("To install it for the version that you are using");
JLabel Text4 = new JLabel("© Nixxx60/Nanikos");
Frame() {
super("BTD 5 Mod Installer");
setBounds(100, 100, 400, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container con = this.getContentPane();
con.add(pane);
PC.setMnemonic('P');
PC.addActionListener(this);
pane.add(PC);
PC.requestFocus();
con.add(pane);
Steam.setMnemonic('P');
Steam.addActionListener(this);
pane.add(Steam);
Steam.requestFocus();
setVisible(true);
pane.add(Text);
pane.add(Text2);
pane.add(Text3);
pane.add(Text4);
}
public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
if (source == PC) {
JOptionPane.showMessageDialog(null, "Mod has been installed on PC/Cracked Edition!", "BTD 5 Installer",
JOptionPane.PLAIN_MESSAGE);
setVisible(true);
}
if (source == Steam) {
JOptionPane.showMessageDialog(null, "Mod has been installed for Steam Edition!", "BTD 5 Installer",
JOptionPane.PLAIN_MESSAGE);
setVisible(true);
}
}
public static void main(String args[]) {
new Frame();
}
}
Also, here is the code for the "CopyDir.java" Class.
package Nanikos.BTD5.Main;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class CopyDir {
public static void main(String args[]) throws Exception
{
copyFiles(new File("C:\\Users\\User\\Desktop\\BTD5 Mod Installer\\Mod Files\\PC Assets"),new File("C:\\Program Files (x86)\\Steam\\steamapps\\common\\BloonsTD5"));
System.out.println("Files Copied");
}
public static void copyFiles(File src,File des) throws Exception
{
if(src.isDirectory())
{
if(!des.exists()) des.mkdir();
String [] filePaths=src.list();
for(String filePath: filePaths)
{
File srcFile =new File(src, filePath);
File desFile =new File(des, filePath);
copyFiles(srcFile,desFile);
}
}
else
{
FileInputStream from =null;
FileOutputStream to =null;
from = new FileInputStream(src);
to = new FileOutputStream(des);
byte [] buffer=new byte[4096];
int byteReads;
while( (byteReads=from.read(buffer))!=-1 )
{
to.write(buffer,0,byteReads);
}
from.close();
to.close();
}
}
}
What you want is to launch your class as a Thread
To launch your class CopyDir as a thread, make it implement Thread, change your main method signature to this :
public void run() {
//Your code
}
Also, to pass parameters to your thread, add a constructor in your CopyDir class that takes the parameters you have and stores them as attributes, to be able to get it from your method.
Then, to launch the thread from your event listeners :
CopyDir myCopyThread = new CopyDir(inputPath,outputPath);
myCopyThread.start();
This code will create a thread that starts running CopyDir in the run() method

Java, how to get into the JPanel

I'm a beginner in Java. My code:
public PlanetSimulator() throws InterruptedException {
JPanel panel = new JPanel();
panel.setBounds(64, 35, 840, 600);
Draw view = new Draw();
panel.add(view);
Drawing in panel Sun and Stars and Black Background.
How can I say Java "Hey you see that panel? Draw there another thing"
But I'm saying in to Java not from the PlanetSimulator, but main function.
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
PlanetSimulator frame = new PlanetSimulator();
frame.setVisible(true);
//DrawPlanetOne DrawPlanetOne = new DrawPlanetOne()
//PlanetSimulator.panel.add(DrawPlanetOne)
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
But it doesn't work. Yes I known that I'm amateur.
Rysowanie / Draw class
http://pastebin.com/RkcTBxrN
I assume, this will be a simulation. And if you like to simulate something with 30FPS, it will be hard with a Panel. You could use a library like processing to animate your planets and stuff. It's a lot easier as using a JPane. But you could call the repaint method?

ImageIcon in JLabel not working

I've been struggling with this problem for weeks to no avail. Other Java forums have not been able to help me with this so I'm hoping I can find someone here with new eyes. I have a JFrame in which, among other things, I am trying to insert an image. I have put together an SSCCE that reproduces the problem.
public class ImageTest {
public static void main(String[] args) {
SplashScreen.ShowWindow();
}
}
public class SplashScreen {
private JFrame frmKcbsEventsSearch;
public SplashScreen() {
initialize();
}
public static void ShowWindow() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SplashScreen window = new SplashScreen();
window.frmKcbsEventsSearch.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public void initialize() {
frmKcbsEventsSearch=new JFrame();
frmKcbsEventsSearch.getContentPane().setBounds(new Rectangle(200, 200, 520, 450));
frmKcbsEventsSearch.getContentPane().setFont(new Font("Times New Roman", Font.PLAIN, 16));
frmKcbsEventsSearch.setTitle("KCBS Events Search");
frmKcbsEventsSearch.setBounds(200,200,520,450);
frmKcbsEventsSearch.getContentPane().setLayout(new FlowLayout());
frmKcbsEventsSearch.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JLabel logo;
java.net.URL logoURL=getClass().getResource("KCBSLogo.jpg");
if (logoURL!=null) {
try {
logo = new JLabel();
logo.setIcon(new ImageIcon(logoURL));
}
catch (Exception e) {
logo = new JLabel("KCBS Logo");
}
}
else {
logo = new JLabel("KCBS Logo missing");
}
logo.setHorizontalAlignment(SwingConstants.CENTER);
logo.setVerticalAlignment(SwingConstants.CENTER);
frmKcbsEventsSearch.getContentPane().add(logo);
}
}
It has been mentioned elsewhere that the layout is important to make this work. I've not had much luck getting layout managers to do what I want but in this case I just want to get the image displayed. Once I have that I'll work through getting it formatted the way I want.
This code works perfectly fine!
I changed the KCBSLogo.jpg to a source that I had named tile.png.
When I ran it displayed perfectly.
Make sure the KCBSLogo.jpg is in your SRC folder in the project.
http://i.stack.imgur.com/sYfYF.png
As you can see the tile image was shown.

Java Event Queue : how to update component in JFrame

I have read about that when programming Java Swing, we should put these components into Java Event Queue, because Java Swing thread is not-thread safe.
But, when I use Event Queue, I don't know how to update component properties (for example : set text for label or change something..). Here is my code :
public class SwingExample {
private JLabel lblLabel;
SwingExample(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
lblLabel = new JLabel("Hello, world!", JLabel.CENTER);
frame.getContentPane().add(lblLabel); // adds to CENTER
frame.setSize(200, 150);
frame.setVisible(true);
}
public void setLabel(){
lblLabel.setText("Bye Bye !!!");
}
public static void main(String[] args) throws Exception
{
SwingExample example = null;
EventQueue.invokeLater(new Runnable()
{
public void run()
{
example = new SwingExample(); // ERROR : Cannot refer to non-final variable inside an inner class defined in different method
}
});
// sometime in the futures, i want to update label, so i will call this method...
example.setLabel();
}
}
I know, if I write SwingExample example = new SwingExample(); the error won't appear again, but if i use that, I cannot process example.setLabel later.
Please tell me about this error and how to fix this.
Thanks :)
By having your SwingExample instance as a field, you can reference it inside the inner classes without it being final.
public class SwingExample {
private JLabel lblLabel;
private static SwingExample instance;
SwingExample() {
// code omitted
}
public void setLabel() {
lblLabel.setText("Bye Bye !!!");
}
public static void main(String[] args) throws Exception {
EventQueue.invokeLater(new Runnable() {
public void run() {
instance = new SwingExample();
}
});
// ...
EventQueue.invokeLater(new Runnable() {
public void run() {
instance.setLabel();
}
});
}
}

Categories

Resources