Newbie to JavaFX here. I'm currently working on a JavaFX project. I somehow managed to create my main window with a text, a textfield and two buttons:
However, I have some trouble positionning a button where I exactly want it to be (the red-filled box)
Output example
Here's part of my launch method (only the code related to the buttons). Please note that grid is a private GridPane attribute.
this.grid = new GridPane();
this.grid.setPadding(new Insets(15));
this.grid.setVgap(2);
this.grid.setHgap(2);
this.grid.setAlignment(Pos.CENTER);
Button submitButton = new Button("Submit");
submitButton.setStyle("-fx-font: 20 arial; -fx-base: #b6e7c9; -fx-background-radius: 10, 10, 10, 10;");
submitButton.setBackground(new Background(new BackgroundFill(Color.LIGHTGREEN, null, null)));
Button exitButton = new Button("Exit");
exitButton.setTextFill(Color.RED);
exitButton.setStyle("-fx-font: 20 arial; -fx-base: #dbd8d6; -fx-background-radius: 10, 10, 10, 10;");
exitButton.setBackground(new Background(new BackgroundFill(Color.RED, null, null)));
this.grid.add(submitButton, 0, 2);
this.grid.add(exitButton, 1, 2);
How can I manage to align the Exit button more to the left, to be symetrical with the Submit button, i.e. just under the TextField?
I really appreciate your help.
EDIT1:
The question (Text) is at 0,0. The TextField is at 0,1.
EDIT2
Code for Text and TextField:
TextField answer = new TextField();
answer.autosize();
this.answer = answer.getText();
this.grid.add(answer, 0, 1);
Text question = new Text("Hi there ! Press Submit to start! Exit to quit.");
question.setFont(Font.font("Century Gothic", FontWeight.BOLD, 22));
question.setFill(Color.BLACK);
this.grid.add(question, 0, 0);
Make the text and text field span two columns:
TextField answer = new TextField();
answer.autosize();
this.answer = answer.getText();
// node, columnIndex, rowIndex, columnSpan, rowSpan:
this.grid.add(answer, 0, 1, 2, 1);
Text question = new Text("Hi there ! Press Submit to start! Exit to quit.");
question.setFont(Font.font("Century Gothic", FontWeight.BOLD, 22));
question.setFill(Color.BLACK);
this.grid.add(question, 0, 0, 2, 1);
and then make the "Exit" button right-aligned, and it should work the way you want:
this.grid.add(submitButton, 0, 2);
this.grid.add(exitButton, 1, 2);
GridPane.setHalignment(exitButton, HPos.RIGHT);
Related
The question is in the title.
Here's my code :
JButton button = new JButton("JOUER", new ImageIcon(getClass().getResource("template.png")));
button.setBounds(0, 0, 215, 84);
button.setBackground(new Color(0,0,0,0));
button.setFont(new Font("Bebas Neue Bold", Font.TRUETYPE_FONT, 50));
button.setForeground(Color.WHITE);
button.setVerticalTextPosition(SwingConstants.CENTER);
button.setHorizontalTextPosition(SwingConstants.CENTER);
Here's the result :
And I just want to know if is it possible to outline my text.
Here's the result I want to have:
I'm relatively new to programming. Hoping someone can help. I have a button that creates a series of labels and text boxes within a frame once the user enters a number. I'm having trouble access the text boxes once they are created. Can someone point me in the right direction. Code snips below. Thanks.
method to create a label and text box.
public JTextField createPrizePanels(){
JLabel prizePanel = new JLabel("Enter Prize Here", SwingConstants.CENTER);
prizePanel.setVerticalAlignment(SwingConstants.TOP);
prizePanel.setFont(new Font("Arial", Font.BOLD, 14));
prizePanel.setForeground(Color.BLUE);
Border border = BorderFactory.createLineBorder(Color.GRAY, 1);
prizePanel.setBorder(border);
prizePanel.setOpaque(true);
prizePanel.setBackground(Color.LIGHT_GRAY);
prizePanel.setBounds(setBoundsX, setBoundsY, 120, 60);
prizeTextBox = new JTextField(50);
prizeTextBox.setBounds(setBoundsX + 5, setBoundsY + 20, 110, 30);
prizeTextBox.setFont(new Font("Arial", Font.BOLD, 12));
prizeTextBox.setOpaque(true);
prizeTextBox.setBackground(Color.WHITE);
prizeTextBox.setForeground(Color.BLACK);
prizeTextBox.setText("No Prize");
prizeTextBox.setHorizontalAlignment(JTextField.CENTER);
lp.add(prizePanel);
lp.add(prizeTextBox);
return prizeTextBox;
}
code that creates multiple text boxes bases on input from user.
JButton numberOfBallonsButton = new JButton("Set");
numberOfBallonsButton.setBounds(360,160,95, 0x1e);
numberOfBallonsButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
numberOfBallons = Integer.parseInt(numberOfBallonsTextBox.getText());
System.out.println(numberOfBallons);
lp.remove(numberOfBallonsButton);
for(int i = 0; i < numberOfBallons; i++ ){
createPrizePanels();
setBoundsX = setBoundsX +125;
if(setBoundsX > 450){
setBoundsX = 120;
setBoundsY = setBoundsY + 65;
}
}
lp.add(startGameButton);
}
});
Up to this point it works fine. However, the user needs to enter text into each text box and press another button. I'm not sure how to access each text field. Thanks in advance.
You are using local fields.you can create global field and the return value of createPrizePanels() is never used and for your case you don't know number of created textfield so you can use List as global field and when you create textfield add it to the list so you access it later from anywhere
I 'm trying to wrap a JTextPane with a JScrollPane, and I need the background to be transparent.
Here's my code:
public CharPanel(){
super.setLayout(null);
JButton LButton = new JButton("<");
LButton.setBounds(0, 300, 50, 50);
super.add(LButton);
JButton RButton = new JButton(">");
RButton.setBounds(950, 300, 50, 50);
super.add(RButton);
JTextPane Bio = new JTextPane();
Bio.setBackground(new Color(0, 0, 0, 0));
JScrollPane BioScroll = new JScrollPane(Bio);
BioScroll.setBackground(new Color(0, 0, 0, 0));
BioScroll.setBounds(0, 500, 1000, 150);
super.add(BioScroll);
}
This is what it looks like normally: https://gyazo.com/db7e4d2a5668b36ffc617cefb1423fc4
This is what it looks like after I enter a character: https://gyazo.com/1509d952005195d6e14365f0ae2da69a
See the weird visual bug?
Bio.setBackground(new Color(0, 0, 0, 0));
Don't use Colors with an alpha value. This breaks Swing painting rules and Swing don't know how to paint the component properly and you get painting artifacts.
For full transparency just use:
component.setOpaque( false );
Check out Backgrounds With Transparency for more information on this and a solution if you need to use partial transparency.
why if i have an image background i cant see all my buttons? only if my arrow go on button i see it..
this is my jlabel with background:
JLabel sfondo = new JLabel("");
sfondo.setBounds(0, 0, 832, 527);
contentPane.add(sfondo);
sfondo.setIcon(new ImageIcon(
"C:\\Users\\bocci\\workspace\\it's time of the series\\src\\immagini\\icone\\sfondo.jpg"));
sfondo.setIcon(icona3.newicona(patchsfondo, sfondo));
and i have 5 jbuttons
JButton btnOpzioni = new JButton("Opzioni");
btnOpzioni.setBounds(497, 5, 67, 23);
contentPane.add(btnOpzioni);
i see only first button.
I am trying to create a GUI for one of my programs. This is my first time using JavaFX.
For starters, I would like to have a GridPane that takes up the entire window.
I have tried everything I could find: setMinWidth/Height, setPrefSize, minWidth, etc. Nothing worked.
Following is the part of the code that defines the GridPane, with two Text objects:
//Grid layout
GridPane grid = new GridPane();
grid.setPrefSize(STAGE_WIDTH, STAGE_HEIGHT);
grid.setMaxSize(Region.USE_COMPUTED_SIZE, Region.USE_COMPUTED_SIZE);
grid.setMinSize(STAGE_WIDTH, STAGE_HEIGHT);
//grid.minWidth(STAGE_WIDTH);
//grid.minHeight(STAGE_HEIGHT);
grid.setGridLinesVisible(true);
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25, 25, 25, 25));
//Text
final Text count = new Text("Count");
grid.add(count,0,0);
final Text total = new Text("Total");
grid.add(total,1,0);
Scene scene = new Scene(grid, STAGE_WIDTH, STAGE_HEIGHT);
primaryStage.setScene(scene);
primaryStage.show();
Does anybody know how to successfully program this simple attribute? A GridPane that takes up the entire window? What am I missing?
Thank you
LD
Delete all these
grid.setPrefSize(STAGE_WIDTH, STAGE_HEIGHT);
grid.setMaxSize(Region.USE_COMPUTED_SIZE, Region.USE_COMPUTED_SIZE);
grid.setMinSize(STAGE_WIDTH, STAGE_HEIGHT);
grid.minWidth(STAGE_WIDTH);
grid.minHeight(STAGE_HEIGHT);
and use ColumnConstraints with optionally RowConstraints:
ColumnConstraints cc = new ColumnConstraints(100, 100, Double.MAX_VALUE,
Priority.ALWAYS, HPos.CENTER, true);
grid.getColumnConstraints().addAll(cc, cc);
RowConstraints rc = new RowConstraints(20, 20, Double.MAX_VALUE,
Priority.ALWAYS, VPos.CENTER, true);
grid.getRowConstraints().addAll(rc, rc);
You will get more control on each cell in this way, by adding individual column and row constraints for it. Please refer to javadocs for more detailed explanations.