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.
Related
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
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.
I have a JPanel of null layout called MainPanel. Onclick of a button I am adding JTextpane on mainPanel. On first click I am creating a textpane of background color white. On second click I am creating another textpane of color blue. What I want is to place the blue textpane upon the white textpane, but the blue textpane is going behind the white textpane. How can I place it on white pane?
Code is very simple here. Onclick I am creating a new JTextpane, setting dimensions to it and placing it on the mainPanel.
Placing a sample screenshot which describes the issue better. Here the blue textpane has gone behind white textpane. I want it above white texpane. How do I do that?
If you want to stick with your current Components, (I think you should use kellax's solution, but I don't know if there's an extra requirement that's forcing you to use your current approach) you can look into Container.setComponentZOrder(Component comp, int index) to directly determine the order in which Components are displayed.
You will have to replace your JPanel "MainPanel" with a LayeredPanel.
Then you can say:
JLayeredPane mainPanel = new JLayeredPane();
JTextPane whitePane = new JTextPane("White text pane on top");
JTextPane bluePane = new JTextPane("Blue text pane behind");
mainPanel.add(whitePane, 2, 0);
mainPanel.add(bluePane, 1, 0);
Edit:
You can read more about the LayeredPane here: LayeredPane
Hello Im trying to create a JTextArea which once the data gets to large to hold the data the scroll bars can allow me to scroll however ATM there are no scroll bars just a little square box and the data is pushing the rest of the GUI up.
This is the code for the textArea and the scrollbars, p3 is the panel im trying to add the scrollbar and textArea too.
//TEXT AREA
final JTextArea dataTextField = new JTextArea();
dataTextField.setSize(750,200);
dataTextField.setLineWrap(true);
dataTextField.setEditable(false);
dataTextField.setVisible(true);
JScrollPane scroll = new JScrollPane (dataTextField);
scroll.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setVisible(true);
p3.add(dataTextField);
p3.add(scroll);
//p3.add(dataTextField);
p3.add(scroll);
A swing component can only have a single parent. When you add the dataTextField to the panel you remove it from the scrollpane. Get rid of that line of code.
Do not use setSize (750, 200) on dataTextField. Just pass number of columns and rows to constructor of JTextArea.
I want to reduce the size between the components with in the Formatting group (left side on the image). How to do this?
JPanel formattingGroup = createGroupWithName("Formatting");
formattingGroup.setMinimumSize(new Dimension(250, 20));
formattingGroup.setLayout(new GridLayout(5, 0));
add(formattingGroup);
final JCheckBox showSurface = new JCheckBox("Show surface");
showSurface.setSelected(true);
formattingGroup.add(showSurface);
final JCheckBox showTerrain = new JCheckBox("Show terrain");
showTerrain.setSelected(true);
formattingGroup.add(showTerrain);
final JCheckBox showVehicleStatus = new JCheckBox("Show vehicle status");
showVehicleStatus.setSelected(true);
formattingGroup.add(showVehicleStatus);
JPanel pnl = createGroupWithName("Depth Stretch");
formattingGroup.add(pnl);
JSlider slider = new JSlider(0, 10);
pnl.add(slider);
When using a GridLayout all components are made the same size.
You are adding a JPanel with a TitledBorder and a JSlider to the grid. Therefore the checkboxes will take the same vertical height as that panel.
You need to use a different layout manager for the panel. Maybe a vertical BoxLayout.
You might look at available size variants, discussed in Resizing a Component.
Use gridbaglayout because that gives you the opportunity to give weights, to columns or rows and set spacing and padding values.
I made a Swing application that contains out of 12 Frames and they all are made with GridBagLayout.
I also tried other before that but they all had limits. That's where the GridBagLayout kicks in. It's a bit harder in begin to understand how it works, but once you get feeling with it, it really is best thing to get the components where you want.
If you want i'll give you a cool example of a frame created with GridBagLayout.