creating custom panel with a label centered that can be edited afterwards












1















I trying to implement a custom panel that had a label in the center which has a custom background color. the Label has a custom contextMenu that has the options to "change the String", "Change the color" , "Copy settings", and "Paste Settings" for the change string it is supposed to change the text in the Label. Im currently stuck with the Label. I want to be able to edit any of the two labels at any time



import static java.lang.Math.random;
import java.util.Optional;
import java.util.Random;
import javafx.beans.property.ReadOnlyDoubleProperty;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.VPos;
import javafx.scene.control.Alert;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextInputDialog;
import javafx.scene.input.ContextMenuEvent;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

/**
*
* @author Gleyv
*/
public class CustomPanel extends Region{
public Label topLabel = new Label();
public Rectangle rect = new Rectangle();;
private StackPane rootPane = new StackPane(topLabel) ;
private Color mColor;
private static final double aspectRatio = 12.0 / 20.0;
public CustomPanel()
{

rect.boundsInParentProperty();
getChildren().addAll(rootPane);
setContext();
setStyle("-fx-background-color: #8fbc8f");
//setStyle("/customStyle/label");
}


/**
*
*/

public void setContext(){
ContextMenu contextMenu = new ContextMenu();

MenuItem cString = new MenuItem("Change String");
cString.setOnAction(actionEvent -> onString());
MenuItem cColor = new MenuItem("Change Color");
cColor.setOnAction(actionEvent -> onColor());
MenuItem cSettings = new MenuItem("Copy Settings");
cSettings.setOnAction(actionEvent -> onCopySettings());
MenuItem pSettings = new MenuItem("Paste Settings");
pSettings.setOnAction(actionEvent -> onPSettings());
contextMenu.getItems().addAll(cString, cColor, cSettings, pSettings);

topLabel.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>()
{

@Override
public void handle(ContextMenuEvent event) {

contextMenu.show(topLabel, event.getScreenX(),
event.getScreenY());
}
});

}

@Override
protected void layoutChildren(){
//topLabel= new Label("Im the label now");
double width = getWidth();
double height = getHeight();

double safeWidth = height * aspectRatio;
double safeHeight = width / aspectRatio;

if (safeHeight > height) safeHeight = height;
if (safeWidth > width) { safeWidth = width;
}
rootPane.setPrefWidth(safeWidth);
rootPane.setPrefWidth(safeHeight);
//setLabel("im the label");
layoutInArea(rootPane, 0.0D, 0.0D, width, height,


getBaselineOffset(), HPos.LEFT, VPos.CENTER);

}

private void onString() {
TextInputDialog dialog = new TextInputDialog();
dialog.setTitle("Text input");
dialog.setHeaderText("Enter some string");
Optional<String> result = dialog.showAndWait();

String entered = result.get();
System.out.println(entered);
topLabel.setText(entered);
getChildren().add(topLabel);

}

private void onColor() {
Random random = new Random();
int r = random.nextInt(255);
int g = random.nextInt(255);
int b = random.nextInt(255);
mColor = Color.rgb(r, g, b);
setStyle("-fx-color= " + mColor);
}

private void onCopySettings() {
throw new UnsupportedOperationException("Not supported yet."); //To
change body of generated methods, choose Tools | Templates.
}

private void onPSettings() {
throw new UnsupportedOperationException("Not supported yet."); //To
change body of generated methods, choose Tools | Templates.
}

}//end of CustomPanel


enter image description here










share|improve this question

























  • Why are you stuck ? Because your label appears twice instead of once ?

    – Pagbo
    Nov 15 '18 at 9:40













  • Can you please modify your question by letting us know what is the exact issue.

    – Sai Dandem
    Nov 16 '18 at 0:09











  • yes I have two custom panels in a vbox, and i want to be able to edit any of the two labels

    – John Wick
    Nov 16 '18 at 5:56











  • i want to be able to change the background color when i right click on the label

    – John Wick
    Nov 16 '18 at 7:14
















1















I trying to implement a custom panel that had a label in the center which has a custom background color. the Label has a custom contextMenu that has the options to "change the String", "Change the color" , "Copy settings", and "Paste Settings" for the change string it is supposed to change the text in the Label. Im currently stuck with the Label. I want to be able to edit any of the two labels at any time



