JLabel's text is in the middle of the hitbox - java

I have created a JLabel and draw a long String on it. But, the problem is, however long is the content, the content always display at the middle of the label's box! I want to push the text to the top of the box.
How can I fix it? Thank you so much!
Font times = new Font("Times New Roman", Font.CENTER_BASELINE, 13);
JLabel mess = new JLabel();
mess.setFont(times);
mess.setLocation(300, 125);
mess.setSize(250, 200);
mess.setText(message);
mess.setForeground(Color.WHITE);
getContentPane().add(mess);

Related

How do i add a scrollbar with JtextArea and implement JScrollPane in a GUI?

I need help adding a scrollbar to an empty textbox that will output the information that i input into the required textboxes by then clicking the buttons. My issue is im not sure how to implement the scrollbar correctly with the textbox that is in the code, it just goes over and it and does nothing like in the picture shown. I need it to align with the textbox and not mess with the Jtextfield so that it can properly scroll the textbox, i will handle the events after i finish the design.
Code:
// Display box for all the inputs
JTextField outputBox = new JTextField(5);
wv.add(outputBox, 39, 575, 800, 150);
JScrollBar outputBoxScrollBar = new JScrollBar(JScrollBar.VERTICAL, 30, 20, 0, 500);
wv.add(outputBoxScrollBar, 790, 300, 50 , 250);
Output:
https://i.stack.imgur.com/oHnY8.png - Link for different OS
A JTextField is single-line, I guess you are looking for JTextArea.
You need to wrap JTextArea in a JScrollPane (not a JScrollBar) like this.
JTextArea outputBox = new JTextArea(5, 20); //rows, columns
wv.add(new JScrollPane(outputBox), 790, 300, 50 , 250);

JScrollBar: Horizontal scrollbar not showing

I have JtabbedPane where every tab has a JtextPane wrapped inside a JScrollPane.
The Vertical scroll bar is showing up when necessary, but the horizontal isn't, and instead of making a horizontal bar it cuts the text and continues it in next line, which is not a point.
I have tried to resize the tab size and declaring the size of the scrollpane larger than tab's, but the result the same.
tabs = new TabbedPane();
tabs.setBounds(0, 0, windowWidth - 70, windowHight - 105);
for (Responses orgData: GUI.getOriginalRequest().getArrayOfResponses())
{
sc = StyleContext.getDefaultStyleContext();
style = sc.addStyle("ConstantWidth", null);
str = formatter.format((formatter.unformat(orgData.getXMLData())));
ksd = new KeywordStyledDocument(style, sc, s);
ksd.insertString(0, str, style);
ksd.refreshDocument();
pane = new JTextPane(ksd);
pane.setFont(new Font("Courier New", Font.PLAIN, 12));
panel = new JScrollPane(pane);
panel.getHorizontalScrollBar().addAdjustmentListener(null);
panel.setPreferredSize(new Dimension(windowWidth - 170, windowHight - 135));
orgData.setXMLData(formatter.unformat(str));
listener.addKeyAdapter(pane);
tabs.addTab(orgData.getSystemName() + " (Original Request)", panel);
}
What am I missing here?
Thank you.
Update:
After implementing the LineWrape by extending the JTextPane and adding pane.setLineWrap(false) that was sugested by Thanasis, the problem was resolved and the textPane which had a text that exceeded the bounds of the field, was wrapped and had a horizontal scroll bar, but textPane which text was less than the bounds of textPane was wrapped to the last character, and thus, the field became less than a space that is allocated to it, and it looks like panel split to 2 parts, textPane and panel.
How do I resolve this?
Please suggest.
TextPane ends at the last character image

Java : JLabel with text and icon

I would like to ask if it is possible in a single JLabel to have text icon text, what i mean is the icon to be in the center of my String text.
I managed to move text left or right of the icon but i cant figure out how to put the icon in the middle.
icon to be in the center of my String text
A JLabel has text and icon - you can have the label on top of the text, but not two text's and one icon. You can achieve the same look with 3 JLabel's together in the proper Layout. For example, using a BoxLayout:
Box box = Box.createHorizontalBox();
box.add(new JLabel("Text 1"));
JLabel image = new JLabel();
image.setIcon(UIManager.getIcon("OptionPane.errorIcon"));
box.add(image);
box.add(new JLabel("Text 2"));
Alternatively, if you wish the text to be on top of the image you can do so by setting the appropriate alignments:
JLabel label = new JLabel();
label.setIcon(UIManager.getIcon("OptionPane.errorIcon"));
label.setText("My Text");
label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.CENTER);
I would like to ask if it is possible in a single JLabel to have text icon text
A JLabel can display simple HTML:
String html = "<html>before <img src=\"file:someFile.jpg\">after</html>";
JLabel label = new JLabel( html );
but HTML takes longer to render so you may want to consider the BoxLayout approach.

