How to Wire One Pane to Another

How to wire one pane to another

Here's an example using the observer pattern, also seen here, here and here. Note that it would also be possible to listen to the combo's model.

Sample Image

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.*;

/**
* @see http://en.wikipedia.org/wiki/Observer_pattern
* @see https://stackoverflow.com/a/10523401/230513
*/
public class PropertyChangeDemo {

public PropertyChangeDemo() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);
f.add(new ObserverPanel());
f.pack();
f.setLocationByPlatform(true);
f.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
PropertyChangeDemo example = new PropertyChangeDemo();
}
});
}
}

class ObserverPanel extends JPanel {

private JLabel title = new JLabel("Value received: ");
private JLabel label = new JLabel("null", JLabel.CENTER);

public ObserverPanel() {
this.setBorder(BorderFactory.createTitledBorder("ObserverPanel"));
JPanel panel = new JPanel(new GridLayout(0, 1));
panel.add(title);
panel.add(label);
this.add(panel);
ObservedPanel observed = new ObservedPanel();
observed.addPropertyChangeListener(new PropertyChangeListener() {

@Override
public void propertyChange(PropertyChangeEvent e) {
if (e.getPropertyName().equals(ObservedPanel.PHYSICIST)) {
String value = e.getNewValue().toString();
label.setText(value);
}
}
});
this.add(observed);
}
}

class ObservedPanel extends JPanel {

public static final String PHYSICIST = "Physicist";
private static final String[] items = new String[]{
"Alpher", "Bethe", "Gamow", "Dirac", "Einstein"
};
private JComboBox combo = new JComboBox(items);
private String oldValue;

public ObservedPanel() {
this.setBorder(BorderFactory.createTitledBorder("ObservedPanel"));
combo.addActionListener(new ComboBoxListener());
this.add(combo);
}

private class ComboBoxListener implements ActionListener {

@Override
public void actionPerformed(ActionEvent ae) {
String newValue = (String) combo.getSelectedItem();
firePropertyChange(PHYSICIST, oldValue, newValue);
oldValue = newValue;
}
}
}

JavaFX drag button from one pane to another

There's no way to add a button to the clipboard content (i.e. to the dragboard). You can only add specific types (string, image) and objects that implement serializable (button doesn't, and it wouldn't do what you wanted anyway). The drag-and-drop API is very deficient in this aspect, imho. You should just add some dummy text to the dragboard and keep a reference to the button that is currently being dragged.

Quick SSCCE:

import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.SplitPane;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DataFormat;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class DragAndDropButton extends Application {

private final DataFormat buttonFormat = new DataFormat("com.example.myapp.formats.button");

private Button draggingButton ;

@Override
public void start(Stage primaryStage) {
FlowPane pane1 = new FlowPane();
FlowPane pane2 = new FlowPane();

for (int i = 1 ; i <= 10; i++) {
pane1.getChildren().add(createButton("Button "+i));
}

addDropHandling(pane1);
addDropHandling(pane2);

SplitPane splitPane = new SplitPane(pane1, pane2);
splitPane.setOrientation(Orientation.VERTICAL);

Scene scene = new Scene(splitPane, 600, 600);
primaryStage.setScene(scene);
primaryStage.show();
}

private Button createButton(String text) {
Button button = new Button(text);
button.setOnDragDetected(e -> {
Dragboard db = button.startDragAndDrop(TransferMode.MOVE);
db.setDragView(button.snapshot(null, null));
ClipboardContent cc = new ClipboardContent();
cc.put(buttonFormat, "button");
db.setContent(cc);
draggingButton = button ;
});
button.setOnDragDone(e -> draggingButton = null);
return button ;
}

private void addDropHandling(Pane pane) {
pane.setOnDragOver(e -> {
Dragboard db = e.getDragboard();
if (db.hasContent(buttonFormat)
&& draggingButton != null
&& draggingButton.getParent() != pane) {
e.acceptTransferModes(TransferMode.MOVE);
}
});

pane.setOnDragDropped(e -> {
Dragboard db = e.getDragboard();
if (db.hasContent(buttonFormat)) {
((Pane)draggingButton.getParent()).getChildren().remove(draggingButton);
pane.getChildren().add(draggingButton);
e.setDropCompleted(true);
}
});
}

public static void main(String[] args) {
launch(args);
}
}

accessing a Pane from another class in javafx

MainController is never initialized in SecondController. You should pass it when creating second controller in nextBtnAction.

MainController code:

private void nextBtnAction(ActionEvent event) {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("firstPane.fxml"));
Parent pessFxml = loader.load();
SecondController controller = (SecondController)loader.getController();
controller.setMainController(this);
secondPane.getChildren().add(pessFxml);
nextTransition = new TranslateTransition(Duration.millis(300), secondPane);
nextTransition.setToX(searchPane.getLayoutX()-secondPane.getLayoutX());
nextTransition.play();
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "Form does not found" ,ex.toString(),0);
}
}

SecondController:

public void setMainController(MainController controller) {
this.mainControll = controller;
}

Trying to pass values from one JPanel to another JPanel

I need something more simple, I think.

I infer that you want ActiveCall to receive an event the signifies a change in the selection state of the JList in CallsPanel. CallsPanel itself contains a ListSelectionListener that updates txfCall. As a component can have more that one listener, let ActiveCall also contain a ListSelectionListener that is registered to receive events from lstOpkald.

If no existing event suits your need, you can define you own PropertyChangeEvent, as shown here.

Set the content of a pane with the pane of another FXML file

Program entry point:

public class App extends Application {
@Override
public void start(Stage primaryStage) throws IOException {
Parent startScreen
= FXMLLoader.load(getClass().getResource("MainScreen.fxml"));
Scene scene = new Scene(startScreen);
primaryStage.setScene(scene);
primaryStage.show();

}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

}

MainScreenController

public class MainScreenController implements Initializable{

private static AnchorPane contentBox;

@FXML
private AnchorPane paneContent;

public MainScreenController() throws IOException {
this.paneContent = FXMLLoader.load(getClass().getResource("Home.fxml"));

}

@Override
public void initialize(URL url, ResourceBundle rb) {
MainScreenController.contentBox = this.paneContent;
}

public static AnchorPane getContentBox(){
return MainScreenController.contentBox;
}

}

Then MainScreen.fxml needs to have MainScreenController as controller and also needs to contain an AnchorPane with fx:id paneContent.
Then from anywhere in your program you can call getContentBox() and use .set() to change the screen.

Is it possible to move single open file between panes in VS Code?

Open the command palette and search for View: Move Editor into to bring up related commands. In this case, you might be specifically looking for the following:

Command

Move Editor into Next Group

Shortcuts

  • Mac: ctrl+cmd+right
  • Windows/Linux: ctrl+alt+right).


Related Topics



Leave a reply



Submit