I am trying to add a history , backward and forward buttons that actually do there jobs , so the history opens a drop down menu that shows the links the user went to and provided with a clear button to delete them if wanted and the backward to move back and the forward to go the the page the user was on.
public Test4(){
initComponents();
}
public static void main(String ...args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
final JFrame frame = new JFrame();
frame.getContentPane().add(new Test4());
frame.setMinimumSize(new Dimension(640, 480));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
private void initComponents(){
jfxPanel = new JFXPanel();
createScene();
setLayout(new BorderLayout());
add(jfxPanel, BorderLayout.CENTER);
swingButton = new JButton();
swingButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Platform.runLater(new Runnable() {
public void run() {
webEngine.reload();
}
});
}
});
swingButton.setText("Reload");
add(swingButton, BorderLayout.SOUTH);
}
private void createScene() {
PlatformImpl.startup(new Runnable() {
public void run() {
stage = new Stage();
stage.setTitle("Hello Java FX");
stage.setResizable(true);
Group root = new Group();
Scene scene = new Scene(root,80,10);
stage.setScene(scene);
browser = new WebView();
webEngine = browser.getEngine();
webEngine.load("http://www.google.com");
ObservableList<Node> children = root.getChildren();
children.add(browser);
jfxPanel.setScene(scene);
}
});
}
}
Related
I want to create an application starter. The ProgressIndicator should running during start of the new Stage in new Thread. But if I click at the start button the ProgressIndicator stops running.
It works if I make some I/O and ProgressIndicator shows progress. It is apparently not possible to update two stages in JavaFX in parallel or does anyone have a solution for me?
public class Main extends Application {
public void start(Stage primaryStage) {
primaryStage.setScene(new Scene(new UserVC().getView(), 600, 200));
primaryStage.show();
userVC.autologin();
}
public static void main(String[] args) { launch(args); }
}
public class UserView extends BorderPane {
private ProgressIndicator progressIndicator = new ProgressIndicator();
public UserView() {
super.setCenter(createContent());
}
public Node createContent() {
HBox userbox = new HBox();
userbox.getChildren().add(progressIndicator);
progressIndicator.show();
return userbox;
}
}
public class UserVC {
private UserView view = new UserView();
public UserVC() {
}
public void autologin() {
Task<Void> task = new Task<Void>() {
#Override public Void call() throws InterruptedException {
try {
Thread.sleep(1000);
Platform.runLater(new Runnable() {
#Override public void run() {
Stage stage = new Stage();
stage.setScene(new Scene(new MainControlVC().getView(), 900, 300));
stage.show();
}
});
}
catch(Exception e) {
e.printStackTrace();
}
return null;
}
};
view.getProgressIndicator().progressProperty().unbind();
view.getProgressIndicator().progressProperty().bind(task.progressProperty());
new Thread(task).start();
}
}
I changed the Task to Thread and it works fine now:
Thread thread = new Thread() {
public void run() {
mainController = new MainControlVC(dataDirectory, user);
Platform.runLater(new Runnable() {
#Override public void run() {
Stage stage = new Stage();
Scene scene = new Scene(mainController.getView(), mainController.getWidth(), mainController.getHeight());
stage.setScene(scene);
stage.show();
}
});
}
};
thread.start();
I am creating a new window controller in which I process data to be put into a linechart.
Though in the end the new window shows an empty LineChart. When I debug the line in which the series is put into the Linechart, the IDE states "series[null]".
So what am I missing?
Class of the new window:
public class ChartWindow {
Map<Job, List<LoadTestResultStatus>> lTRSMapOfJobs;
final LineChart lineChartMain;
Job originJob;
XYChart.Series series;
boolean lTRSfilled=false;
ObjectHub objectHub;
public ChartWindow(Job job, ObjectHub objectHub) {
this.objectHub = objectHub;
series = new XYChart.Series();
final NumberAxis xAxis = new NumberAxis();
final NumberAxis yAxis = new NumberAxis();
xAxis.setLabel("Time");
yAxis.setLabel("Time");
lineChartMain = new LineChart<Number, Number>(xAxis, yAxis);
lTRSMapOfJobs = new HashMap<>();
originJob = job;
lTRSMapOfJobs.put(job, objectHub.getDbManagement().getLTRSListOfJobFromDB(job));
fillLineChart();
}
public void fillLineChart() {
Platform.runLater(new Runnable() {
#Override
public void run() {
objectHub.getGuiReporter().statusStart(0);
objectHub.getGuiReporter().progressbarAddAmountOfStep(lTRSMapOfJobs.get(originJob).size());
}
});
for (LoadTestResultStatus l : lTRSMapOfJobs.get(originJob)) {
objectHub.getExecutor().submit(new Runnable() {
#Override
public void run() {
XYChart.Data xyChart = new XYChart.Data(l.getTs(), l.getLt());
try {
addToSeries(xyChart);
objectHub.getGuiReporter().progressbarAddStep();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
//TODO heavy CPU load:(
while(!lTRSfilled){
if(ObjectHub.getGuiReporter().progressBarDifference()==0){
lTRSfilled = true;
}
}
lineChartMain.getData().addAll(series);
}
void addToSeries(XYChart.Data xyChart) {
synchronized (this) {
series.getData().add(xyChart);
}
}
public Map<Job, List<LoadTestResultStatus>> getlTRSMapOfJobs() {
return lTRSMapOfJobs;
}
public void setlTRSMapOfJobs(Map<Job, List<LoadTestResultStatus>> lTRSMapOfJobs) {
this.lTRSMapOfJobs = lTRSMapOfJobs;
}
public LineChart getLineChartMain() {
return lineChartMain;
}
}
MainController method who processes the new window:
public void showLineChartForJob() {
progressBarLabel.setText("Preparing Linechart calculation...");
ChartWindow object to request executorservices
Thread createChartWindow = new Thread(new Runnable() {
#Override
public void run() {
int jobId = Integer.parseInt(visualizeTabIdInputTextField.getText());
Job lineChartJob = new Job(objectHub);
for (Job j : objectHub.getLtResultManagement().getjobSet()) {
if (j.getNumber() == jobId) {
lineChartJob = j;
break;
}
}
chartWindowList.add(new ChartWindow(lineChartJob, objectHub));
Platform.runLater(new Runnable() {
#Override
public void run() {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/ChartWindow.fxml"));
fxmlLoader.setController(chartWindowList.get(0));
StackPane secondaryLayout = new StackPane();
try {
secondaryLayout.getChildren().setAll(Collections.singleton(fxmlLoader.load()));
} catch (IOException e) {
e.printStackTrace();
}
Scene secondScene = new Scene(secondaryLayout, 1200, 1600);
Stage newWindow = new Stage();
newWindow.setTitle("Second Stage");
newWindow.setScene(secondScene);
newWindow.setX(50);
newWindow.setY(50);
newWindow.show();
}
});
}
});
createChartWindow.setName("createChartWindowThread");
createChartWindow.start();
}
Capture from debug taken at the last line of the First Controller:
Regards
Ok, case closed:
Failure was to not adress the scene to the linechart:
Scene secondScene = new Scene(secondaryLayout, 1200, 1600);
Fixed by obtaining the chart from the controller:
Scene secondScene = new Scene(chartWindowList.get(0).lineChartMain, 1200, 1600);
Basically i am using JavaFX scene buidler 2.0 i want to change the scene from one to other without using any button for them.
Main File
public class OurFirstProject extends Application {
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
stage.setFullScreenExitHint("");
//stage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds();
Scene scene = new Scene(root, screenBounds.getWidth(), screenBounds.getHeight());
stage.setScene(scene);
stage.setFullScreen(true);
stage.show();
public static void main(String[] args) {
Application.launch(args);
}
}
public class FXMLDocumentController implements Initializable {
#FXML
private void change(ActionEvent event) throws IOException {
Parent sceneChange = FXMLLoader.load(getClass().getResource("Change.fxml"));
Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds();
Scene changeScene = new Scene(sceneChange, screenBounds.getWidth(), screenBounds.getHeight());
Stage Window = (Stage) ((Node) event.getSource()).getScene().getWindow();
Window.setScene(changeScene);
Window.setFullScreen(true);
Window.show();
}
int a = 0;
#FXML
public Button helloButton;
#FXML
private Label ourLabel;
#FXML
private void printHello(ActionEvent e) {
a++;
if (a % 2 == 0) {
ourLabel.setText("Hello World! Kyun" + a);
} else {
ourLabel.setText("Hello Dunia" + a);
}
}
#Override
public void initialize(URL url, ResourceBundle rb) {
enter code here
}
The Scene Mentioned in the FXML File Name"change", i want to Run this Scen without Using Button I wanna run this on the delay of five second of FIrst scene.
You can use a Timer() like,
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
Platform.runLater(new Runnable() {
#Override
public void run() {// After this you can add your change.fxml load code
Parent root = null;
try {
root = fxmlLoader.load();
}catch(Exception e)
{
//Exception catch code here
}
primaryStage.show();//Here you can write your show code like window.show()
}
});
}
},5000);// 5000- time delay in milliseconds
I cant get this to resize, it always go for the preferred size for each screen. This is not ideal for a full screen application. It bascially just becomes a little box in the top left corner =(
I've spent days on this now but cant get it to work.
Could anyone tell me what im doing wrong? thanks
Main class:
public class Main extends Application {
public static final String MAIN_SCREEN = "main";
public static final String MAIN_SCREEN_FXML = "../gui/main.fxml";
public static final String CUSTOMER_SCREEN = "customer_main";
public static final String CUSTOMER_SCREEN_FXML = "../gui/customer_main.fxml";
#Override
public void start(Stage primaryStage) {
primaryStage.setFullScreen(true);
primaryStage.centerOnScreen();
ScreensController mainContainer = new ScreensController();
mainContainer.loadScreen(Main.MAIN_SCREEN,
Main.MAIN_SCREEN_FXML);
mainContainer.loadScreen(Main.CUSTOMER_SCREEN,
Main.CUSTOMER_SCREEN_FXML);
mainContainer.setScreen(Main.MAIN_SCREEN);
Group root = new Group();
root.getChildren().addAll(mainContainer);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
mainContainer.requestLayout();
}
public static void main(String[] args) {
launch(args);
}
First screen controller class (has FXML file):
public class MainScreenController implements ControlledScreen, Initializable{
ScreensController myController;
#FXML
private VBox mainScreen;
#FXML
private void mainScreenClicked(){
myController.setScreen(Main.CUSTOMER_SCREEN);
}
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
}
#Override
public void setScreenParent(ScreensController screenParent) {
myController = screenParent;
}
Stack pane for a nice layout:
public class ScreensController extends StackPane {
public ScreensController(){
}
private HashMap<String, Node> screens = new HashMap<>();
public void addScreen(String name, Node screen) {
screens.put(name, screen);
}
public boolean loadScreen(String name, String resource) {
try {
FXMLLoader myLoader = new FXMLLoader(getClass().getResource(resource));
Parent loadScreen = (Parent) myLoader.load();
ControlledScreen myScreenControler =
((ControlledScreen) myLoader.getController());
myScreenControler.setScreenParent(this);
addScreen(name, loadScreen);
return true;
}catch(Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
return false;
}
}
public boolean setScreen(final String name) {
if(screens.get(name) != null) { //screen loaded
final DoubleProperty opacity = opacityProperty();
//Is there is more than one screen
if(!getChildren().isEmpty()){
Timeline fade = new Timeline(
new KeyFrame(Duration.ZERO,
new KeyValue(opacity,1.0)),
new KeyFrame(new Duration(1000),
new EventHandler() {
#Override
public void handle(Event t) {
//remove displayed screen
getChildren().remove(0);
//add new screen
getChildren().add(0, screens.get(name));
Timeline fadeIn = new Timeline(
new KeyFrame(Duration.ZERO,
new KeyValue(opacity, 0.0)),
new KeyFrame(new Duration(800),
new KeyValue(opacity, 1.0)));
fadeIn.play();
}
}, new KeyValue(opacity, 0.0)));
fade.play();
} else {
//no one else been displayed, then just show
setOpacity(0.0);
getChildren().add(screens.get(name));
Timeline fadeIn = new Timeline(
new KeyFrame(Duration.ZERO,
new KeyValue(opacity, 0.0)),
new KeyFrame(new Duration(2500),
new KeyValue(opacity, 1.0)));
fadeIn.play();
}
return true;
} else {
System.out.println("screen hasn't been loaded!\n");
return false;
}
}
public boolean unloadScreen(String name) {
if(screens.remove(name) == null) {
System.out.println("Screen didn't exist");
return false;
} else {
return true;
}
}
}
interface so that each screen knows its parent:
public interface ControlledScreen {
public void setScreenParent(ScreensController screenPage);
}
Second controller class to verify that the stackpane works (has FXML file):
public class CustomerMenuController implements ControlledScreen, Initializable {
ScreensController myController;
#FXML
private FlowPane customerMenuFlow;
#Override
public void initialize(URL location, ResourceBundle resources) {
for (int i = 0; i < 3;i++){
new Customer();
}
Button [] menuButtons = new Button[Customer.customers.size()];
for (int i = 0; i < Customer.customers.size();i++){
menuButtons[i] = new Button("Customer " + i);
customerMenuFlow.getChildren().add(menuButtons[i]);
}
}
#Override
public void setScreenParent(ScreensController screenParent) {
myController = screenParent;
}
}
You shouldn't use primaryStage.setFullScreen(true); for resizable applications. Not in this context anyway. Once you remove that line, the application will start with its preferred size, but then the user is able to drag the corners of the window to resize the application.
I have implemented drag'n'drop functionality very similar to How to drag and drop tab nodes between tab panes, which works fine so far, but i also add a new Stage (SideStage below) if the tab is dragged outside of the main-window/stage. The problem is, the Tab is only showing after i either drag a new Tab to the TabPane there or if i resize the window. The main part of that drag'n'drop action is happening in the GuiPartFactory in the EventHandler which get's assigned to the Label of the Tab that gets dragged. I tried all sorts of size assigning, requestLayout, etc.
Sidestage class:
private TabPane tabPane;
public SideStage(double xPos, double yPos) {
tabPane = initTabArea();
VBox root = new VBox(0,GuiPartFactory.initMenuAndTitle(this),tabPane);
Scene scene = new Scene(root, 800, 600);
setScene(scene);
scene.setOnDragExited(new DnDExitAndEntryHandler());
scene.setOnDragEntered(new DnDExitAndEntryHandler());
setX(xPos);
setY(yPos);
;
initCloseWindowHandler();
}
private void initCloseWindowHandler() {
setOnCloseRequest(new EventHandler<WindowEvent>() {
#Override
public void handle(WindowEvent event) {
System.out.println("closing this sidestage");
MainApp.sideStages.remove(SideStage.this);
}
});
}
private TabPane initTabArea() {
final TabPane tabPane = GuiPartFactory.createTabPane();
tabPane.addEventHandler(Tab.TAB_CLOSE_REQUEST_EVENT, new EventHandler<Event>() {
#Override
public void handle(Event event) {
System.out.println("tab got closed mon!");
if (tabPane.getTabs().isEmpty()) {
SideStage.this.close();
}
}
});
VBox.setVgrow(tabPane, Priority.ALWAYS);
return tabPane;
}
public void addTab(Tab tab) {
tabPane.getTabs().add(tab);
}
And the GuiPartFactory class:
public static ResourceBundle BUNDLE = ResourceBundle.getBundle("locales/Bundle", new Locale("en", "GB"));
public static TabPane createTabPane() {
final TabPane tabPane = new TabPane();
//This event gets fired when the cursor is holding a draggable object over this tabpane
tabPane.setOnDragOver(new EventHandler<DragEvent>() {
#Override
public void handle(DragEvent event) {
final Dragboard dragboard = event.getDragboard();
if (dragboard.hasString() && dragboard.getString().equals("tab") && ((Tab) MainApp.dndTemp).getTabPane() != tabPane) {
event.acceptTransferModes(TransferMode.MOVE);
event.consume();
}
}
});
//This event gets fired when the cursor is releasing a draggable object over this tabpane (this gets only called if it has been accepted in the previos dragover event!)
tabPane.setOnDragDropped(new EventHandler<DragEvent>() {
#Override
public void handle(DragEvent event) {
System.out.println("dropped");
Tab tab = (Tab) MainApp.dndTemp;
MainApp.dndHandled = true;
tab.getTabPane().getTabs().remove(tab);
tabPane.getTabs().add(tab);
tabPane.getSelectionModel().select(tab);
event.setDropCompleted(true);
event.consume();
}
});
return tabPane;
}
public static Tab createTab(String text) {
final Tab tab = new Tab();
final Label label = new Label("Tab" + text);
tab.setGraphic(label);
StackPane pane = new StackPane();
//need to set a real size here
pane.setPrefSize(500, 500);
tab.setContent(pane);
tab.getContent().setOnDragOver(new EventHandler<DragEvent>() {
#Override
public void handle(DragEvent event) {
final Dragboard dragboard = event.getDragboard();
if (dragboard.hasString() && dragboard.getString().equals("tab") && ((Tab) MainApp.dndTemp).getTabPane() != tab.getTabPane()) {
event.acceptTransferModes(TransferMode.MOVE);
event.consume();
}
}
});
tab.getContent().setOnDragDropped(new EventHandler<DragEvent>() {
#Override
public void handle(DragEvent event) {
System.out.println("dropped");
Tab transferTab = (Tab) MainApp.dndTemp;
MainApp.dndHandled = true;
transferTab.getTabPane().getTabs().remove(transferTab);
tab.getTabPane().getTabs().add(transferTab);
tab.getTabPane().getSelectionModel().select(transferTab);
event.setDropCompleted(true);
event.consume();
}
});
label.setOnDragDetected(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
Dragboard dragboard = label.startDragAndDrop(TransferMode.MOVE);
ClipboardContent clipboardContent = new ClipboardContent();
clipboardContent.putString("tab");
MainApp.dndTemp = tab;
MainApp.dndHandled = false;
dragboard.setContent(clipboardContent);
dragboard.setDragView(new Image("img/dragcursor.png"));
event.consume();
}
});
label.setOnDragDone(new EventHandler<DragEvent>() {
#Override
public void handle(DragEvent event) {
//our dragndrop failed
if (!MainApp.dndHandled) {
//lets see if it failed because we dropped outside of our java-windows
if (!MainApp.dndInside) {
if (!MainApp.dndTemp.equals(tab)) {
System.out.println("something is wrong here");
}
System.out.println("gotta make a new window!");
SideStage sideStage = new SideStage(event.getScreenX(), event.getScreenY());
MainApp.sideStages.add(sideStage);
//just a check to make sure we don't access a null variable
if (tab.getTabPane() != null) {
tab.getTabPane().getTabs().remove(tab);
}
sideStage.addTab(tab);
sideStage.sizeToScene();
tab.getTabPane().getSelectionModel().select(tab);
System.out.println("width: " + ((StackPane) tab.getContent()).getWidth());
System.out.println("height: " + ((StackPane) tab.getContent()).getHeight());
System.out.println("width: " + tab.getTabPane().getWidth());
System.out.println("height: " + tab.getTabPane().getHeight());
sideStage.show();
}
}
}
});
return tab;
}
public static Tab createTabRandomColor(String text) {
Random rng = new Random();
Tab tab = GuiPartFactory.createTab(text);
StackPane pane = (StackPane) tab.getContent();
int red = rng.nextInt(256);
int green = rng.nextInt(256);
int blue = rng.nextInt(256);
String style = String.format("-fx-background-color: rgb(%d, %d, %d);", red, green, blue);
pane.setStyle(style);
Label label = new Label("This is tab " + text);
label.setStyle(String.format("-fx-text-fill: rgb(%d, %d, %d);", 256 - red, 256 - green, 256 - blue));
pane.getChildren().add(label);
return tab;
}
public static MenuBar initMenuAndTitle(Stage stage) {
//setting up a window title
stage.setTitle(BUNDLE.getString("title.name"));
//adding the menubar
MenuBar menuBar = new MenuBar();
//adding the base menu entry
Menu menuStart = new Menu(BUNDLE.getString("menu.start"));
Menu menuView = new Menu(BUNDLE.getString("menu.view"));
Menu menuHelp = new Menu(BUNDLE.getString("menu.help"));
menuBar.getMenus().addAll(menuStart, menuView, menuHelp);
//adding the menuitems inside the menus
MenuItem aboutItem = new MenuItem(BUNDLE.getString("menu.about"));
aboutItem.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
Dialogs popup = Dialogs.create();
popup.title(BUNDLE.getString("menu.about.title"));
popup.message(BUNDLE.getString("menu.about.message"));
popup.showInformation();
}
});
menuHelp.getItems().add(aboutItem);
MenuItem exitItem = new MenuItem(BUNDLE.getString("menu.exit"));
exitItem.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
Dialogs popup = Dialogs.create();
popup.title(BUNDLE.getString("menu.exit.title"));
popup.message(BUNDLE.getString("menu.exit.message"));
popup.actions(new Action[]{YES, NO});
Action response = popup.showConfirm();
if(response.equals(YES)) {
System.exit(0);
}
}
});
menuStart.getItems().add(exitItem);
return menuBar;
}
And finally the MainApp class:
//for handling tag drag'n'drop outside of java windows
public static Object dndTemp = null;
public static boolean dndHandled = true;
public static boolean dndInside = true;
public static Set<SideStage> sideStages = new HashSet<>();
//holding the tabs (doe)
private TabPane tabPane;
#Override
public void start(Stage stage) {
tabPane = initTabs();
VBox root = new VBox(0, GuiPartFactory.initMenuAndTitle(stage), tabPane);
Scene scene = new Scene(root, 800, 600);
stage.setScene(scene);
stage.show();
scene.setOnDragExited(new DnDExitAndEntryHandler());
scene.setOnDragEntered(new DnDExitAndEntryHandler());
}
private TabPane initTabs() {
TabPane tabPane = GuiPartFactory.createTabPane();
Tab bedraggin = GuiPartFactory.createTab("be draggin");
Tab beraggin = GuiPartFactory.createTab("be raggin");
tabPane.getTabs().add(bedraggin);
tabPane.getTabs().add(beraggin);
for (int i = 0; i < 3; i++) {
Tab tab = GuiPartFactory.createTabRandomColor("" + i);
tabPane.getTabs().add(tab);
}
VBox.setVgrow(tabPane, Priority.ALWAYS);
return tabPane;
}
/**
* The main() method is ignored in correctly deployed JavaFX application.
* main() serves only as fallback in case the application can not be
* launched through deployment artifacts, e.g., in IDEs with limited FX
* support. NetBeans ignores main().
*
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
public boolean removeSideStage(SideStage sideStage) {
return sideStages.remove(sideStage);
}
public void addTab(Tab tab) {
tabPane.getTabs().add(tab);
}
public void removeTab(Tab tab) {
tabPane.getTabs().remove(tab);
}
edit: A temporary workaround is to put the actual adding of the tab into the new TabPane into a runnable. But i think this is a rather bad solution:
Platform.runLater(new Runnable() {
#Override
public void run() {
sideStage.addTab(tab);
tab.getTabPane().getSelectionModel().select(tab);
}
});