image location iside Borderpane in javafx - java

Is there a way I can locate the moon photo inside the Border Pane so that it is located in the center rather than outside using javafx. below is a picture + code.
(By the way, the reason I chose to put the image of the moon inside a label is so that I can update the photos else where in the code)
Code:
BorderPane Moonpane = new BorderPane();
Moonpane.setId("moonpane");
Moonpane.setPadding(new Insets(10, 0, 10, 10));
Moonpane.setPrefSize(290,70);
Moonpane.setMaxSize(290,70);
Moonpane.setMinSize(290,70);
ImageView Moon_img = new ImageView(new Image(getClass().getResourceAsStream("/Images/Moon/100%.png")));
Moon_img.setFitWidth(100);
Moon_img.setFitHeight(100);
Moon_img.setPreserveRatio(true);
Moon_img.setSmooth(true);
Moon_Image_Label.setGraphic(Moon_img);
Moonpane.setRight(Moon_Image_Label);
Moon_Date_Label.setId("moon-text-english");
Moonpane.setLeft(Moon_Date_Label);

Et voila and finally, I used Gridpane instead as per code below
public GridPane moonpane() {
GridPane Moonpane = new GridPane();
Moonpane.setId("moonpane");
Moonpane.getColumnConstraints().setAll(
ColumnConstraintsBuilder.create().prefWidth(160).minWidth(160).build(),
ColumnConstraintsBuilder.create().prefWidth(100).minWidth(100).build()
);
Moonpane.setHgap(10);
Moonpane.setMaxHeight(50);
ImageView Moon_img = new ImageView(new Image(getClass().getResourceAsStream("/Images/Moon/100%.png")));
Moon_img.setFitWidth(100);
Moon_img.setFitHeight(100);
Moon_img.setPreserveRatio(true);
Moon_img.setSmooth(true);
Moon_Image_Label.setGraphic(Moon_img);
Moonpane.setConstraints(Moon_Image_Label, 1, 0);
Moonpane.getChildren().add(Moon_Image_Label);
Moon_Date_Label.setId("moon-text-english");
Moonpane.setConstraints(Moon_Date_Label, 0, 0);
Moonpane.getChildren().add(Moon_Date_Label);
Reflection r = new Reflection();
r.setFraction(0.15f);
Moonpane.setEffect(r);
Moonpane.setGridLinesVisible(true);
return Moonpane;
}

Related

How to make a Label's font size be as big as it can and also be able to resize itself? JavaFX [duplicate]

Is there a way to remove the default space (padding/margin) that JavaFX label adds? I want to get rid of the space displayed between the black lines on the image below:
Source code:
public class LabelTest extends Application
{
#Override
public void start(final Stage primaryStage)
{
final Group root = new Group();
final Scene scene = new Scene(root, 300, 130, Color.WHITE);
final GridPane gridpane = new GridPane();
gridpane.setPadding(new Insets(5));
gridpane.setHgap(10);
gridpane.setVgap(10);
final Label label = new Label("Label");
label.setStyle("-fx-font-size:44px;-fx-font-weight: bold;-fx-text-fill:#5E34B1;-fx-background-color:#ffc300;");
GridPane.setHalignment(label, HPos.CENTER);
gridpane.add(label, 0, 0);
root.getChildren().add(gridpane);
primaryStage.setScene(scene);
primaryStage.show();
}
}
One of the more dynamic ways to do this is to use a Text instead of a Label and set the boundsType as VISUAL. This results in a Text without any padding on the any of the sides of the Text, irrespective of the font size.
Text text = new Text();
text.setBoundsType(TextBoundsType.VISUAL);
You can achieve that by adding -fx-padding: -10 0 0 0; to the list of your styles.
For more flexible solution you can use FontMetrics information:
FontMetrics metrics = Toolkit.getToolkit().getFontLoader().getFontMetrics(label.getFont());
label.setPadding(new Insets(-metrics.getDescent(), 0, 0, 0));
NB: You need to call that code after scene.show(). Before that graphics engine is not ready to provide correct metrics.
For me it was easiest to just use setPadding.
label.setPadding(new Insets(-2,0,0,0)); //top, right, bottom, left
This way I did not have to deal with the css-style sheet.
For more details see my post here Substructure styling
You could also do it like this after the stage.show().
With the example of an separator:
Separator separator = new Separator();
separator.setStyle(""
+ "-fx-border-width: 1px;"
+ "-fx-border-color: black;"
+ "-fx-padding: 0px;"
+ "");
stage.show()
Do
Node line = separator.lookup(".line");
line.setStyle(""
+ "-fx-border-insets: 0px;"
+ "-fx-border-width: 0px;"
+ "");
or this way index 0 because it has only one element at index 0 which is an Region with style calss .line
separator.getChildrenUnmodifiable().get(0).setStyle(""
+ "-fx-border-insets: 0px;"
+ "-fx-border-width: 0px;"
+ "");

