Created
July 15, 2014 22:11
-
-
Save mansueli/eacf74ee63a31a23851f to your computer and use it in GitHub Desktop.
Custom Pane (minimizes itself) http://stackoverflow.com/questions/24756285/javafx-how-to-make-a-netbeans-like-tool-window
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<?import java.lang.*?> | |
<?import javafx.scene.control.*?> | |
<?import javafx.scene.layout.*?> | |
<?import javafx.scene.shape.*?> | |
<fx:root prefHeight="400.0" prefWidth="400.0" | |
type="javafx.scene.layout.AnchorPane" xmlns="http://javafx.com/javafx/8" | |
xmlns:fx="http://javafx.com/fxml/1"> | |
<children> | |
<Rectangle fx:id="separator" arcHeight="5.0" arcWidth="5.0" fill="WHITE" | |
height="33.0" layoutY="1.0" stroke="BLACK" strokeType="INSIDE" width="600.0" /> | |
<Button fx:id="closePane" layoutX="573.0" layoutY="6.0" | |
mnemonicParsing="false" text="X" /> | |
<Button fx:id="minPane" layoutX="545.0" layoutY="6.0" | |
mnemonicParsing="false" text="__" /> | |
<Label fx:id="title" layoutX="14.0" layoutY="12.0" text="Title" /> | |
<Pane fx:id="contentPane" layoutY="34.0" prefHeight="366.0" | |
prefWidth="600.0" /> | |
</children> | |
</fx:root> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.URL; | |
import java.util.ResourceBundle; | |
import javafx.collections.ObservableList; | |
import javafx.event.ActionEvent; | |
import javafx.event.EventHandler; | |
import javafx.fxml.FXML; | |
import javafx.fxml.FXMLLoader; | |
import javafx.fxml.Initializable; | |
import javafx.scene.Node; | |
import javafx.scene.control.Button; | |
import javafx.scene.control.Label; | |
import javafx.scene.layout.AnchorPane; | |
import javafx.scene.layout.Pane; | |
import javafx.scene.shape.Rectangle; | |
public class CustomPane extends AnchorPane implements Initializable { | |
@FXML Button closePane; | |
@FXML Button minPane; | |
@FXML Label title; | |
@FXML Pane contentPane; | |
@FXML Rectangle separator; | |
private boolean restoreFlag = false; | |
private double prefX; | |
private double prefY; | |
public CustomPane(String title){ | |
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource( | |
"CustomPane.fxml")); | |
fxmlLoader.setRoot(this); | |
fxmlLoader.setController(this); | |
try { | |
fxmlLoader.load(); | |
} catch (Exception exception) { | |
throw new RuntimeException(exception); | |
} | |
this.title.setText(title); | |
setButtonsLayout(); | |
setStandardLayout(); | |
} | |
public CustomPane(String title, double y, double x){ | |
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource( | |
"CustomPane.fxml")); | |
fxmlLoader.setRoot(this); | |
fxmlLoader.setController(this); | |
try { | |
fxmlLoader.load(); | |
} catch (Exception exception) { | |
throw new RuntimeException(exception); | |
} | |
this.title.setText(title); | |
this.setPrefSize(x, y); | |
setButtonsLayout(); | |
setStandardLayout(); | |
restoreRoot(); | |
} | |
@Override | |
public void initialize(URL location, ResourceBundle resources) { | |
closePane.setOnAction(new EventHandler<ActionEvent>(){ | |
@Override | |
public void handle(ActionEvent event) { | |
setInvisible(); | |
} | |
}); | |
minPane.setOnAction(new EventHandler<ActionEvent>(){ | |
@Override | |
public void handle(ActionEvent event) { | |
if(restoreFlag){ | |
restoreFlag = false; | |
minPane.setText("_"); | |
restoreRoot(); | |
} | |
else{ | |
restoreFlag = true; | |
minPane.setText("▭"); | |
minimizeRoot(); | |
} | |
} | |
}); | |
} | |
protected void minimizeRoot() { | |
this.setPrefSize(prefX/2, title.getPrefHeight() + 5.0); | |
separator.setWidth(prefX/2 + 5); | |
this.setLayoutX(0); | |
this.setLayoutY(prefY-40); | |
setButtonsLayout(); | |
contentPane.setVisible(false); | |
} | |
protected void restoreRoot() { | |
this.setPrefSize(prefX, prefY); | |
separator.setWidth(prefX); | |
contentPane.setVisible(true); | |
this.setLayoutX(0); | |
this.setLayoutY(0); | |
setButtonsLayout(); | |
} | |
private void setStandardLayout() { | |
prefX = this.getPrefHeight(); | |
prefY = this.getPrefWidth()-50; | |
} | |
public void setButtonsLayout(){ | |
closePane.setLayoutX(this.getPrefWidth()-30); | |
minPane.setLayoutX(this.getPrefWidth()-55); | |
} | |
public void setInvisible(){ | |
this.setVisible(false); | |
} | |
public ObservableList<Node> getContent(){ | |
return contentPane.getChildren(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.application.Application; | |
import javafx.scene.Scene; | |
import javafx.scene.control.TreeItem; | |
import javafx.scene.control.TreeView; | |
import javafx.scene.layout.Pane; | |
import javafx.stage.Stage; | |
public class CustomTest extends Application{ | |
@Override | |
public void start(Stage primaryStage) throws Exception { | |
Pane p = new Pane(); | |
CustomPane cp = new CustomPane("Costumer Window",400,500); | |
TreeItem<String> rootItem = new TreeItem<String> ("Inbox"); | |
rootItem.setExpanded(true); | |
for (int i = 1; i < 6; i++) { | |
TreeItem<String> item = new TreeItem<String> ("Message" + i); | |
rootItem.getChildren().add(item); | |
} | |
TreeView<String> tree = new TreeView<String> (rootItem); | |
tree.setPrefSize(400,500); | |
cp.getContent().add(tree); | |
p.getChildren().add(cp); | |
Scene scene = new Scene(p); | |
primaryStage.setScene(scene); | |
primaryStage.show(); | |
} | |
public static void main(String[] args){ | |
launch(args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment