Can I highlight text in a JLabel? - java

I have a JLabel with some text in it. This JLabel takes up the whole bottom side of my JFrame. I have changed the background of it to yellow, and the whole bottom side becomes yellow. Is there any way to make it look like the text, which is within the JLabel, is being "highlighted" with only the text having a background color of yellow, and the rest having a different color? The top part of my JFrame is a JPanel.
combo.addItemListener(new ItemChangeListener());
JPanel container = new JPanel();
container.setBackground(Color.BLUE);
response.setFont(new Font("Times new Roman", Font.BOLD, 40));
response.setHorizontalAlignment(SwingConstants.CENTER);
response.setBackground(Color.YELLOW);
response.setOpaque(true);
add(response , BorderLayout.CENTER);
container.add(selectone);
container.add(combo);
this.add(container, BorderLayout.NORTH);

There is a very simple way to do that - use HTML in the text of the label. Mind you, Swing interprets a pretty old version of HTML, that doesn't meet today's standards.
JLabel response = new JLabel( "<html><span bgcolor=\"yellow\">This is the label text</span></html>" );
Nowadays, one should work with style sheets. But if you want to do that, you should probably switch from Swing to JavaFX.
For further details on using HTML in Swing components, see the applicable part of the tutorial.

Use a wrapper panel that respects the preferred size of the label:
JLabel label = new JLabel("...");
label.setOpaque( true );
label.setBackground(...);
JPanel wrapper = new JPanel();
wrapper.add( label );
frame.add(wrapper, BorderLayout.CENTER);
The default layout for a JPanel is a FlowLayout that is horizontally center aligned.
If you want both horizontal and vertical centering then use a GridBagLayout on the panel, with the default GrigBagConstraints when you add the label to the panel.

Related

How do I resize the text inside my JScrollpane - JAVA

I'm currently learning more about Java. I'm working on creating a GUI which is able to 'translate' amino-acid characters into their 3-letter codes.
I've got everything working as intended, but I'm still struggling to understand how I can resize the text inside my JScrollpane to not exceed the width. (Example in picture)
Do I just need to change some settings or maybe add '\n's to fit the JTextArea? Here's the code:
Thanks in advance!
private void createGUI() {
Container window = this.getContentPane();
window.setLayout(new FlowLayout());
panel = new JPanel();
inputField = new JTextField();
startButton = new JButton("Convert to 3-letter code");
display = new JTextPane();
scroll = new JScrollPane(display);
//CUSTOMIZE GUI OBJECTS
inputField.setPreferredSize(new Dimension(200, 20));
display.setPreferredSize(new Dimension(400, 300));
startButton.addActionListener(this);
//SETTING UP TEXTAREA
display.setEditable(false);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
panel.setLayout(new BorderLayout());
panel.add(scroll, BorderLayout.CENTER);
//
window.add(inputField);
window.add(startButton);
window.add(panel);
}
Better use a JTextArea instead of a JScrollPane since the best that the JScrollPane can do is to dynamically resize (Dynamically Resize a JScrollPane?)
I changed JTextPane display to a JTextArea object and changed 'display.setLineWrap(true);'
This fixed the issue I was having with JTextPane.
To answer the question in the title: How do I resize the text inside my JScrollpane
Inside your scrollpane you have some JComponent. Either that JComponent is fully visible since it is smaller or equal to the JScrollpane's viewport. Or it is bigger, in which case the JScrollpane will start displaying scrollbars and the relevant part.
To resize the text you will just have to tell the JComponent inside the JScrollpane to display the text differently. Depending on the JComponent you use this method may vary. Here some examples:
In a JLabel and most other components, increase the font size (How to change the size of the font of a JLabel to take the maximum size)
In a JLabel, switch to a multiline label (Multiline text in JLabel)
In a JTextArea, turn on word wrapping and line wrapping
In a JEditorPane you can even use markup inside the document to use different font sizes at the same time

java JLabel text exceeds parent JLabel width

I have a class called iconLabel that extends JLabel to make buttons.
I use the Font Awesome font to set the text of the JLabel to an icon. I also add another JLabel to that iconLabel object in the constructor to display text on the icon. The problem is that the text on the icon exceeds the width of the icon, so you'll get "...". How can I make it so the JLabel text may exceed the icon width?
Here is an image for clarification.
The problem is that the text on the icon exceeds the width of the icon, so you'll get "...".
A JLabel can display an Icon with text painted on top of the Icon. There is no need for a custom class to do this.
The basic code is:
JLabel label1 = new JLabel( new ImageIcon(...) );
label1.setText( "Easy Way" );
label1.setHorizontalTextPosition(JLabel.CENTER);
label1.setVerticalTextPosition(JLabel.CENTER);
The thing is that I want to use the font awesome icons
So you use the setText() method and you get an Icon painted? Cool!
Maybe you can use a JPanel with an OverlayLayout. Then you add the two labels to the panel, making sure the alignmentX/Y values are both 0.5f so the components are centered in the panel. Now the panel should be the size of the largest label.
Or maybe you can use the Text Icon. This class allows you to create an Icon from a text String. You will still need to labels but the sizing should work correctly.
The code should be something like:
JLabel iconLabel = new JLabel();
iconLabel.setFont(...);
TextIcon icon = new TextIcon(iconLabel, "...")
Jlabel label = new JLabel("some text");
label.setIcon(icon);

How do I stop the position of JLabels changing inside a JPanel whenever I change the text of other JLabels in the same JPanel?

I have multiple JLabels in a JPanel. Whenever I change the text in one, it causes the other JLabels inside the JPanel to move. How to I lock them in place?
The approach really depends how you labels are positioned and what LayoutManager you are using.
JLabel by default will calculate its size mostly based on the label text and font size. If you are changing the text, then the JLabel size will change as well and thus make the layout manager to reposition the other labels.
Here is an example that uses FlowLayout and hard sets JLabel preferred size so that it wont change when its text changes. Other layout managers might choose to ignore the preferred size:
JFrame frame = new JFrame();
frame.getContentPane().setLayout( new FlowLayout() );
JLabel l = new JLabel("text1");
l.setPreferredSize( new Dimension( 50, (int)l.getPreferredSize().getHeight() ) );
frame.getContentPane().add(l);
frame.getContentPane().add( new JLabel("text2") );
frame.getContentPane().add( new JLabel("text3") );
Here if you change text for l it wont shift the other 2 labels.

Java how to preserve newline from String to JLabel?

JLabel newLabel = new JLabel();
String a = b;//b is String from database.
newLabel.setText(a);
I need to generate text pulled from my database which contains multiple line, but when i put them into the label, all of the text became same line.
I tried using JTextArea and it works, however, for some reason it messed with all the other component's alignment in a 1 column BoxLayout panel... which I wanted all the content to be aligned to the left. Any help would be much appreciated! thanks
however, for some reason it messed with all the other component's alignment in a 1 column BoxLayout panel
The is related to the setAlignmentX(...) method of each component.
A JLabel uses 0.0f (for left alignment). A JTextArea and JScrollPane use 0.5f (for center alignment).
Using components with different alignments will cause alignment issues when using a BoxLayout.
The solution is to change the alignment of the text area or scroll pane (whichever component you add to the BoxLayout).
So the basic code would be:
JLabel label = new JLabel(...);
JTextArea textArea = new JTextArea(...);
textArea.setAlignmentX(0.0f);
JPanel panel = new BoxLayout(...);
panel.add(label);
panel.add(textArea);
Now both components will be left aligned.

JAVA JTAB multiple compoments

I'm begging with JAVA Swing. But when I use my JPanel can I only add one JTextPane. How can I do so I can add multiple.
Here's is what I do
JPanel panel = new JPanel();
JTP = new JTextPane();
JTP.setBackground(Color.black);
JTP.setForeground(Color.WHITE);
Lines = new JTextPane();
new BorderLayout();
panel.add(JTP, BorderLayout.WEST);
new BorderLayout();
panel.add(Lines, BorderLayout.EAST);
And there's is no errors. I know i not have giving you all the code but it's work.
JPanel panel = new JPanel();
This create a JPanel which uses a FlowLayout by default.
new BorderLayout();
This statement does nothing because you don't have a reference to the BorderLayout, to you can't use it as a layout manager on any panel.
But there is no need to use a BorderLayout for you panel, since a FlowLayout can display multiple components at one time. The problem is that the text pane needs a "preferred size" before it can be using with a layout manager.
For something simple why don't you just start with a JTextArea since it is easer to use. You can create the text area with code like:
JTextArea textArea = new JTextArea(5, 20);
and it will create a text area with a preferred size to display 5 lines of text with about 20 characters on each line.
Then you create two text areas and add them to your panel.
Of course any time you use a text area you should probably add it to a JScrollPane:
JScrollPane scrollPane = new JScrollPane( textArea );
panel.add( scrollPane );
and then add the scrollPane to the panel.
From the looks of it, you are only seeing one TextField because you aren't properly using BorderLayout.
EDIT: You said you want to change background colors, you can do that to a JTextField and a JTextArea. Check the docs! It's one line of code.
You are supposed to explicitly set the layout of the JPanel to BorderLayout like this :
JPanel panel = new JPanel(new BorderLayout());
OR
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
Even simpler, you don't need to use BorderLayout.
JPanel, by default, is assigned a FlowLayout which is capable of neatly handling more than one JComponent. Try using a JTextField or JTextArea instead of a JTextPane.
If you need to use a JTextPane because of a certain layout goal, then try the code examples I listed above.
Good luck. Don't be afraid to refer to the official Oracle docs/tutorials for Swing Layouts
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

Categories

Resources