Android programmatically setting up layout for different screen sizes/densities

I am currently creating an Android app that I want to support multiple screen sizes/densities. When I set up layouts in xml, everything looks fine across different screen sizes. However, there are rows I need to add programatically.
Whenever I add the rows, I can't seem to get things to look consistent across different devices. I believe using the dp unit when settings up heights, widths in xml, along with wrap_content and match_parent when appropriate, have allowed things to easily translate between different devices.
Programatically, when I try to set a layout height/width, I have been trying to convert the anticipated dp value to a pixel value. I've done so using this:
public static int convertToPixels(int value) {
Resources resources = mContext.getResources();
int x = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,value,resources.getDisplayMetrics());
return x;
}
Overall, the heights look ok, but the widths don't look good. For instance, the width of the row will look such that on a smaller device, the information neatly displays across the row, which is what I want. However,when I try to run the app on a tablet, for instance, the information will only stretch to half of the row, which doesn't look good. I want the sizes to scale and neatly display just like on the smaller device.
If anyone has any idea what my problem might be, I would greatly appreciate it. Below is the source for adding a row using java:
LinearLayout row = new LinearLayout(mContext);
row.setOrientation(LinearLayout.HORIZONTAL);
row.setId(Integer.parseInt(txn.getId().toString()));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertToPixels(60));
params.setMargins(0, convertToPixels(1), 0, 0);
row.setLayoutParams(params);
row.setBackgroundColor(mContext.getResources().getColor(R.color.white));
LinearLayout imageLayout = new LinearLayout(mContext);
LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams(convertToPixels(40), convertToPixels(40));
imageParams.gravity = Gravity.CENTER;
imageLayout.setLayoutParams(imageParams);
ImageView image = new ImageView(mContext);
if (txn.getTransactionStateID() == Constants.TXN_STATUS_OK) {
image.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ok));
} else if (txn.getTransactionStateID() == Constants.TXN_STATUS_SUSPICIOUS) {
image.setImageDrawable(mContext.getResources().getDrawable(R.drawable.alert));
} else if (txn.getTransactionStateID() == Constants.TXN_STATUS_RED_FLAG) {
image.setImageDrawable(mContext.getResources().getDrawable(R.drawable.flag));
}
imageLayout.addView(image);
row.addView(imageLayout);
LinearLayout txnMiddleLayout = new LinearLayout(mContext);
txnMiddleLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams txnTopParams = new LinearLayout.LayoutParams(convertToPixels(400), convertToPixels(60));
txnTopParams.setMargins(convertToPixels(10), 0, 0, 0);
txnMiddleLayout.setLayoutParams(txnTopParams);
TextView txnTopContents = new TextView(mContext);
txnTopContents.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertToPixels(30)));
txnTopContents.setText(txn.getTopLineContents());
txnTopContents.setTextColor(Color.BLACK);
// txnTopContents.setTextSize(convertToPixels(16));
TextView txnBottomContents = new TextView(mContext);
txnBottomContents.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertToPixels(30)));
txnBottomContents.setText(txn.getBottomLineContents());
// txnBottomContents.setTextSize(convertToPixels(12));
txnMiddleLayout.addView(txnTopContents);
txnMiddleLayout.addView(txnBottomContents);
row.addView(txnMiddleLayout);
LinearLayout txnBottomLayout = new LinearLayout(mContext);
txnBottomLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams txnBottomParams = new LinearLayout.LayoutParams(convertToPixels(120), convertToPixels(60));
txnBottomLayout.setLayoutParams(txnBottomParams);
TextView amount = new TextView(mContext);
amount.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertToPixels(30)));
amount.setText(txn.getAmountStr());
amount.setTextColor(Color.BLACK);
// amount.setTextSize(convertToPixels(16));
TextView date = new TextView(mContext);
date.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertToPixels(30)));
date.setText(txn.getDateStr());
// date.setTextSize(convertToPixels(12));
txnBottomLayout.addView(amount);
txnBottomLayout.addView(date);
row.addView(txnBottomLayout);
txnList.addView(row);
I eventually found a solution. Instead of trying to set an exact width value, I set the width of the layout or textview to 0, and used layout_weight instead.
https://stackoverflow.com/a/3996104/4056947

