Printing out an AttributedString - java

I am using an attributed string to bold the heading names, and I would like to print out in a Text object in javafx, but I cannot figure out how. I've looked a fair amount of places online, including the java docs but nothing seems to go over this... Here is what I am trying to do:
AttributedString boldName = new AttributedString("Name: ");
boldName.addAttribute(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
String name = innerNode.name;
Text info = new Text(**boldName.something()?** + name);
The result should be this: Name: name, something quite simple I feel but how??

You cannot use the AttributedString in JavaFX. Instead use,
On JavaFX 2.2 and earlier:
Text name = new Text("Name: ");
name.setFill(Color.BLUE);
name.setFont(Font.font("Helvetica", FontWeight.BOLD, 12));
Text info = new Text(innerNode.name);
HBox hbox = new HBox(1);
hbox.getChildren().addAll(name, info);
On JavaFX 8:
Text name = new Text("Name: ");
name.setFill(Color.BLUE);
name.setFont(Font.font("Helvetica", FontWeight.BOLD, 12));
Text info = new Text(innerNode.name);
TextFlow textFlow = new TextFlow(name, info);
Reference:
Using Text and Text Effects in JavaFX
javafx.scene.text.TextFlow

Related

With multiple yes/no radio buttons, do I have to create each instance?

I'm trying to make an app with a panel where it asks a question (whether an image contains humans, cars, or other objects... and more). Each question is answered with yes/no. I didn't want to make JRadioButton for each of the 'yes's and 'no's. So I tried to add the same Radio Button multiple times but it doesn't work (See below).
contentsPanel = new JPanel();
contentsHumans = new JLabel("Humans? ");
contentsCars = new JLabel("Cars? ");
contentsOtherObjects = new JLabel("Other Objects? ");
yes = new JRadioButton("Yes");
no = new JRadioButton("No");
binaryAnswer = new ButtonGroup();
binaryAnswer.add(yes);
binaryAnswer.add(no);
contentsPanel.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(), "Image contains... "));
contentsPanel.add(contentsHumans);
contentsPanel.add(yes); contentsPanel.add(no);
contentsPanel.add(contentsCars);
contentsPanel.add(yes); contentsPanel.add(no);
contentsPanel.add(contentsOtherObjects);
contentsPanel.add(yes); contentsPanel.add(no);
Do I have to separately create y1 = new JRadioButton("Yes");, y2 = new JRadioButton("Yes");, etc., for all my 'yes's and 'no's?? I can definitely copy and paste to do that but I was wondering if there is any other way.
Here's the image of current output:

Java how do I access text field

I'm relatively new to programming. Hoping someone can help. I have a button that creates a series of labels and text boxes within a frame once the user enters a number. I'm having trouble access the text boxes once they are created. Can someone point me in the right direction. Code snips below. Thanks.
method to create a label and text box.
public JTextField createPrizePanels(){
JLabel prizePanel = new JLabel("Enter Prize Here", SwingConstants.CENTER);
prizePanel.setVerticalAlignment(SwingConstants.TOP);
prizePanel.setFont(new Font("Arial", Font.BOLD, 14));
prizePanel.setForeground(Color.BLUE);
Border border = BorderFactory.createLineBorder(Color.GRAY, 1);
prizePanel.setBorder(border);
prizePanel.setOpaque(true);
prizePanel.setBackground(Color.LIGHT_GRAY);
prizePanel.setBounds(setBoundsX, setBoundsY, 120, 60);
prizeTextBox = new JTextField(50);
prizeTextBox.setBounds(setBoundsX + 5, setBoundsY + 20, 110, 30);
prizeTextBox.setFont(new Font("Arial", Font.BOLD, 12));
prizeTextBox.setOpaque(true);
prizeTextBox.setBackground(Color.WHITE);
prizeTextBox.setForeground(Color.BLACK);
prizeTextBox.setText("No Prize");
prizeTextBox.setHorizontalAlignment(JTextField.CENTER);
lp.add(prizePanel);
lp.add(prizeTextBox);
return prizeTextBox;
}
code that creates multiple text boxes bases on input from user.
JButton numberOfBallonsButton = new JButton("Set");
numberOfBallonsButton.setBounds(360,160,95, 0x1e);
numberOfBallonsButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
numberOfBallons = Integer.parseInt(numberOfBallonsTextBox.getText());
System.out.println(numberOfBallons);
lp.remove(numberOfBallonsButton);
for(int i = 0; i < numberOfBallons; i++ ){
createPrizePanels();
setBoundsX = setBoundsX +125;
if(setBoundsX > 450){
setBoundsX = 120;
setBoundsY = setBoundsY + 65;
}
}
lp.add(startGameButton);
}
});
Up to this point it works fine. However, the user needs to enter text into each text box and press another button. I'm not sure how to access each text field. Thanks in advance.
You are using local fields.you can create global field and the return value of createPrizePanels() is never used and for your case you don't know number of created textfield so you can use List as global field and when you create textfield add it to the list so you access it later from anywhere

how to get the content of a JTextPane with its styled format

I am trying to make a simple word processor that edits the text to make it bold, italic, underline, background color and foreground color. The problem is I want to set the contents/text of the JTextPane with all its edited attributes to a single object to save it to another class as a data field which have other data fields like date created and the name of the document given by the user.
I think the best approach its using html as content type for the Text Pane and string builders.
for example,
TextPane tp = new JTextPane();
tp.setContentType("text/html");
StringBuilder sb = new StringBuilder();
sb.append("<span style=\"color:red\">" + Hello red + "</span>");
sb.append("<span style=\"color:blue\">" + Hello blue + "</span>");
...
tp.setText(sb); // will print text with the style
works the same in the other way,
String txt = tp.getText();
System.print(txt); //wil show html code
You can reference http://www.java2s.com/Tutorials/Java/Swing_How_to/JTextPane/Style_JTextPane_with_HTML_and_CSS.htm