import static java.lang.Math.random;
import java.util.Optional;
import java.util.Random;
import javafx.beans.property.ReadOnlyDoubleProperty;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.VPos;
import javafx.scene.control.Alert;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextInputDialog;
import javafx.scene.input.ContextMenuEvent;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

/**
*
* @author Gleyv
*/
public class CustomPanel extends Region{
public Label topLabel = new Label();
public Rectangle rect = new Rectangle();;
private StackPane rootPane = new StackPane(topLabel) ;
private Color mColor;
private static final double aspectRatio = 12.0 / 20.0;
public CustomPanel()
{

rect.boundsInParentProperty();
getChildren().addAll(rootPane);
setContext();
setStyle("-fx-background-color: #8fbc8f");
//setStyle("/customStyle/label");
}


/**
*
*/

public void setContext(){
ContextMenu contextMenu = new ContextMenu();

MenuItem cString = new MenuItem("Change String");
cString.setOnAction(actionEvent -> onString());
MenuItem cColor = new MenuItem("Change Color");
cColor.setOnAction(actionEvent -> onColor());
MenuItem cSettings = new MenuItem("Copy Settings");
cSettings.setOnAction(actionEvent -> onCopySettings());
MenuItem pSettings = new MenuItem("Paste Settings");
pSettings.setOnAction(actionEvent -> onPSettings());
contextMenu.getItems().addAll(cString, cColor, cSettings, pSettings);

topLabel.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>()
{

@Override
public void handle(ContextMenuEvent event) {

contextMenu.show(topLabel, event.getScreenX(),
event.getScreenY());
}
});

}

@Override
protected void layoutChildren(){
//topLabel= new Label("Im the label now");
double width = getWidth();
double height = getHeight();

double safeWidth = height * aspectRatio;
double safeHeight = width / aspectRatio;

if (safeHeight > height) safeHeight = height;
if (safeWidth > width) { safeWidth = width;
}
rootPane.setPrefWidth(safeWidth);
rootPane.setPrefWidth(safeHeight);
//setLabel("im the label");
layoutInArea(rootPane, 0.0D, 0.0D, width, height,


getBaselineOffset(), HPos.LEFT, VPos.CENTER);

}

private void onString() {
TextInputDialog dialog = new TextInputDialog();
dialog.setTitle("Text input");
dialog.setHeaderText("Enter some string");
Optional<String> result = dialog.showAndWait();

String entered = result.get();
System.out.println(entered);
topLabel.setText(entered);
getChildren().add(topLabel);

}

private void onColor() {
Random random = new Random();
int r = random.nextInt(255);
int g = random.nextInt(255);
int b = random.nextInt(255);
mColor = Color.rgb(r, g, b);
setStyle("-fx-color= " + mColor);
}

private void onCopySettings() {
throw new UnsupportedOperationException("Not supported yet."); //To
change body of generated methods, choose Tools | Templates.
}

private void onPSettings() {
throw new UnsupportedOperationException("Not supported yet."); //To
change body of generated methods, choose Tools | Templates.
}

}//end of CustomPanel


enter image description here










share|improve this question

























  • Why are you stuck ? Because your label appears twice instead of once ?

    – Pagbo
    Nov 15 '18 at 9:40













  • Can you please modify your question by letting us know what is the exact issue.

    – Sai Dandem
    Nov 16 '18 at 0:09











  • yes I have two custom panels in a vbox, and i want to be able to edit any of the two labels

    – John Wick
    Nov 16 '18 at 5:56











  • i want to be able to change the background color when i right click on the label

    – John Wick
    Nov 16 '18 at 7:14














1












1








1








I trying to implement a custom panel that had a label in the center which has a custom background color. the Label has a custom contextMenu that has the options to "change the String", "Change the color" , "Copy settings", and "Paste Settings" for the change string it is supposed to change the text in the Label. Im currently stuck with the Label. I want to be able to edit any of the two labels at any time



import static java.lang.Math.random;
import java.util.Optional;
import java.util.Random;
import javafx.beans.property.ReadOnlyDoubleProperty;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.VPos;
import javafx.scene.control.Alert;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextInputDialog;
import javafx.scene.input.ContextMenuEvent;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