Problems to align a Javafx button

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);

JavaFX - Inflating a GridPane to parent's size

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.

how to make JTable display all records form database

My problem is when i run the program the data displayed at the JTable but not all records are displayed although i have a scroll Pane and aJtable inside it,when i run it provides a scroll but for the firs few records ,the jtable have a textfield to provide searching in table using rowSorter ,although jtable doesn't show all records when i search for a record that is not viewed i gets it.this is an image describing the problem.please how can i fix that.
this is the link i have no permission to attache an image here
http://www.megafileupload.com/en/file/561971/Untitled-jpg.html
this is piece of code where i initialize these component
table2 = new JTable(tableModel);
table2.setFont(new Font("Tahoma", Font.PLAIN, 16));
table2.getTableHeader().setFont(new Font("Tahoma", Font.BOLD, 14));
scrollPane_1.setColumnHeaderView(table2.getTableHeader());
table2.setBackground(new Color(210, 180, 140));
table2.setBorder(new LineBorder(new Color(105, 105, 105)));
table2.setForeground(new Color(0, 0, 0));
sorter2 = new TableRowSorter<TableModel>(table2.getModel());
table2.setRowSorter(sorter2);
JViewport vp2 = new JViewport();
vp2.setBackground(new Color(210, 105, 30));
vp2.setForeground(new Color(0, 0, 0));
scrollPane_1.setViewportView(vp2);
vp2.add(table2);
this is the method that fill the table
table2Data = new Vector<>();
Iterator<SellBean> buyIt = boughtedList.iterator();
while (buyIt.hasNext()) {
SellBean buyBean = buyIt.next();
Vector rec = new Vector<>();
rec.add(buyBean.getPRODUCT_ID());
rec.add(buyBean.getPRODUCT_NAME());
// System.out.println(buyBean.getPRODUCT_NAME());
rec.add(buyBean.getQUANTITY());
rec.add(buyBean.getBUY_PRICE());
rec.add(buyBean.getSELL_PRICE());
rec.add(buyBean.getPROFIT());
rec.add(buyBean.getBUY_DATE());
rec.add(buyBean.getRETURNED());
table2Data.add(rec);
}
tm2.setDataVector(table2Data, table2ColNames);
for (int i = 0; i < table2.getColumnCount(); i++) {
DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
centerRenderer.setHorizontalAlignment(JLabel.CENTER);
table2.getColumnModel().getColumn(i)
.setCellRenderer(centerRenderer);
}
for (int i = 0; i < table2.getRowCount(); i++) {
table2.setRowHeight(i, 28);
}
table2.getColumnModel().getColumn(0).setPreferredWidth(25);
}
i hope it is clear now
Maybe you have problems with the scrollpane (like: no scrollbars visible).
Are your sure you are using it correct?
this looks strange to me:
You create a ViewPort and put this as View?!
JViewport vp2 = new JViewport();
scrollPane_1.setViewportView(vp2);
vp2.add(table2);
why not just set the Table as ViewportView (as the name implies?)
just:
scrollPane_1.setViewportView(table2);
Edit: Info about the ViewPort:
The "viewport" or "porthole" through which you see the underlying information. When you scroll, what moves is the viewport. It is like peering through a camera's viewfinder. Moving the viewfinder upwards brings new things into view at the top of the picture and loses things that were at the bottom.
http://docs.oracle.com/javase/7/docs/api/javax/swing/JViewport.html

Categories

Resources