what is the difference between setprefwidth() and setmaxwidth()? - java

What is the basic difference ?
When I do like this
Button button = new Button();
button.setPrefWidth(50);
button.setMaxWidth(Double.MAX_VALUE);
any one will think that the button will be shown with width 50 and can be expandable up to infinite , but what,s happening is the button occupies the total width of the screen .
Does any one know what's their intention?
my border pane code :
borderPane.setPadding(new Insets(10,10,10,10));
borderPane.setCenter(button);
borderPane.getStyleClass().add("bpane");
//Region region = (Region) anyNode;
button.setStyle("-fx-background-color: red;");

Related

Buttons of the same size in tilepane javafx

Hi guys I m completely new with GUIs... so far all of my programs where text based. I ve just started and im using JAVAFX.
I wanted to recreate a telephone keypad. It should have 4 rows of 3 buttons each and each button should have its related number along with its letters. And it does. But the size of the buttons is not the same.
I mean… since i'm using a tilePane the buttons occupy the same amount of pixels (or at least I guess it is like that), but the actual an visible size of the buttons is different because every button takes only the size that it needs in order to display its contents. I stored the buttons in an array. Is there a way to make them all of the same size of the biggest one?
public class PhoneKeyboard extends Application
{
Button buttons [];
#Override
public void start(Stage stage) throws Exception
{
// Create the set of necessary buttons.
buttons = new Button[12];
fillButtons(buttons);
//Create the grid for the buttons.
TilePane pane = new TilePane();
pane.setPadding(new Insets(10,10,10,10));
pane.setPrefColumns(3);
pane.setMaxWidth(Region.USE_PREF_SIZE);// Use the pref size as max size to make sure there will be the expected size
pane.setVgap(5.0);// to set ome spacing between each tile of the pane.
pane.setHgap(5.0);
//Put the buttons on the pane (layout);
pane.getChildren().addAll(buttons);
// JavaFX must have a Scene (window content) inside a Stage (window)
Scene scene = new Scene(new StackPane(pane), 300,400);// In order to use the pfer size of the pane, the tile Pane doesn"t have to be the root in the scene. Therefore we created a scene with a stack pane containing our tile pane as the root.
stage.setTitle("Keyboard");
stage.setScene(scene);
// Show the Stage (window)
stage.show();
}
A Button has a maximum width and maximum height which are just big enough to hold the Button’s content. Most layouts, including TilePane, won’t make child nodes larger than their maximum size.
If you want each button to be capable of being made larger, set its maximum width and height, after calling fillButtons:
for (Button button : buttons) {
button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
}

How to fix right button border not showing in app?

I am trying to make a game with multiple levels. My plan is to print 15 levels on 1 page. I have attached a photo of the simple design I am currently using.
As you can see in the picture the right border of a button is not showing. For the parent I am using this java code:
LinearLayout horl = new LinearLayout(this);
horl.setId(worldVar);
horl.setOrientation(LinearLayout.HORIZONTAL);
horl.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
And to set 3 buttons horizontally in this LinearLayout I use this code for the buttons. This code just has a simple for loop around to put 3 buttons in this LinearLayout:
Button button = new Button(this);
LinearLayout.LayoutParams params = new LinearLayout
.LayoutParams(getMarginInDp(100), getMarginInDp(100));
params.setMargins(getMarginInDp(1), getMarginInDp(25), getMarginInDp(1), 0);
button.setLayoutParams(params);
GradientDrawable gd = new GradientDrawable();
gd.setCornerRadius(5);
gd.setStroke(1, 0xFF000000);horl.addView(button);
The function getMarginInDp looks like this:
public int getMarginInDp(int dp){
return (int) TypedValue
.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
}
Does anybody have any idea how to make the border appear because I want to keep the buttons approximately this size and I may even slightly increase the margins?
You can set the weight to 0.33f for each button:
Button button = new Button(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(getMarginInDp(100), getMarginInDp(100), 0.33f);
so they are distributed equally in the width of the layout.

Display images dynamically in a ScrollPane

I want to add multiple images in a ScrollPane when clicking a button. Now when I click the button a new rectangle is created and fitted with a new image.
The problem is the scrollbar is not displaying when a new image is added.
Here is the code:
#FXML
public void handleButtonPause(){
Rectangle r = new Rectangle();
if(counterIm==0){
//Ap1.setMinSize(Sp1.getWidth(), Sp1.getHeight() - 10);
r.setY(Sp1.getHeight() *((double)1/36));
r.setWidth(Sp1.getWidth()*0.75);
rectWidth=r.getWidth();
r.setHeight(Sp1.getHeight()/6);
rectHeight=r.getHeight();
}
else {
r.setY(Sp1.getHeight()*((7*counterIm+1)/36));
r.setWidth(rectWidth);
r.setHeight(rectHeight);
}
r.setX(Sp1.getWidth() / 10);
r.setArcWidth(20);
r.setArcHeight(20);
Ap1.getChildren().add(r);
Image image = new Image(new File("C:\\Users\\Manuel\\Desktop\\error.png").toURI().toString());
r.setFill(new ImagePattern(image));
Sp1.setVvalue(1);
counterIm=counterIm+1;
I don't know how your composition looks like.
A good approach could be to put the images into a VBox and put the VBox into the ScrollPane.
And check if you have set the VBar Policy of your SrollPane:
srollbar.setVbarPolicy(ScrollBarPolicy.AS_NEEDED);
You can also set the maximal height of the VBox to double maximum to make it sure it will always grow vertically:
vbox.setMaxHeight(Double.MAX_VALUE);

Android : Unable to change width of dynamic button

I am trying to make dynamic buttons of fixed dimensions. I am able to alter the height but unable to change the width. The width of the button seems to MATCH_PARENT (it takes up whole screen width. If there are two buttons, each button width is half screen width and when there is only one button, button width is screen width).
TableLayout table = (TableLayout)findViewById(r.id.table);
TableRow tableRow = new TableRow(this);
table.addView(tableRow);
Button button = new Button(this);
button.setLayoutParams(new TableRow.LayoutParams(30,30));
tableRow.addView(button);
Could anybody point out where I am going wrong.
Only This You have to do to change Button Dynamically
First I get Button
Button btn = (Button) findViewById(R.id.button1);
Then Button Click event I put This Code Only
TableRow.LayoutParams lp = new TableRow.LayoutParams(70, 70);
btn.setLayoutParams(lp);
And Works Fine. Try It. Will solve your Problem
Instated "button.setLayoutParams(new TableRow.LayoutParams(30,30));" change to following code change the button Height and Width.
LayoutParams lp = new LayoutParams(40, 40);
table.addView(button, lp);

WHEN to programmatically resize a button created programmatically?

I am creating some buttons in a tablelayout. I am creating the buttons programmatically.
For example the following code is in the "onresume" method...
TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams rowParams = new TableRow.LayoutParams(0,android.widget.TableRow.LayoutParams.MATCH_PARENT,3f);
TableLayout tableLayout = (TableLayout) findViewById(R.id.sometablelayoutivealreadydefinedinxml);
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(tableParams);
Button btnOne = new Button(this);
btnOne.setId(100);
btnOne.setLayoutParams(rowParams);
btnTwo.setId(200);
btnTwo.setLayoutParams(rowParams);
tableRow.addView(btnOne);
tableRow.addView(btnTwo);
tableLayout.addView(tableRow);
I want to set the height of the button to be the SAME as the width. The width is determined by the size of the screen. The problem as I understand it,is that until the buttons are actually drawn to the screen they don't exist so any width returned will be zero. So I tried the following code.
Button yourBtn = (Button)findViewById(100);
int btnSize;
btnSize = yourBtn.getWidth();
yourBtn.setLayoutParams(new TableRow.LayoutParams(btnSize, btnSize));
Now the problem is that if I run this code immediately after creating the buttons the width of the button is returned as 0 as the button hasn't been drawn yet. However, if I assign the code to a test button then it resize the button as I want it to. However, I don't want the user to have to press a button after the screen has loaded to resize the buttons properly.
So, the question is when is the appropriate time to call this to resize the buttons properly when the activity is created but after the buttons have been drawn and how do I do this?

Categories

Resources