how to use zk hbox array? - java

I am not sure how to use a zk Hbox Array. I am trying to create an array of ZK Hbox components and use it within a for block.
void createRow(Component container, final Node fieldNode, FieldCreator [] fieldDescription) {
final Vbox fieldsRows = new Vbox();
final Hbox fieldsRow = new Hbox();
final Hbox[] fieldBox;
int i=0;
for (FieldCreator fieldDesc : fieldDescription) {
fieldBox[i] = new Hbox();
fieldDesc.createField(fieldNode, fieldBox[i]);
i++;
}
fieldsRow.appendChild(fieldBox[]);
Button removeFieldButton = new Button(Messages.getString("MXFC_removeFieldButton")); //$NON-NLS-1$
fieldsRow.appendChild(removeFieldButton);
fieldsRows.appendChild(fieldsRow);
removeFieldButton.addEventListener(Events.ON_CLICK, new EventListener() {
public void onEvent(Event event) throws Exception {
fieldNode.getParentNode().removeChild(fieldNode);
fieldBox[].setParent(null);
}
});
container.appendChild(fieldsRows);
}
The code above is incorrect. The compiler throws the error: "Syntax error on token "[", Expression expected after this token." on lines :
fieldsRow.appendChild(fieldBox[]);
fieldBox[].setParent(null);
How do I fix this?
Thanks,
Sony

Sony,
There are a few syntax errors in your java code.
fieldBox[] does not mean anything here in Java.
You need to initialize fieldBox before you can assign value to its entries.
To fix these problems we have to understand what you want to achieve in this piece of code. Based on my guess, you should
initialize fieldBox.
Hbox[] fieldBox = new Hbox[fieldDescription.length];
iterate through columns as you append/detach children of the row.
for(int i=0; i<fieldBox.length; i++) fieldsRow.appendChild(fieldBox[i]);
for(int i=0; i<fieldBox.length; i++) fieldBox[i].setParent(null);

Related

print text from textArea useing javaFX Printer

I am try to print text from text Area using JavaFX Printer(Like in notepad printing feature). I want to print that text without textArea background styles. So I try it with another text area. I wrap its text for avoid x bar overflow. But, I can't find a way to avoid y bar overflow. also I can't hide the background. can some one help or give a subjections for this or any other way. This is my current code. Also I try to hide scroll bars using css. but it did't support. I want to print only the text.
TextArea txtArea = new TextArea("This is some long text..");
public void printText() {
PrinterJob job = PrinterJob.createPrinterJob();
if(job == null) {
System.out.println("Error");
return;
}
boolean proseed = job.showPrintDialog(root.getScene().getWindow());
JobSettings ss1 = job.getJobSettings();
PageLayout pageLayout1 = ss1.getPageLayout();
double pgW1 = pageLayout1.getPrintableWidth();
double pgH1 = pageLayout1.getPrintableHeight();
TextArea tempTxtArea = new TextArea(txtArea.getText());
tempTxtArea.setPrefSize(pgW1, pgH1);
tempTxtArea.setWrapText(true);
tempTxtArea.setId("tempScroolBar");
if(proseed) {
boolean printed = job.printPage(tempTxtArea);
if (printed) {
job.endJob();
} else {
System.out.println("Fail");
}
}
}
this is my css file
#tempScroolBar > .scroll-pane {
-fx-vbar-policy: never;
-fx-hbar-policy: never;
}
Update
I updated my code. But now I have another problem. I changed TextArea to Label and to print all the content(avoid y bar overflow) I want to get number of the pages. But it doesn't return height(always return 0). Then I refer https://stackoverflow.com/a/21075734/13862869 and I set It to the Scene. Without add the label to the new Scene it doesn't give the width. But when I set the label to the Scene only three '.' are print in my document. Can some one help me to solve this...
public void printText() {
PrinterJob job = PrinterJob.createPrinterJob();
if(job == null) {
System.out.println("Error");
return;
}
boolean proseed = job.showPrintDialog(root.getScene().getWindow());
JobSettings ss1 = job.getJobSettings();
PageLayout pageLayout1 = ss1.getPageLayout();
double pgW1 = pageLayout1.getPrintableWidth();
double pgH1 = pageLayout1.getPrintableHeight();
HBox h = new HBox();
Label tempText = new Label();
tempText.setPrefWidth(pgW1);
tempText.setWrapText(true);
tempText.setText(txtArea.getText());
h.getChildren().add(tempText);
Scene s = new Scene(h); // when i remove this line again text can print
tempText.applyCss();
double fullLabelHeight = tempText.prefHeight(-1);
int numberOfPages = (int) Math.ceil(fullLabelHeight/ pgH1);
if(proseed) {
job.printPage(tempText);
job.endJob();
}
}

Remove FixedLeading at the first line on each page

