Java Swing - setting margins on TextArea with Line Border - java

As the title says, I am simply trying to set the margins (provide some padding) on a TextArea with a LineBorder set. Without setting the Border, .setMargins works fine. Here is the specific chunk of code.
aboutArea = new JTextArea("program info etc.....");
Border border = BorderFactory.createLineBorder(Color.BLACK);
aboutArea.setSize(400, 200);
aboutArea.setBorder(border);
aboutArea.setEditable(false);
aboutArea.setFont(new Font("Verdana", Font.BOLD, 12));
add(aboutArea);
I have tried each of these:
aboutArea.setMargins(10,10,10,10);
.getBorders(aboutArea).set(10,10,10,10);
UIManager.put("aboutArea.margin", new Insets(10, 10, 10, 10));
but nothing affects the margins after I apply the border, the padding is always 0. Any ideas how to set the padding on the textArea with the border?

What if you try adding a CompoundBorder , won't this do, this will give you almost same thing
JTextArea tarea = new JTextArea("program info etc.");
Border border = BorderFactory.createLineBorder(Color.BLACK);
tarea.setBorder(BorderFactory.createCompoundBorder(border,
BorderFactory.createEmptyBorder(10, 10, 10, 10)));

Related

JLabel multiline with HTML

I have a JLabel that I would like to contain a text that can go over multiple lines, and resizes if the window changes shape.
I've looked this up and most people seem to recommend wrapping the label text in HTML. This however does not make new lines for me.
My label is located in a JPanel and I suspect that the problem may be that my panel has misconfigured its border, and so the label text just continues beyond the panel border.
here is how the label looks inside the status panel
Here are the settings of the panel:
private final JPanel statusPanel = new JPanel(new FlowLayout());
statusPanel.setBackground(Color.white);
statusPanel.add(latestOrdreLabel);
this.add(statusPanel, new GridBagConstraints(0, 0, 6, 1, 1.0, 1.0
, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0));
Then the label is set up like this:
private final JLabel latestOrdreLabelResult = new JLabel();
String latestOrdreStatus = getBean().getLatestOrdreStatus(etelOrderInterface.getOrderId());
latestOrdreLabelResult.setText("<html>"+latestOrdreStatus+"</html>");
statusPanel.add(latestOrdreLabelResult);
In HTML, to down line your'e need to use <br/> instead of \n.
You can to use in replace String method, for example:
latestOrdreStatus.replace("\n", "<br/>");
You just need to resize the label.

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

How to change border width of JTextarea in java

How to change the border width? I want a lighter border.
Below is my try
// Set border
Border border = BorderFactory.createLineBorder(Color.LIGHT_GRAY);
textarea.setBorder(BorderFactory.createCompoundBorder(border, BorderFactory.createEmptyBorder(5, 5, 5, 5)));
The LineBorder of the text area is 1 pixel wide.
The JScrollPane also has a LineBorder 1 pixel wide.
Don't add the LineBorder to the textarea. Just use the EmptyBorder.

Designing an Etched Border

I want to design a GUI with theme of Black, but my etched border looks raised etched type.
I want to make it look lowered etched type. I can't find the color combination to do that. How would I do that?
It's looking like this, but the difference is that the background is Black.
Have you tried using a BevelBorder instead?
JPanel outerPanel = new JPanel();
outerPanel.setBackground(Color.black);
outerPanel.setPreferredSize(new Dimension(400, 400));
outerPanel.setLayout(new BorderLayout());
Border outsideBorder = BorderFactory.createEmptyBorder(5, 5, 5, 5);
Border insideBorder = BorderFactory.createBevelBorder(BevelBorder.LOWERED);
Border innerPanelBorder = BorderFactory.createCompoundBorder(outsideBorder , insideBorder );
JPanel innerPanel = new JPanel();
innerPanel.setBorder(innerPanelBorder);
innerPanel.setOpaque(false);
outerPanel.add(innerPanel);

JScrollPane Contents Not Showing

I have a JTextArea inside of a JPanel that is then placed into the JScrollPane. When the JPanel that contains the JScrollPane is first show the JScrollPane shows up but not the contents. As soon as the JFrame is resized the contents show up.
JTextArea area = new JTextArea(6, 20);
area.setText("Some test text");
JPanel panel = new JPanel(new BorderLayout());
panel.add(area, BorderLayout.CENTER);
JScrollPane pane = new JScrollPane();
pane.setBounds(20, 20, WIDTH - 40, 300 - 40);
pane.setPreferredSize(new Dimension(WIDTH - 40, 300 - 40));
add(pane);
pane.setViewportView(panel);
pane.setBounds(20, 20, WIDTH - 40, 300 - 40);
pane.setPreferredSize(new Dimension(WIDTH - 40, 300 - 40));
Those two lines of code doen't make sense (although they are not the cause of your problem)
The first line is used when you are using a "null layout".
The second is used when you are using layout managers.
They should not be used together.
The second is preferred since you should be using layout managers.
In the application different JPanels are swapped out in a manner similar to a slide-show. So something like this would be found in the code:
panel.remove(slide1);
panel.add(slide2);
panel.repaint();
The problem being that all of the contents of the second slide, slide2, would not show up. The solution is to add
frame.validate();
Where frame is the parent window of panel.
new JScrollPane(panel);
I believe that you need to add the panel to the scroll pane constructor.

Categories