Is there anyway to have a JOptionPane window without the text area but with only a scroll bar,
JTextArea listBox = new JTextArea(aLineFromFile);
JScrollPane scroll = new JScrollPane(listBox);
listBox.setLineWrap(true);
listBox.setWrapStyleWord(true);
scroll.setPreferredSize(new Dimension(200, 400));
JOptionPane.showMessageDialog(null, scroll, "Dictionary enteries", JOptionPane.PLAIN_MESSAGE);
I don't want the white background behind the text I just want the scroll bar.
Or anyway to make the text in the text area uneditable.
"Or anyway to make the text in the text area uneditable."...
Try:
listBox.setEditable(false);
Also, you might be better off using a JLabel rather than a JTextArea.
Related
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.
I'm making a simple GUI and have a problem.
This is my code :
JFrame jFrame = new JFrame();
jFrame.setTitle("Simple Editor");
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setLocation(50,50);
jFrame.setResizable(true);
Box box = new Box(BoxLayout.Y_AXIS);
JTextArea jTextArea = new JTextArea();
jTextArea.setPreferredSize(new Dimension(470,500));
JLabel jLabel = new JLabel();
box.add(jTextArea);
box.add(jLabel);
jLabel.setText("Font type : " + Main.fontType + " font size : " + Main.size
+ " background color : " + Main.backgroundColor
+ " font color : " + Main.fontColor);
jFrame.setContentPane(box);
jFrame.pack();
jFrame.setVisible(true);
When I typing something in JTextArea, text in JLabel is moving. I can't figure out how to solve this. Maybe some component between them? Any advice and help is welcome.
This looks like an artifact of how the Box is calculating sizes and locations. Note that some components and layout managers do not use setPreferredSize, or only take it as a hint, or use it as only one part of a computation, or etc. so it cannot be depended upon as a reliable method to set the size of a component.
In this case, I would hypothesize what is going on is something like: BoxLayout generally uses minimum/maximum sizes, not preferred sizes, and the min/max of a JTextArea is computed based on its text content. As the text changes, the size is recalculated so the layout changes too.
In general if you have a text area, you should put it in a JScrollPane instead:
Box box = new Box(BoxLayout.Y_AXIS);
JTextArea jTextArea = new JTextArea();
JScrollPane jScrollPane = new JScrollPane(jTextArea);
jScrollPane.getViewport().setPreferredSize(new Dimension(470,500));
JLabel jLabel = new JLabel();
box.add(jScrollPane);
box.add(jLabel);
This way when the text content changes in the JTextArea it can simply do its thing, recalculating its size, and flow out the side of the scroll pane.
Also see How to Use Scroll Panes, How to Use Text Areas.
Per Andrew's comment, here are a couple ways to set the initial size of the scroll pane which are perhaps more reliable than setting the viewport's preferred size explicitly:
// specify rows & columns
JTextArea jTextArea = new JTextArea(20, 20);
// specify preferred scrollable viewport size
JTextArea jTextArea = new JTextArea() {
#Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(470,500);
}
};
jTextArea.addKeyListener(new KeyAdapter() {
#Override
public void keyTyped(KeyEvent e) {
jLabel.setText(jTextArea.getText());
}
});
where,
jTextArea - your name object of JTextArea class
jLabel - your name object of JLabel class
You add text in the textarea and text in the label is changing. I think, this code help you to decide your problem.
Hi I have made a JTextArea but it is not scrolling, can someone please tell me why?
JTextArea textArea = new JTextArea(2, 0);
textArea.setText("sdsdsd \n dfdfdf \n dsdsdsdsd \n dsdsdsd \n sdsdsdsd");
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
panel.add(textArea);
Also, I would like it to auto scroll down, when new content gets added to, show it only shows the last 2 lines automatically, if possible.
Thanks.
Use,
panel.add(scrollPane);
not
panel.add(textArea);
Add the JScrollPane to the JPanel, not the JTextArea.
To Scroll to the bottom, see this answer.
at now i am trying to add a label beside a frame. And i have a text for the label and want to align it to center but it fails and always be left-align. Please help me!
Simulation sm = new Simulation(dm);
JFrame simulation = new JFrame();
simulation.setTitle("Traffic light and Car park Siumulation");
simulation.setSize(800,600);
simulation.setResizable(false);
simulation.setVisible(true);
simulation.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
simulation.add(sm, BorderLayout.CENTER);
//Carpark label
JLabel carparklb = new JLabel("abcd");
carparklb.setPreferredSize(new Dimension(200,600));
carparklb.setVerticalTextPosition(JLabel.CENTER);
carparklb.setHorizontalTextPosition(SwingConstants.CENTER);
simulation.add(carparklb, BorderLayout.EAST);
From the Java Docs
setVerticalTextPosition(int textPosition)
Sets the vertical position of the label's text, relative to its image.
Try JLabel.setVerticalAlignment and JLabel.setHorizontalAlignment instead
Try:
carparklb.setAlignmentY(JLabel.CENTER_ALIGNMENT);
carparklb.setAlignmentX(JLabel.CENTER_ALIGNMENT);
This will align the text to the center of the label both horizontally and vertically. If you only wanted to center it horizontally, you can try either:
JLabel carparklb = new JLabel("abcd", JLabel.CENTER_ALIGNMENT);
or
carparklb.setAlignmentX(JLabel.CENTER_ALIGNMENT);
Use html code as:
"<html><font size=\"5\"><P ALIGN =\"CENTER\">ARGHYA</P></font></html>"
and use it as the String in the label.
I have a JFrame with JScrollPane in it. I have JPanel inside a scrollPane. And add multiline labels in it.
Everything is ok with multiline labels. I enclose my text in <HTML>..</HTML> tags.
And labels display its wrapped text.
"..." means long multiline text.
The problem is that useless area is displayed in the bottom.
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(300, 300));
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
label1.setText("<html>" + "..." + "</html>");
panel.add(label1);
label2.setText("<html>" + "..." + "</html>");
panel.add(label2);
JScrollPane scroll = new JScrollPane(panel);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
frame.setContentPane(scroll);
frame.pack();
frame.setVisible(true);
EDIT.
So I have to set preferred size for inner JPanel. After that scrollPane draws its content(shows scrollbars) as its content has this fixed "inner panel preffered size".
If I won't set preferred size for the panel, JLabels wouldn't wrap the text.
After being layed out by the layout manager inner panel's size grows and became larger than previously set preferred size. Panel grows itself, its ok, I see wrapped text of labels in it. But scrollpane behaves incorrectly. It paints scroll as inner panel is still of prefferred size size. So I need correct resizing behaviour for JScrollPane.
use JTextPane or JEditorPane instead of JPanel contains bunch of JLabels
JTextPane or JEditorPane supporting stylled text or Html <= 3.2 for Java6
theoretically you can use JList, instead of Jlabels, but in this case you have to call for setPreferredScrollableViewportSize(new Dimension) same as for JPanel in the JScrollPane
EDIT
then use Highlighter
use built-in reader/writer for JTextComponents