I tried to follow this example Font Awesome with Swing
Every thing work fine but when i tried to add some text the font not show correct, it show me something like this:
My code
....
try (InputStream is = TestFontAwsome.class.getResourceAsStream("fontawesome-webfont.ttf")) {
Font font = Font.createFont(Font.TRUETYPE_FONT, is);
font = font.deriveFont(Font.PLAIN, 24f);
JLabel label = new JLabel("\uf0c0 font not correct");
label.setFont(font);
label.setForeground(Color.red);
label.setFont(font);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
frame.add(label);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (IOException | FontFormatException exp) {
exp.printStackTrace();
}
....
The result
I already installed the Font:
Any idea about the problem?
JLabel label = new JLabel("\uf0c0 font not correct");
The JLabel font is set to Font Awesome, which has no glyph for the ascii text provided in the JLabel. AFAIK there is not a way to mix the fonts within a single JLabel - you may be able to do this with some form of html, but a easier solution might be to just use two JLabels with different fonts.
JLabel l1 = new JLabel("\uf0c0");
JLabel l2 = new JLabel("This is ascii text");
l1.setFont(fontAwesome);
Box mix = Box.createHorizontalBox();
mix.add(l1); mix.add(l2);
myContainer.add(mix);
You can alternatively custom paint a Component using paintComponent, setting the Font as needed.
Related
I tried to put a simple icon in a JPanel formatted with the BoxLayout.
JPanel panel_4 = new JPanel();
contentPane.add(panel_4, BorderLayout.CENTER);
panel_4.setLayout(new BoxLayout(panel_4, BoxLayout.X_AXIS));
ImageIcon seven= new ImageIcon("C:\\Users\\alewe\\workspace\\SlotMachine\\Lucky_Seven-128.png");
JLabel lblNewLabel_1 = new JLabel(seven);
panel_4.add(lblNewLabel_1);
When I ran the code it gave me the error "Some characters cannot be mapped using "Cp1252" character encoding", I saved by UTF-8, now it starts but I can't see the icon.
Maybe if you use setIcon will help you:
ImageIcon seven= new ImageIcon("C:\\Users\\alewe\\workspace\\SlotMachine\\Lucky_Seven-128.png");
JLabel lblNewLabel_1 = new JLabel();
//Set your icon to your label
lblNewLabel_1.setIcon(seven);
panel_4.add(lblNewLabel_1);
You can read more about icons here
you will need a inputstream to read the picture. use it like this:
File f = new File("filepath");
InputStream in=new FileInputStream(f);
if (in != null) {
ImageIcon imageIcon = new ImageIcon(ImageIO.read(in));
label.setIcon(imageIcon);
} else {
LOG.debug("No icon found...");
}
I have created a GUI. It has several buttons. i would like to add a console like object underneath them in which i would be able to write messages so the user would see them.
what element/object/class should i use? i want it to be able to present messages in different lines.
here is my code for creating the GUI:
public ssGUI() {
setLayout(new BorderLayout());
bRunNoAdj = new JButton("Run no adjustment");
bRunNoAdj.setVerticalTextPosition(AbstractButton.CENTER);
bRunNoAdj.setHorizontalTextPosition(AbstractButton.LEADING);
bRunNoAdj.setMnemonic(KeyEvent.VK_E);
bRunNoAdj.addActionListener(this);
bRunNoAdj.setEnabled(false);
bRunNoAdj.setBackground(Color.white);
bRunAdj = new JButton("Run adjustment");
bRunAdj.setVerticalTextPosition(AbstractButton.CENTER);
bRunAdj.setHorizontalTextPosition(AbstractButton.LEADING);
bRunAdj.setMnemonic(KeyEvent.VK_E);
bRunAdj.addActionListener(this);
bRunAdj.setEnabled(false);
bRunAdj.setBackground(Color.white);
bConnect = new JButton("Connect");
bConnect.setMnemonic(KeyEvent.VK_E);
bConnect.addActionListener(this);
bConnect.setEnabled(true);
bConnect.setBackground(Color.white);
bDisconnect = new JButton("Disconnect");
bDisconnect.setMnemonic(KeyEvent.VK_E);
bDisconnect.addActionListener(this);
bDisconnect.setEnabled(false);
bDisconnect.setBackground(Color.white);
bStationary = new JButton("Show Stationary");
bStationary.setMnemonic(KeyEvent.VK_E);
bStationary.addActionListener(this);
bStationary.setEnabled(false);
bStationary.setBackground(Color.white);
bMoving = new JButton("Show Moving");
bMoving.setMnemonic(KeyEvent.VK_E);
bMoving.addActionListener(this);
bMoving.setEnabled(false);
bMoving.setBackground(Color.white);
JPanel topPanel = new JPanel();
topPanel.add(bConnect);
topPanel.add(bDisconnect);
add(topPanel, BorderLayout.NORTH);
JPanel centerPanel = new JPanel();
centerPanel.add(bRunNoAdj);
centerPanel.add(bRunAdj);
add(centerPanel, BorderLayout.CENTER);
JPanel bottomPanel = new JPanel();
bottomPanel.add(bStationary);
bottomPanel.add(bMoving);
add(bottomPanel, BorderLayout.SOUTH);
}
any help would be appreciated, thank you.
The easiest would probably be to use a JTextArea.
You would ofcourse prevent the user from editing the area, this can be done like this:
JTextArea area = new JTextArea();
area.setEditable(false);
And if you want the user to be able to scroll the area you can add it to a JScrollPane like this:
JTextArea area = new JTextArea();
JScrollPane scrollableArea = new JScrollPane(area);
And lastly you can add a line to the area by doing:
area.setText(area.getText() + "\n" + newLine);
Or preferably:
area.append("\n" + newLine);
I hope this helps :)
Use a JTextArea. Call text_area.setEditable(false); on it to make it read-only.
I think what you mean is JTextArea or JTextField
I have a panel with FlowLayout specified that is full of labels, text fields, and text areas. The frame isn't that wide, but the panel becomes tall because of all the components inside. I want to add the panel to a JScrollPane, so I can scroll vertically through the panel. However, when I add the panel to the scroll pane and add the scroll pane to the frame, all the components are right next to each other and it scrolls horizontally through them. Here's the code:
public class Form {
JTextField jtfName = new JTextField(15);
JTextField jtfTitle = new JTextField(15);
JTextField jtfAuthor = new JTextField(15);
JTextArea jtaSetting = new JTextArea(5, 15);
JTextArea jtaMainChars = new JTextArea(5, 15);
JTextArea jtaConflict = new JTextArea(5, 15);
JTextArea jtaQuote = new JTextArea(5, 15);
JTextArea jtaMainCharShows = new JTextArea(5, 15);
JPanel jpnlName = new JPanel();
JPanel jpnlTitle = new JPanel();
JPanel jpnlAuthor = new JPanel();
Form() {
// Create a new JFrame container.
JFrame jfrm = new JFrame("Organizer");
jfrm.setLayout(new GridLayout(0,1));
// Give the frame an initial size.
jfrm.setSize(300, 300);
// Terminate the program when the user closes the application.
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a panel.
JPanel jpnl = new JPanel();
// Create labels.
JLabel jlabName = new JLabel("Student Name:");
JLabel jlabTitle = new JLabel("Title:");
JLabel jlabAuthor = new JLabel("Author:");
JLabel jlabSetting = new JLabel("Setting (Time and Place):");
jlabSetting.setHorizontalAlignment(SwingUtilities.CENTER);
JLabel jlabMainChars = new JLabel("Main Characters:");
jlabMainChars.setHorizontalAlignment(SwingUtilities.CENTER);
JLabel jlabConflict = new JLabel("<html>Describe the major conflict of the<br>story in one well-written sentence:");
jlabConflict.setHorizontalAlignment(SwingUtilities.CENTER);
JLabel jlabQuote = new JLabel("<html>Find and write down a passage (quote<br>from the book that reveals a significant<br>personality trait of one of the main characters<br>and GIVE THE PAGE #:");
jlabQuote.setHorizontalAlignment(SwingUtilities.CENTER);
JLabel jlabMainCharShows = new JLabel("<html>Explain in your own words what the<br>passage (quote) shows about the main character.");
jlabMainCharShows.setHorizontalAlignment(SwingUtilities.CENTER);
// Add text fields to panel.
jpnlName.add(jlabName);
jpnlName.add(jtfName);
jpnlTitle.add(jlabTitle);
jpnlTitle.add(jtfTitle);
jpnlAuthor.add(jlabAuthor);
jpnlAuthor.add(jtfAuthor);
// Add components to main panel.
jpnl.add(jpnlName);
jpnl.add(jpnlTitle);
jpnl.add(jpnlAuthor);
jpnl.add(jlabSetting);
jpnl.add(jtaSetting);
jpnl.add(jlabMainChars);
jpnl.add(jtaMainChars);
jpnl.add(jlabConflict);
jpnl.add(jtaConflict);
jpnl.add(jlabQuote);
jpnl.add(jtaQuote);
jpnl.add(jlabMainCharShows);
jpnl.add(jtaMainCharShows);
// Add the panel to a scroll pane.
JScrollPane jspPanel = new JScrollPane(jpnl);
// Add the scroll pane to the frame.
jfrm.getContentPane().add(jspPanel);
// Display the frame.
jfrm.setVisible(true);
}
}
Without know exactly the layout you want, the best solution I can suggest is to try the WrapLayout
It addresses the major problem with the FlowLayout, it doesn't wrap.
Why don't you try BoxLayout:
jpnl.setLayout(new BoxLayout(jpnl, BoxLayout.PAGE_AXIS));
I'm using a JFrame to present a message box with some text and 2 buttons. How do I get the text to wrap automatically based on the size of the box?
Here's my current code:
dialogFrame = new JFrame();
JButton newButton = new JButton("New");
newButton.addActionListener(new newUploadAction());
JButton resumeButton = new JButton("Resume");
resumeButton.addActionListener(new resumeUploadAction());
//dialogFrame.setUndecorated(true);
JPanel addPanel = new JPanel();
JPanel addPanel2 = new JPanel();
addPanel.add(newButton);
addPanel.add(resumeButton);
String text = "<html><p>A previous control file exists for this file. ";
text += "Would you like to initiate a new transfer or resume the previous one?</p></html>";
JLabel testLabel = new JLabel(text);
//testLabel.setPreferredSize(new Dimension(1, 1));
addPanel2.add(testLabel);
Container content = dialogFrame.getContentPane();
//content.setBackground(Color.white);
content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));
content.add(addPanel2);
content.add(addPanel);
dialogFrame.setSize(200,200);
dialogFrame.setVisible(true);
dialogFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
I read somewhere that calling
testLabel.setPreferredSize(new Dimension(1, 1));
would cause the wrapping behavior I want, but that just resulted in the text not showing up at all.
You could place the text in a JTextArea and call setLineWrap(true) and setWrapStyleWord(true) on the JTextArea. If you want it to look more JLabel-ish, then just change the color settings of the JTextArea to your liking.
EDIT 1
Also another thing that could work: consider having the holding JPanel use a BorderLayout:
JPanel addPanel2 = new JPanel(new BorderLayout()); //!! added BorderLayout
So that the JLabel added will fill the addPanel2.
And how do I put Image on a JPanel using Netbeans?
Have a look at this tutorial: Handling Images in a Java GUI Application
At the same time you could code as well:
JPanel panel = new JPanel();
ImageIcon icon = new ImageIcon("image.jpg");
JLabel label = new JLabel();
label.setIcon(icon);
panel.add(label);
You can use ImageIcon and JLabel:
ImageIcon icon = createImageIcon("image.gif", "sample image");
// Label with text and icon
JLabel label1 = new JLabel("Image and Text", icon, JLabel.CENTER);
// Label with icon only
JLabel label2 = new JLabel(icon);
// Add to your JPanel
panel.add(label1);
panel.add(label2);
1) First Paste the image to the Application folder
2) Add a label to the panel
3) Right click label-> Properties
4) Select Icon-> more option
5) Select Package and File and Click oK
6) U R DONE :)
You have to set a JLabel with your icon property set to the picture.
For a more detailed solution to the particular problem, go to
http://www.netbeanstutorials.com/p/handling-images.html
There's even a video how to do it there in case you need it.