Align text in JLabel to the right

I have a JPanel with some JLabel added with the add() method of JPanel. I want to align the JLabel to the right like the image below but I don't know how to do that. Any Idea? Thanks!
This can be done in two ways.
JLabel Horizontal Alignment
You can use the JLabel constructor:
JLabel(String text, int horizontalAlignment)
To align to the right:
JLabel label = new JLabel("Telephone", SwingConstants.RIGHT);
JLabel also has setHorizontalAlignment:
label.setHorizontalAlignment(SwingConstants.RIGHT);
This assumes the component takes up the whole width in the container.
Using Layout
A different approach is to use the layout to actually align the component to the right, whilst ensuring they do not take the whole width. Here is an example with BoxLayout:
Box box = Box.createVerticalBox();
JLabel label1 = new JLabel("test1, the beginning");
label1.setAlignmentX(Component.RIGHT_ALIGNMENT);
box.add(label1);
JLabel label2 = new JLabel("test2, some more");
label2.setAlignmentX(Component.RIGHT_ALIGNMENT);
box.add(label2);
JLabel label3 = new JLabel("test3");
label3.setAlignmentX(Component.RIGHT_ALIGNMENT);
box.add(label3);
add(box);
JLabel label = new JLabel("fax", SwingConstants.RIGHT);
To me,
it seems as if your actual intention is to put different words on different lines.
But let me answer your first question:
JLabel lab=new JLabel("text");
lab.setHorizontalAlignment(SwingConstants.LEFT);
And if you have an image:
JLabel lab=new Jlabel("text");
lab.setIcon(new ImageIcon("path//img.png"));
lab.setHorizontalTextPosition(SwingConstants.LEFT);
But, I believe you want to make the label such that there are only 2 words on 1 line.
In that case try this:
String urText="<html>You can<br>use basic HTML<br>in Swing<br> components,"
+"Hope<br> I helped!";
JLabel lac=new JLabel(urText);
lac.setAlignmentX(Component.RIGHT_ALIGNMENT);

Stretching in Java Swing Box

I have a code:
Box box_general = Box.createHorizontalBox();
Box box_panel1 = Box.createVerticalBox();
Box box_panel2 = Box.createVerticalBox();
JPanel jpanel_1 = new JPanel();
jpanel_1.setPreferredSize(new Dimension(50, 152));
jpanel_1.setOpaque(true);
jpanel_1.setBorder(BorderFactory.createLineBorder(Color.BLUE));
JPanel jpanel_2 = new JPanel();
jpanel_2.setPreferredSize(new Dimension(340, 152));
jpanel_2.setOpaque(true);
jpanel_2.setBorder(BorderFactory.createLineBorder(Color.RED));
JTextField jtxtf_populationSize = new JTextField();
jtxtf_populationSize.setSize(10, 20);
JTextField jtxtf_processorsAmount = new JTextField();
JButton jbtn_loadProcesses = new JButton("File path");
box_panel1.add(Box.createRigidArea(new Dimension(0,4)));
box_panel1.add(jtxtf_processorsAmount);
box_panel1.add(Box.createRigidArea(new Dimension(0,20)));
box_panel1.add(jbtn_loadProcesses);
jpanel_1.add(box_panel1);
JLabel jlbl_populationSize = new JLabel("Enter the population size");
JLabel jlbl_processorsAmount = new JLabel("Enter the amount of processors");
JLabel jlbl_loadProcesses = new JLabel("Load processes from file");
jlbl_populationSize.setFont(font);
jlbl_processorsAmount.setFont(font);
jlbl_loadProcesses.setFont(font);
box_panel2.add(jlbl_populationSize);
box_panel2.add(Box.createRigidArea(new Dimension(0,4)));
box_panel2.add(jlbl_processorsAmount);
box_panel2.add(Box.createRigidArea(new Dimension(0,15)));
box_panel2.add(jlbl_loadProcesses);
jpanel_2.add(box_panel2);
box_general.add(jpanel_2);
box_general.add(Box.createRigidArea(new Dimension(10,0)));
box_general.add(jpanel_1);
It creates 3 boxes, where the general box contains two other boxes. The problem is: in the box all the compopents are stretched of width. For example, there is a line jtxtf_populationSize.setSize(10, 20); but this text field is stretched in the box_panel1 on width. I tried to use a BoxLayout with it's Alignment_X but this didn't work.
Could you, please, advise me, what to do - how to avoid stretching?
Most of the swing layouts will use the preferred size and the minimum/maximum size over a call to setSize. The key here is to get the right preferred and minimums so that they don't shrink too much, and then insert a strut (Box#createHorizontalStrut) to fill up space where you don't want a component.
With complex layouts like this, consider the SpringLayout, which admittedly has a higher learning curve, but once you get used to it, will allow you to more naturally state the constraints you want.

Categories