/**
*
* @author Gleyv
*/
public class CustomPanel extends Region{
public Label topLabel = new Label();
public Rectangle rect = new Rectangle();;
private StackPane rootPane = new StackPane(topLabel) ;
private Color mColor;
private static final double aspectRatio = 12.0 / 20.0;
public CustomPanel()
{

rect.boundsInParentProperty();
getChildren().addAll(rootPane);
setContext();
setStyle("-fx-background-color: #8fbc8f");
//setStyle("/customStyle/label");
}


/**
*
*/

public void setContext(){
ContextMenu contextMenu = new ContextMenu();

MenuItem cString = new MenuItem("Change String");
cString.setOnAction(actionEvent -> onString());
MenuItem cColor = new MenuItem("Change Color");
cColor.setOnAction(actionEvent -> onColor());
MenuItem cSettings = new MenuItem("Copy Settings");
cSettings.setOnAction(actionEvent -> onCopySettings());
MenuItem pSettings = new MenuItem("Paste Settings");
pSettings.setOnAction(actionEvent -> onPSettings());
contextMenu.getItems().addAll(cString, cColor, cSettings, pSettings);

topLabel.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>()
{

@Override
public void handle(ContextMenuEvent event) {

contextMenu.show(topLabel, event.getScreenX(),
event.getScreenY());
}
});

}

@Override
protected void layoutChildren(){
//topLabel= new Label("Im the label now");
double width = getWidth();
double height = getHeight();

double safeWidth = height * aspectRatio;
double safeHeight = width / aspectRatio;

if (safeHeight > height) safeHeight = height;
if (safeWidth > width) { safeWidth = width;
}
rootPane.setPrefWidth(safeWidth);
rootPane.setPrefWidth(safeHeight);
//setLabel("im the label");
layoutInArea(rootPane, 0.0D, 0.0D, width, height,


getBaselineOffset(), HPos.LEFT, VPos.CENTER);

}

private void onString() {
TextInputDialog dialog = new TextInputDialog();
dialog.setTitle("Text input");
dialog.setHeaderText("Enter some string");
Optional<String> result = dialog.showAndWait();

String entered = result.get();
System.out.println(entered);
topLabel.setText(entered);
getChildren().add(topLabel);

}

private void onColor() {
Random random = new Random();
int r = random.nextInt(255);
int g = random.nextInt(255);
int b = random.nextInt(255);
mColor = Color.rgb(r, g, b);
setStyle("-fx-color= " + mColor);
}

private void onCopySettings() {
throw new UnsupportedOperationException("Not supported yet."); //To
change body of generated methods, choose Tools | Templates.
}

private void onPSettings() {
throw new UnsupportedOperationException("Not supported yet."); //To
change body of generated methods, choose Tools | Templates.
}

}//end of CustomPanel


enter image description here










share|improve this question
















I trying to implement a custom panel that had a label in the center which has a custom background color. the Label has a custom contextMenu that has the options to "change the String", "Change the color" , "Copy settings", and "Paste Settings" for the change string it is supposed to change the text in the Label. Im currently stuck with the Label. I want to be able to edit any of the two labels at any time



import static java.lang.Math.random;
import java.util.Optional;
import java.util.Random;
import javafx.beans.property.ReadOnlyDoubleProperty;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.VPos;
import javafx.scene.control.Alert;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextInputDialog;
import javafx.scene.input.ContextMenuEvent;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

/**
*
* @author Gleyv
*/
public class CustomPanel extends Region{
public Label topLabel = new Label();
public Rectangle rect = new Rectangle();;
private StackPane rootPane = new StackPane(topLabel) ;
private Color mColor;
private static final double aspectRatio = 12.0 / 20.0;
public CustomPanel()
{

rect.boundsInParentProperty();
getChildren().addAll(rootPane);
setContext();
setStyle("-fx-background-color: #8fbc8f");
//setStyle("/customStyle/label");
}


/**
*
*/

public void setContext(){
ContextMenu contextMenu = new ContextMenu();

MenuItem cString = new MenuItem("Change String");
cString.setOnAction(actionEvent -> onString());
MenuItem cColor = new MenuItem("Change Color");
cColor.setOnAction(actionEvent -> onColor());
MenuItem cSettings = new MenuItem("Copy Settings");
cSettings.setOnAction(actionEvent -> onCopySettings());
MenuItem pSettings = new MenuItem("Paste Settings");
pSettings.setOnAction(actionEvent -> onPSettings());
contextMenu.getItems().addAll(cString, cColor, cSettings, pSettings);

topLabel.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>()
{

@Override
public void handle(ContextMenuEvent event) {

contextMenu.show(topLabel, event.getScreenX(),
event.getScreenY());
}
});

}

