adding images to a gridpane javafx - image

I'm adding a list of images from a directory using an arraylist.When images are added,my ScrollPane gets crowded.How can I keep spacings between images ?
here's my code
File file = new File("D:\\SERVER\\Server Content\\Apps\\icons");
File[] filelist1 = file.listFiles();
ArrayList<File> filelist2 = new ArrayList<>();
hb = new HBox();
for (File file1 : filelist1) {
filelist2.add(file1);
}
System.out.println(filelist2.size());
gridpane.setPadding(new Insets(50,50,50,50));
gridpane.setHgap(20);
gridpane.setVgap(20);
int imageCol = 0;
int imageRow = 0;
for (int i = 0; i < filelist2.size(); i++) {
System.out.println(filelist2.get(i).getName());
image = new Image(filelist2.get(i).toURI().toString());
pic = new ImageView();
pic.setFitWidth(130);
pic.setFitHeight(130);
pic.setImage(image);
hb.getChildren().add(pic);
gridpane.add(pic, imageCol, imageRow );
imageCol++;
// To check if all the 4 images of a row are completed
if(imageCol > 2){
// Reset Column
imageCol=0;
// Next Row
imageRow++;
}

Try using HBox and VBox.
Basically, they are like little containers where you store your stuff and you can add gaps into it!
HBox ab = new HBox(10); <--The 10 is adding space (Answer to your question)
If you want to add stuff into HBox, simply write
ab.getChildren().addAll(your content here);

Related

Java-FX How to set image in a GridPane on click - game Othello

Hi i'm trying to click on a position (from 0 to 7) in my GridPane.
I would set an image inside it. I tryed everything but i can't see any improvement...
This is my board
Here my code on click on grid
#FXML
private void clickGrid(MouseEvent event) {
myGrid = new GridPane();
black = new Image("othello/images/black.png");
white = new Image("othello/images/white.png");
empty = new Image("othello/images/empty.png");
Node source = (Node)event.getSource() ;
Integer colIndex = GridPane.getColumnIndex(source);
Integer rowIndex = GridPane.getRowIndex(source);
System.out.printf("Mouse clicked cell [%d, %d]%n", colIndex.intValue(), rowIndex.intValue());
myGrid.add(new ImageView(white), colIndex, rowIndex);
}
Here my code when i click restart
#FXML
private void restartGame(ActionEvent event)throws Exception{
myGrid = new GridPane();
black = new Image("othello/images/black.png");
white = new Image("othello/images/white.png");
empty = new Image("othello/images/empty.png");
for (int i = 0; i < 8; i++){ //Per righe
for (int j = 0; j < 8; j++){ // Per colonne
myGrid.add(new ImageView(empty), i, j);
}
}
myGrid.add(new ImageView(black), 3, 3);
myGrid.add(new ImageView(black), 4, 3);
myGrid.add(new ImageView(white), 4, 4);
myGrid.add(new ImageView(white), 4, 3);
}
black is my piece colored of black, for white is white.
Source path
I have main project in src of netbeans.
Inside it, i have:
- othello (it contains my main)
- othello.images (it cointains all my image also backgrounds)
- othello.view (it contains my FXML files)
- othello.model (now nothing)
- othello.controller (it contains the controllers about the fxml files)
I think you don't see new images because you add to a new Grid, not to the existent one:
myGrid = new GridPane(); // !!! here a problem
myGrid.add(new ImageView(white), colIndex, rowIndex);
Don't create a new GridPane on every click:
myGrid = new GridPane(); // delete this
delete this line, and add an image to the GridPane you've prepared in FXML

Unable to add an Image to a powerpoint presentation using open xml

I am using the following code to add a new slide to a ppt file and add an image. I am using Open XML 2.5 SDK.
A new slide is getting added but not the image. Is there anything wrong in this code?
int position = 1;
using (PresentationDocument presentationDocument = PresentationDocument.Open("c.pptx", true))
{
PresentationPart presentationPart = presentationDocument.PresentationPart;
Slide slide = new Slide(new CommonSlideData(new ShapeTree()));
NonVisualGroupShapeProperties nonVisualProperties = slide.CommonSlideData.ShapeTree.AppendChild(new NonVisualGroupShapeProperties());
nonVisualProperties.NonVisualDrawingProperties = new NonVisualDrawingProperties() { Id = 1, Name = "" };
nonVisualProperties.NonVisualGroupShapeDrawingProperties = new NonVisualGroupShapeDrawingProperties();
nonVisualProperties.ApplicationNonVisualDrawingProperties = new ApplicationNonVisualDrawingProperties();
// Specify the group shape properties of the new slide.
slide.CommonSlideData.ShapeTree.AppendChild(new GroupShapeProperties());
// Create the slide part for the new slide.
SlidePart slidePart = presentationPart.AddNewPart<SlidePart>();
// Save the new slide part.
slide.Save(slidePart);
string imgId = "rId" + new Random().Next(2000).ToString();
ImagePart imagePart = slidePart.AddImagePart(ImagePartType.Png, imgId);
using (FileStream stream = new FileStream("a.png", FileMode.Open))
{
stream.Position = 0;
imagePart.FeedData(stream);
}
slide.Save(slidePart);
// Modify the slide ID list in the presentation part.
// The slide ID list should not be null.
SlideIdList slideIdList = presentationPart.Presentation.SlideIdList;
// Find the highest slide ID in the current list.
uint maxSlideId = 1;
SlideId prevSlideId = null;
foreach (SlideId slideId in slideIdList.ChildElements)
{
if (slideId.Id > maxSlideId)
{
maxSlideId = slideId.Id;
}
position--;
if (position == 0)
{
prevSlideId = slideId;
}
}
maxSlideId++;
// Get the ID of the previous slide.
SlidePart lastSlidePart;
if (prevSlideId != null)
{
lastSlidePart = (SlidePart)presentationPart.GetPartById(prevSlideId.RelationshipId);
}
else
{
lastSlidePart = (SlidePart)presentationPart.GetPartById(((SlideId)(slideIdList.ChildElements[0])).RelationshipId);
}
// Use the same slide layout as that of the previous slide.
if (null != lastSlidePart.SlideLayoutPart)
{
slidePart.AddPart(lastSlidePart.SlideLayoutPart);
}
// Insert the new slide into the slide list after the previous slide.
SlideId newSlideId = slideIdList.InsertAfter(new SlideId(), prevSlideId);
newSlideId.Id = maxSlideId;
newSlideId.RelationshipId = presentationPart.GetIdOfPart(slidePart);
// Save the modified prsentation.
presentationPart.Presentation.Save();
Thanks in advance.

JavaFX Tree View Height According to Content

I have number of treeviews in VBox.
I want treeview to take a height based on the number of nodes visible.
Say if I collapse root node of treeview then height of that treeview should also change.
In the case of this image Item0 shows 7 subitems and then there is a lot of whitespace and then Item1 starts, Item1 is collapsed now , so Item2 should start immediately below Item1.
Please tell me what is the right way of doing this.
Try this
#Override
public void start(Stage primaryStage) {
VBox treeContainer = new VBox();
TreeItem<String> hiddenRootItem = new TreeItem<String>();
TreeView<String> tree = new TreeView<String>(hiddenRootItem);
tree.setShowRoot(false);
treeContainer.getChildren().add(tree);
for (int j = 0; j < 3; ++j) {
TreeItem<String> rootItem = new TreeItem<String>("Item " + j);
rootItem.setExpanded(true);
String[] names = {"SubItem1", "SubItem2", "SubItem3", "SubItem4", "SubItem5", "SubItem6", "SubItem7",};
for (int i = 0; i < names.length; i++) {
TreeItem<String> item = new TreeItem<String>(names[i]);
rootItem.getChildren().add(item);
}
hiddenRootItem.getChildren().add(rootItem);
}
StackPane root = new StackPane();
root.getChildren().add(treeContainer);
Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}

How to create grid border?

This is what I'm trying to create. White areas are System.Windows.Shapes.Rectangle in a Grid. This is my code for creating grid, columns, rows and rectangles;
Grid newGrid = new Grid();
for(int r=0; r<10; r++ ) {
newGrid.RowDefinitions.Add(
new RowDefinition { Height = new GridLength(30) });
for( int c=0; c<10; c++ ) {
newGrid.ColumnDefinitions.Add(
new ColumnDefinition { Width = new GridLength(30) });
Rectangle rec = new Rectangle{
Fill = new SolidColorBrush(Colors.White)
};
Grid.SetColumn(rec, c);
Grid.SetRow(rec, r);
newGrid.Children.Add(rec);
}
}
LayoutRoot.Children.Add(newGrid);
But I have not any idea how can add borders as we can see in picture. Thanks for suggestions.
Try
SolidColorBrush blackBrush = new SolidColorBrush();
blackBrush.Color = Colors.Black;
rec.Stroke = blackBrush;

How to create PowePoint presentation in OpenXML format with Apache Poi and XSLF?

If I go to Apache POI XSLF there should be samples for both OLE2 and OpenXML specs, but there are only the OLE2 based Horrible Slide Layout Format examples.
Could please anybody help me out with XML Slide Layout Format example ? The API is quite different.
It is not like with spreadsheet where one just change the implementation of HSSFWorkbook to XSSFWorkbook.
How would this look like with XSLF implementation ? POI apparently can't create a document from scratch, so we need an existing empty dummy document, right ?
//table data
String[][] data = {
{"INPUT FILE", "NUMBER OF RECORDS"},
{"Item File", "11,559"},
{"Vendor File", "300"},
{"Purchase History File", "10,000"},
{"Total # of requisitions", "10,200,038"}
};
SlideShow ppt = new SlideShow();
Slide slide = ppt.createSlide();
//create a table of 5 rows and 2 columns
Table table = new Table(5, 2);
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
TableCell cell = table.getCell(i, j);
cell.setText(data[i][j]);
RichTextRun rt = cell.getTextRun().getRichTextRuns()[0];
rt.setFontName("Arial");
rt.setFontSize(10);
cell.setVerticalAlignment(TextBox.AnchorMiddle);
cell.setHorizontalAlignment(TextBox.AlignCenter);
}
}
//set table borders
Line border = table.createBorder();
border.setLineColor(Color.black);
border.setLineWidth(1.0);
table.setAllBorders(border);
//set width of the 1st column
table.setColumnWidth(0, 300);
//set width of the 2nd column
table.setColumnWidth(1, 150);
slide.addShape(table);
table.moveTo(100, 100);
FileOutputStream out = new FileOutputStream(file);
ppt.write(out);
out.close();
It is not implemented yet, org.apache.poi version 3.8-beta3, when it will be implemented is very unknown to me.
XMLSlideShow.java
public MasterSheet createMasterSheet() throws IOException {
throw new IllegalStateException("Not implemented yet!");
}
public Slide createSlide() throws IOException {
throw new IllegalStateException("Not implemented yet!");
}

Resources