I want to remove setFixedLeading at the first line on each page (100+)
I read a bit text(more 100 page with help while). And I set padding and margin to 0 but I still have top indent. Why? Help me pls? How delete it?
public static final String DEST = "PDF.pdf";
public static void main(String[] args) throws FileNotFoundException {
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
Document doc = new Document(pdfDoc);
doc.setMargins(0,0,0,0);
for (int i = 0; i <20 ; i++) {
Paragraph element = new Paragraph("p " + i);
element.setPadding(0);
element.setMargin(0);
element.setFixedLeading(55);
doc.add(element);
}
doc.close();
}
PDF file:
https://pdfhost.io/v/Byt9LHJcy_PDFpdf.pdf
At the time of element creation you don't know the page it will end up on nor its resultant position. I don't think there is a property that allows you to configure the behavior depending on whether it's the top element on a page (such property would be too custom and tied to a specific workflow).
Fortunately, the layout mechanism is quite flexible and you can implement the desired behavior in a couple of lines of code.
First off, let's not use setFixedLeading and set the top margin for all paragraphs instead:
Document doc = new Document(pdfDocument);
doc.setMargins(0, 0, 0, 0);
for (int i = 0; i < 20; i++) {
Paragraph element = new Paragraph("p " + i);
element.setPadding(0);
element.setMargin(0);
element.setMarginTop(50);
doc.add(element);
}
doc.close();
This does not pretty much change anything in the visual result - it's just another way of doing things.
Now, we need a custom renderer to tweak the behavior of a paragraph if it is rendered at the top of the page. We are going to override layout method and check if the area we are given is located at the top of the page - and if so, we will not apply the top margin:
private static class CustomParagraphRenderer extends ParagraphRenderer {
Document document;
public CustomParagraphRenderer(Paragraph modelElement, Document document) {
super(modelElement);
this.document = document;
}
#Override
public IRenderer getNextRenderer() {
return new ParagraphRenderer((Paragraph) modelElement);
}
#Override
public LayoutResult layout(LayoutContext layoutContext) {
if (layoutContext.getArea().getBBox().getTop() == document.getPdfDocument().getDefaultPageSize().getHeight()) {
((Paragraph)getModelElement()).setMarginTop(0);
}
return super.layout(layoutContext);
}
}
Now the only thing we need to do is to set the custom renderer instance to each paragraph in the loop:
element.setNextRenderer(new CustomParagraphRenderer(element, doc));
Visual result:

Blend Mode doesn't work for ImageView

I have two image that is
and this:
I need to place that image (stacked) in a single ImageView. I tried to use blend mode, but doesn't work for ImageView like
Group group = new Group();
group.setBlendMode(BlendMode.SRC_OVER);
// tempImage is array of buffered Images
for(int i=0; i < tempImage.length ;i++){
if(tempImage[i] != null){
ImageView view = new ImageView();
Image im = SwingFXUtils.toFXImage(tempImage[i],
null );
view.setImage(im);
group.getChildren().add(view);
}
}
Just a little trick, instead of using BlendModeuse a HBox, add the images inside the HBox and set the HBox inside the Group
Group group = new Group();
HBox box = new HBox
// tempImage is array of buffered Images
for(int i=0; i < tempImage.length ;i++){
if(tempImage[i] != null){
ImageView view = new ImageView();
Image im = SwingFXUtils.toFXImage(tempImage[i],
null );
view.setImage(im);
box.getChildren().add(view);
}
}
group.getChildren.add(box);
But this wont help you to get the new image, guess you don't even need it !

define multiple labels in javafx at once

I would like to declare multiple labels in javafx all at once, is this possible?
at the moment this is what i type, and i have line 20 to 30 labels!!
fajr_Label_ar = new Label();
fajr_Label_eng = new Label();
zuhr_Label_ar = new Label();
zuhr_Label_eng = new Label();
asr_Label_ar = new Label();
asr_Label_eng = new Label();
maghrib_Label_ar = new Label();
maghrib_Label_eng = new Label();
isha_Label_ar = new Label();
isha_Label_eng = new Label();
hadith_Label = new Label();
fajr_hourLeft = new Label();
Can I declare something along the line of;
label1, label2, label3.... = new Label();
This is not about JavaFX. This question is about Java in general ..
I see some localizations there : en,ar ... so I would suggest this pattern:
Label lbs[];
lbs=new Label[7];
String strings_en[]=new String[]{
"fajr",
"zuhr",
"asr",
"maghrib",
"isha",
"hadith",
"fajr_lefthour"
};
String strings_ar[]=new String[]{
"فجر",
"ظهر",
"عصر",
"مغرب",
"عشاء",
"حديث",
"فجر ساعة لاحقة"
};
String strings[];
//assing lan list
strings=strings_ar; // or strings=strings_en;
for (int i = 0; i < lbs.length; i++) {
lbs[i]=new Label(strings[i]);//initializing labels
//doing what you want here with labels
//...
}
It would be better to isolate data from design as you can see we have labels as Label array and data as String array.. and it would be better and better to define the lang set before retrieving data as we have done when assign object array strings to object array Ar or en .. that would help you to extend/scale/debug your program easily and healthy by adding more labels or languages ..

How can I add a clickable URL in a JTextArea?

I am writing an application and in that I am using JTextArea to display some text. Now I want to show some clickable URL in text area along with the normal text and I want if user click on the URL then the web page that URL referring to should open in new web browser window.
Use JEditorPane with HTMLEditorKit or JTextPane and set content type to "text/html"
..url referring to should open in new web browser window.
// 1.6+
Desktop.getDesktop().browse(URI);
Here is an example of opening links from JTextArea:
JTextArea jtxa = new JTextArea(25,100);
JScrollPane jsp = new JScrollPane(jtxa);
JPanel jp = new JPanel();
jp.add(jsp);
jp.setSize(100,50);
jtxa.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent me)
{
if(me.getClickCount()==2) //making sure there was a double click
{
int x = me.getX();
int y = me.getY();
int startOffset = jtxa.viewToModel(new Point(x, y));//where on jtextarea click was made
String text = jtxa.getText();
int searchHttp = 0;
int wordEndIndex = 0;
String[] words = text.split("\\s");//spliting the text to words. link will be a single word
for(String word:words)
{
if(word.startsWith("https://") || word.startsWith("http://"))//looking for the word representing the link
{
searchHttp = text.indexOf(word);
wordEndIndex = searchHttp+word.length();
if(startOffset>=searchHttp && startOffset<=wordEndIndex)//after the link word was found, making sure the double click was made on this link
{
try
{
jtxa.select(searchHttp, wordEndIndex);
desk.browse(new URI(word)); //opening the link in browser. Desktop desk = Desktop.getDesktop();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
}
}
});

Categories

Resources