0
@FXML
Private void handleItemBackAction(ActionEvent eve)
{  
    java.awt.Color color=JColorChooser.showDialog(null,"Select a color",java.awt.Color.CYAN);

    String hex = Integer.toHexString(color.getRGB() & 0xffffff);

    hex="#"+hex;
    Text.setText(hex);
    ShortcutButton.setStyle("-fx-background-color: " + hex + ";");
}

When I run this window and click on button at first time color chooser goes behind my actual pane.

When I click on button second time while running it shows at top of all other pane which is correct and so on it works properly.

Then why color chooser not shows in front first time on button click?

Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
Drashti Pandya
  • 348
  • 1
  • 5
  • 15

1 Answers1

0

The first argument to JColorChooser.showDialog is the parent component of the dialog. You told that method to show the dialog with no parent, so it doesn't know about your other windows.

Instead of using JColorChooser.showDialog, you'll need to embed a JColorChooser instance inside a JavaFX dialog or window:

JColorChooser colorChooser = new JColorChooser(java.awt.Color.CYAN);

SwingNode colorChooserNode = new SwingNode();
colorChooserNode.setContent(colorChooser);

Alert dialog = new Alert(Alert.AlertType.NONE);
// Guarantees dialog will be above (and will block input to) mainStage.
dialog.initOwner(mainStage);
dialog.setTitle("Select a color");

dialog.getDialogPane().setContent(colorChooserNode);

dialog.getDialogPane().getButtonTypes().setAll(
    ButtonType.OK, ButtonType.CANCEL);

Optional<ButtonType> response = dialog.showAndWait();
if (response.filter(r -> r == ButtonType.OK).isPresent()) {
    int rgb = colorChooser.getColor().getRGB();
    String hex = String.format("#%06x", rgb & 0xffffff);

    Text.setText(hex);
    ShortcutButton.setBackground(new Background(
        new BackgroundFill(Color.valueOf(hex), null, null)));
} else {
    System.out.println("User canceled");
}

Of course, you're probably better off using ColorPicker in your main window, so you don't have to create an explicit dialog at all:

final ColorPicker colorPicker = new ColorPicker(Color.CYAN);
colorPicker.setOnAction(e -> {
    Color color = colorPicker.getValue();
    String hex = String.format("#%02x02x02x",
        (int) (color.getRed() * 255),
        (int) (color.getGreen() * 255),
        (int) (color.getBlue() * 255));

    Text.setText(hex);
    ShortcutButton.setBackground(
        new Background(new BackgroundFill(color, null, null)));
});

myLayoutPane.getChildren().add(colorPicker);

As an aside, Java variable names should always start with a lowercase letter, to make them easy to distinguish from class names. Consider changing Text to text, and ShortcutButton to shortcutButton.

VGR
  • 40,506
  • 4
  • 48
  • 63
  • when i try color chooser i haven't any stage so i create new stage and pass it.but It shows error at that point Stage mainStage=new Stage(); Caused by: java.lang.NullPointerException at javafx.scene.control.HeavyweightDialog.updateStageBindings(HeavyweightDialog.java:319) at javafx.scene.control.HeavyweightDialog.initOwner(HeavyweightDialog.java:120) – Drashti Pandya Jun 10 '15 at 07:36
  • In color picker i get right hexa value on select color but it doesn't effect on my shortcut button. – Drashti Pandya Jun 10 '15 at 07:43
  • Instead of setting the button's style, change the button's color using `shortcutButton.setBackground(new BackgroundFill(color, null, null));`. Answer updated to indicate this. – VGR Jun 10 '15 at 12:51
  • Incompatible type:Backgroundfill can't converted to Background – Drashti Pandya Jun 11 '15 at 07:44
  • Sorry, that should have been `shortcutButton.setBackground(new Background(new BackgroundFill(color, null, null)));`. The code in the answer had it correct, though my comment did not. Note that you can easily find out all of this yourself from [this documentation](http://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/Region.html#setBackground-javafx.scene.layout.Background-). – VGR Jun 11 '15 at 14:38
  • can i change position of color picker?It is not dragable so how can i do that? – Drashti Pandya Jun 16 '15 at 07:52
  • Use one subclasses of Pane defined [here](http://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/package-summary.html). – VGR Jun 16 '15 at 13:11