@Override
protected void layoutChildren(){
//topLabel= new Label("Im the label now");
double width = getWidth();
double height = getHeight();

double safeWidth = height * aspectRatio;
double safeHeight = width / aspectRatio;

if (safeHeight > height) safeHeight = height;
if (safeWidth > width) { safeWidth = width;
}
rootPane.setPrefWidth(safeWidth);
rootPane.setPrefWidth(safeHeight);
//setLabel("im the label");
layoutInArea(rootPane, 0.0D, 0.0D, width, height,


getBaselineOffset(), HPos.LEFT, VPos.CENTER);

}

private void onString() {
TextInputDialog dialog = new TextInputDialog();
dialog.setTitle("Text input");
dialog.setHeaderText("Enter some string");
Optional<String> result = dialog.showAndWait();

String entered = result.get();
System.out.println(entered);
topLabel.setText(entered);
getChildren().add(topLabel);

}

private void onColor() {
Random random = new Random();
int r = random.nextInt(255);
int g = random.nextInt(255);
int b = random.nextInt(255);
mColor = Color.rgb(r, g, b);
setStyle("-fx-color= " + mColor);
}

private void onCopySettings() {
throw new UnsupportedOperationException("Not supported yet."); //To
change body of generated methods, choose Tools | Templates.
}

private void onPSettings() {
throw new UnsupportedOperationException("Not supported yet."); //To
change body of generated methods, choose Tools | Templates.
}

}//end of CustomPanel


enter image description here







java user-interface javafx javafx-8






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 6:49







John Wick

















asked Nov 15 '18 at 9:32









John WickJohn Wick

64




64













  • Why are you stuck ? Because your label appears twice instead of once ?

    – Pagbo
    Nov 15 '18 at 9:40













  • Can you please modify your question by letting us know what is the exact issue.

    – Sai Dandem
    Nov 16 '18 at 0:09











  • yes I have two custom panels in a vbox, and i want to be able to edit any of the two labels

    – John Wick
    Nov 16 '18 at 5:56











  • i want to be able to change the background color when i right click on the label

    – John Wick
    Nov 16 '18 at 7:14



















  • Why are you stuck ? Because your label appears twice instead of once ?

    – Pagbo
    Nov 15 '18 at 9:40













  • Can you please modify your question by letting us know what is the exact issue.

    – Sai Dandem
    Nov 16 '18 at 0:09











  • yes I have two custom panels in a vbox, and i want to be able to edit any of the two labels

    – John Wick
    Nov 16 '18 at 5:56











  • i want to be able to change the background color when i right click on the label

    – John Wick
    Nov 16 '18 at 7:14

















Why are you stuck ? Because your label appears twice instead of once ?

– Pagbo
Nov 15 '18 at 9:40







Why are you stuck ? Because your label appears twice instead of once ?

– Pagbo
Nov 15 '18 at 9:40















Can you please modify your question by letting us know what is the exact issue.

– Sai Dandem
Nov 16 '18 at 0:09





Can you please modify your question by letting us know what is the exact issue.

– Sai Dandem
Nov 16 '18 at 0:09













yes I have two custom panels in a vbox, and i want to be able to edit any of the two labels

– John Wick
Nov 16 '18 at 5:56





yes I have two custom panels in a vbox, and i want to be able to edit any of the two labels

– John Wick
Nov 16 '18 at 5:56













i want to be able to change the background color when i right click on the label

– John Wick
Nov 16 '18 at 7:14





i want to be able to change the background color when i right click on the label

– John Wick
Nov 16 '18 at 7:14












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53316291%2fcreating-custom-panel-with-a-label-centered-that-can-be-edited-afterwards%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53316291%2fcreating-custom-panel-with-a-label-centered-that-can-be-edited-afterwards%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly