How to display an ArrayList in a JPanel - arraylist

How can I get a JList to show in a JPenel. For example I'd like to have the following groups of items displayed in a JPanel, with each group showing on it's own column, with a new group being dropped into a new line, like how Google lists search results.
For example:
import java.util.ArrayList;
import java.util.List;
import javax.swing.JPanel;
public class ReaderImpl {
JPanel paneTo = new JPanel();
List<String> text() {
List<String> lovely = new ArrayList<String>(4);
lovely.add("Tall, Short, Average"); // line 1
lovely.add("mangoes, apples, Bananas"); // line 2
lovely.add("12, 33");
return lovely;
}
// How do I add the lovely ArrayList to paneTo
}

A JList renderer can draw a checkbox, but JList does not support a cell editor. Instead, consider a one-column JTable.
Check out this link here.
Hope that helps.

Related

PDF created by iText7 with Header is not accessible

I created a pdf with iText7. The pdf has a header on each page which consists of two (sometimes more) rows. I added them as in the jump start tutorial, chapter 3.
The problem is, that there are no tags generated, so the screenreader (JAWS) does'nt find the header and blind users can not access it.
I tried to add some tags manually to mimic a table, but that seems to be ignored completly.
Here is my code to create the pdf:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.events.Event;
import com.itextpdf.kernel.events.PdfDocumentEvent;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.*;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.pdfa.PdfADocument;
public class ITextHeader {
private PdfADocument pdf;
private PdfFont bf;
public static void main(String[] args) throws Exception {
new ITextHeader().createPdf();
}
private void createPdf() throws Exception {
PdfWriter writer = new PdfWriter(new FileOutputStream("header.pdf"));
InputStream icm = new FileInputStream("sRGB_CS_profile.icm");
pdf = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_1A,
new PdfOutputIntent("Custom", "", null, "sRGB IEC61966-2.1", icm));
pdf.setTagged();
bf = PdfFontFactory.createFont("arial.ttf", PdfEncodings.IDENTITY_H);
try (Document pdfDocument = new Document(pdf, PageSize.A4, true)) {
pdfDocument.setMargins(100, 15, 50, 15);
pdf.addEventHandler(PdfDocumentEvent.START_PAGE, this::createHeader);
pdfDocument.add(new Paragraph("Here is the content").setFont(bf).setFontSize(10));
}
}
public void createHeader(Event event) {
PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
PdfPage page = docEvent.getPage();
PdfCanvas pdfCanvas = new PdfCanvas(
page.newContentStreamBefore(), page.getResources(), pdf);
pdfCanvas.beginText()
.setFontAndSize(bf, 10)
.beginMarkedContent(PdfName.Table)
.moveText(15, 804)
.beginMarkedContent(PdfName.TR)
.beginMarkedContent(PdfName.TD)
.showText("My Title")
.endMarkedContent() // TD
.moveText(466, 0)
.beginMarkedContent(PdfName.TD)
.showText("Date: 01.01.2022")
.endMarkedContent() // TD
.endMarkedContent() // TR
.moveText(-466, -14)
.beginMarkedContent(PdfName.TR)
.beginMarkedContent(PdfName.TD)
.showText("My Subtitle")
.endMarkedContent() // TD
.moveText(466, 0)
.beginMarkedContent(PdfName.TD)
.showText("Time: 12:30")
.endMarkedContent() // TD
.endMarkedContent() // TR
.endMarkedContent() // TABLE
.endText();
}
}
This is the structure of the pdf as shown by PDF Accessibility Checker:
The Accesibility Checker also complains about not tagged content:
We solved the issue with the following workaround: the header on the first page is rendered as a PDF table, on the following pages we use the canvas to display the text.
This solution is somewhat ankward because we have to implement the headers twice with different techniques, but now JAWS finds at least the header on the first page.

how do i call captureScreenshot method for reporting a bug?

My following code displayed 5 tooltip list only, actually 6 tooltips are there. System.out.println(contactTooltipList.size()) giving the size 6.
public void clickOnContacts()
{
log.info("Clicked on 'Contacts' tab option and Tooltips are: ");
System.out.println(contactTooltipList.size());
contactDropDown.click();
Iterator<WebElement> itr= contactTooltipList.iterator();
while(itr.hasNext()) {
String toolTips = itr.next().getText();
toolTips=toolTips.replaceAll("\n", " ");
System.out.println(toolTips);
}
System.out.println("\n");
}
The first tooltip(Address) is missing, above code has displayed the tooltips from 2 to 5 as shown below:
Home: (111) 222-3333
Mobile: (500) 000-0000
Email: xxxxxxxxx...
Secondary Language -
Preferred Unknown
I would like to report as bug by capturing the screenshot as displayed tooltips are not as per size.
How do i call captureScreenshot method? Or how do i report it's a bug?
In Java, you can take a screenshot as such:
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
// add the above imports
File screenshotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
Now that you have screenshotFile stored, you can do whatever you need with it -- such as save it to your system:
FileUtils.copyFile(screenshotFile, new File("C:/Path/where/to/save/screenshot));

Remove the border of a table headerCell itext7 pdf

I am creating a table using itext7.
I can change the color of a regular cell. How can I change the border to Null of a header cell?
I cannot add a check to create a "false" headerCell as the table may be a mutli-column so I would like the cells to be atop each column.
Having this false headerCell would just be on the first cell generated.
I tried:
//does not work.
table.setBorder(null);
//---------------------------does not work.
table.addHeaderCell("one: ").setBorder(Border.NO_BORDER);
table.addHeaderCell("two: ").setBorder(null).addStyle(normal);
table.addHeaderCell("three: ").setBorder(null).addStyle(normal);
Minimal:(just set your location)
package application;
import java.io.IOException;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import com.itextpdf.io.font.FontConstants;
import com.itextpdf.kernel.color.Color;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfDocumentInfo;
import com.itextpdf.kernel.pdf.PdfString;
import com.itextpdf.kernel.pdf.PdfViewerPreferences;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.ColumnDocumentRenderer;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.Style;
import com.itextpdf.layout.border.Border;
import com.itextpdf.layout.border.SolidBorder;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.element.Text;
import javafx.application.Application;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
exportRes();
}
public static void main(String[] args) {
launch(args);
}
public void exportRes() throws IOException{
PdfWriter writer = new PdfWriter(--location--);
PdfDocument pdf = new PdfDocument(writer);
pdf.getCatalog().setLang(new PdfString("en-US"));
pdf.getCatalog().setViewerPreferences(new PdfViewerPreferences().setDisplayDocTitle(true));
PdfDocumentInfo info = pdf.getDocumentInfo();
info.setTitle("List");
Document document = new Document(pdf,PageSize.A4);
PdfFont font = PdfFontFactory.createFont(FontConstants.HELVETICA);
Style normal = new Style();
normal.setFont(font).setFontSize((float) 7.5);
Style notesF = new Style();
notesF.setFont(font).setItalic().setFontSize((float) 7.5);
Style lastNameF = new Style();
lastNameF.setFont(font).setFontSize((float) 7.5).setBold();
Table table = new Table(new float[]{(float) 2,29,10});
table.setWidthPercent(100);
table.setFont(font);
//does not work.
table.setBorder(null);
//---------------------------does not work.
table.addHeaderCell("one: ").setBorder(Border.NO_BORDER);
table.addHeaderCell("two: ").setBorder(null).addStyle(normal);
table.addHeaderCell("three: ").setBorder(null).addStyle(normal);
Cell check = new Cell().add(new Paragraph(" ").addStyle(normal).setFixedLeading(0)).setBorder(new SolidBorder(Color.LIGHT_GRAY, (float) 0.5));
table.addCell(check);
Text lastName = new Text("Name ").setBold().addStyle(lastNameF);
Text address = new Text("address").addStyle(normal);
Cell name = new Cell().add(new Paragraph().add(lastName).add(address)).setBorder(new SolidBorder(Color.LIGHT_GRAY, (float) 0.5));
table.addCell(name);
Cell notes = new Cell().add("notes").addStyle(notesF).setBorder(new SolidBorder(Color.LIGHT_GRAY, (float) 0.5));
table.addCell(notes);
document.add(table);
document.close();
}
}
Your code is almost right. The problem is very simple and can be found in the next line:
table.addHeaderCell("one: ").setBorder(Border.NO_BORDER);
Notice that Table#addHeaderCell method returns Table not Cell object. So then you apply Border.NO_BORDER on Table.
The solution is:
table.addHeaderCell(new Cell().add("one: ").setBorder(Border.NO_BORDER));
Also if you want to set specific border on header/footer (the analog of Table border), write summat table.getHeader().setBorder(your border). Notice also that itext applys table border on header/footer too.
I also want to mention that invoking Table constructor as you do isn't 100% correct if you use itext 7.0.2+. If you want columns widths to be in a ratio 2 29 30 (or any else), call Table(UnitValue.createPercentArray(new float[]{2, 29, 30)) ;

JavaFX Separator depend not on the Width of the Class

i would like to get a Separator, which changes his Size with the Size of the Mother-Component.
In my Example, i have a JavaFX Popup and there i add a VBox. To this VBox i add a HBox. And this HBox has a Label, a Speparator and a Button.
Now i would like to have that the Button is on the Right End and the Label is on the Left End of the HBox. I think i have to use a Separator between these components to get the Space.
How can i handle it...
I made something like this, but it does not work.
// Box for the Headline
HBox headLine = new HBox();
headLine.setPadding(new Insets(5, 5, 5, 5));
// Label with the HeadLine Description in
final Label heading = new Label(headLineText);
heading.getStyleClass().addAll("popup-label-name");
// Close Button
close = new Button("X");
close.setVisible(false);
closeButtonHandler();
// Creates an invisble Separator1
Separator sep = new Separator(Orientation.HORIZONTAL);
sep.setVisible(false);
sep.widthProperty().add(m_container.widthProperty().get());
close.getStyleClass().addAll("popup-button", "popup-button-color");
// Adds to the Headline the Data
headLine.getChildren().addAll(heading, sep, close);
The Variable m_container is the VBox! How can i handle it?
Thanks for your help :)
The simplest way (if not using a different container like AnchorPane) is to insert an invisible, but expandible 'space' object:
void testLabelSpace(HBox box) {
Text first = new Text("first");
Text second = new Text("second");
Node space = new HBox();
HBox.setHgrow(space, Priority.ALWAYS);
box.getChildren().addAll(first, space, second);
}
If I understand the question correctly, you just want blank space between the label and the button. Just tell the Label always to grow horizontally, and set its maximum width to allow it to grow to any size:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;
public class HBoxExample extends Application {
#Override
public void start(Stage primaryStage) {
HBox hbox = new HBox();
Label label = new Label("Label");
Button button = new Button("Button");
HBox.setHgrow(label, Priority.ALWAYS);
label.setMaxWidth(Double.MAX_VALUE);
hbox.getChildren().addAll(label, button);
primaryStage.setScene(new Scene(hbox, 350, 75));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

How to Add Text and Image Both in a SWT LABEL

Is there any way to add Text and Image in SWT label in a single line.
Once I add image, text goes off.
No you can't have an image and text simultaneously in a Label (unless you custom draw it). Else use org.eclipse.swt.custom.CLabel:
Code:
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class LabelTest {
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Image image = new Image(display, "next.png");
CLabel label = new CLabel(shell, SWT.BORDER);
label.setImage(image);
label.setText("This is a CLabel !!");
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
if(image != null)
{
image.dispose();
image = null;
}
display.dispose();
}
}
Output:
Button click to open FileDialog Box and selected any image to display with text on specific label.
import org.eclipse.swt.custom.CLabel
A Label which supports aligned text and/or an image and different border styles.
I hope this answer is usefull.
please visit this page:
How to load image to view in RCP?
Yes, using an intermediary composite with the right layout
Composite composite = new Composite(parent, SWT.NONE);
composite.setLayout(new RowLayout(SWT.HORIZONTAL));
Label imageLabel = new Label(composite, SWT.NONE);
mageLabel.setImage(...);
Label textLabel = new Label(composite, SWT.NONE);
textLabel.setText(...)