Text Formatting is changed while Sharing data: Android

I want to share diamond detail in table format(Plain text not HTML)
I have made this using this library but its prints the data properly using the system.out but while I sharing it, its format is changed:
Below is my code:
List<String> headersList = Arrays.asList("", "");
List<List<String>> rowsList = Arrays.asList(
Arrays.asList("Stone Id :", details[0]),
Arrays.asList("Lab", details[1]),
Arrays.asList("Shape", details[2]),
Arrays.asList("Carat", details[3]),
Arrays.asList("Clarity-Color", details[4]),
Arrays.asList("Cut-Pol-Sym-Flou", details[5]));
Board board = new Board(75);
Table table = new Table(board, 75, headersList, rowsList);
table.invalidate().setGridMode(Table.GRID_NON).setRowsList(rowsList);
List<Integer> colWidthsList = Arrays.asList(30, 14);
table.setColWidthsList(colWidthsList);
Block tableBlock = table.tableToBlocks();
board.setInitialBlock(tableBlock);
board.build();
String preview1 = board.getPreview();
System.out.print(preview1);
sharingIntent.putExtra(Intent.EXTRA_TEXT,preview1);
The console uses monospaced font which takes exactly same width for all characters. But your view isn't using it, so it looks messed up.
Use a monospaced font.
Or use a tabular format. Perhaps a ListView with each row having two text views side by side having fixed width.

How do I easily edit the style of the selected text in a JTextPane?

How do I easily edit the style of the selected text in a JTextPane? There doesn't seem to be many resources on this. Even if you can direct me to a good resource on this, I'd greatly appreciate it.
Also, how do I get the current style of the selected text? I tried styledDoc.getLogicalStyle(textPane.getSelectionStart()); but it doesn't seem to be working.
Here's a code snippet to insert a formatted "Hello World!" string in a JEditorPane:
Document doc = yourEditorPane.getDocument();
StyleContext sc = new StyleContext();
Style style = sc.addStyle("yourStyle", null);
Font font = new Font("Arial", Font.BOLD, 18);
StyleConstants.setForeground(style, Color.RED);
StyleConstants.setFontFamily(style, font.getFamily());
StyleConstants.setBold(style, true);
doc.insertString(doc.getLength(), "Hello World!", style);
Take a look at the following code in this pastebin:
http://pbin.oogly.co.uk/listings/viewlistingdetail/d6fe483a52c52aa951ca15762ed3d3
The example is from here:
http://www.java2s.com/Code/Java/Swing-JFC/JTextPaneStylesExample3.htm
It looks like you can change the style using the following in an action listener:
final Style boldStyle = sc.addStyle("MainStyle", defaultStyle);
StyleConstants.setBold(boldStyle, true);
doc.setCharacterAttributes(0, 10, boldStyle, true);
It sets the style of the text between the given offset and length to a specific style.
See the full pastebin for more details. That should fix your problem though.
The easiest way to manipulate text panels is using editor kits and their associated actions. You can find a demo of this in the JDK samples (under jdk\demo\jfc\Stylepad).
Sample code that installs a StyledEditorKit and uses a FontSizeAction to manipulate the text:
public static void main(String[] args) {
// create a rich text pane
JTextPane textPane = new JTextPane();
JScrollPane scrollPane = new JScrollPane(textPane,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
// install the editor kit
StyledEditorKit editorKit = new StyledEditorKit();
textPane.setEditorKit(editorKit);
// build the menu
JMenu fontMenu = new JMenu("Font Size");
for (int i = 48; i >= 8; i -= 10) {
JMenuItem menuItem = new JMenuItem("" + i);
// add an action
menuItem
.addActionListener(new StyledEditorKit.FontSizeAction(
"myaction-" + i, i));
fontMenu.add(menuItem);
}
JMenuBar menuBar = new JMenuBar();
menuBar.add(fontMenu);
// show in a frame
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 400);
frame.setJMenuBar(menuBar);
frame.setContentPane(scrollPane);
frame.setVisible(true);
}
(Tip: if you want to use a FontFamilyAction, have a look at GraphicsEnvironment.getAvailableFontFamilyNames() and logical font family names.)
I'd recommend taking a look at Sun's Java Tutorial about editor panes.
Ok, wow. Hard question. So I have not found a way to get the style of a given character. You can, however, get the MutableAttributeSet for a given character and then test to see if the style is in that attribute set.
Style s; //your style
Element run = styledDocument.getCharacterElement(
textPane.getSelectionStart() );
MutableAttributeSet curAttr =
( MutableAttributeSet )run.getAttributes();
boolean containsIt = curAttr.containsAttributes( s );
One problem with getting the Style for a range of characters is that there may be more than one style applied to that range (example: you may select text where some is bold and some is not).
To update the selected text you can:
Style s; //your style
JTextPane textPane; //your textpane
textPane.setCharacterAttributes( s, false );
Oh, and it appears that the function getLogicalStyle doesn't work because it's returning the default style (or maybe just the style) for the paragraph that contains p, rather than the the style of the character at p.

Categories

Resources