public class DailogDemo
{
private JDialog chatdailog;
private JTextArea chatHistory;
private JScrollPane mScrollMessage;
DailogDemo()
{
chatdailog=new JDialog();
chatdailog.setSize(300, 400);
chatHistory=new JTextArea();
chatHistory.setPreferredSize(new Dimension(150,100));
mScrollMessage=new JScrollPane();
mScrollMessage.add(chatHistory);
mScrollMessage.setBounds(4, 10, 150, 100);
mScrollMessage.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
chatdailog.add(mScrollMessage);
chatdailog.show();
}
public static void main(String args[])
{
new DailogDemo();
}
}
In the above code, I can't able to see the JTextArea in JScrollPane. Does anybody know what I am doing wrong?
use JTextArea(int rows, int columns)
don't set and remove chatdailog.setSize(300, 400);
don't set and remove chatHistory.setPreferredSize(new Dimension(150,100));
don't set and remove mScrollMessage.add(chatHistory); use JScrollPane scrollPane = new JScrollPane(textArea); instead
don't set and remove mScrollMessage.setBounds(4, 10, 150, 100);
don't set and remove chatdailog.show(); use chatdailog.setVisible(true);
add code line chatdailog.pack() before line chatdailog.setVisible(true);
if is there another parent for this JDialog wrap chatdailog.setVisible(true); into invokeLater()
If you have a layout, you can use new JTextArea(24, 32) and pack() to get a nice arrangement.
set size for JTextArea
chatHistory.setSize(new Dimension(width,height));
Related
I made a program and I want to add a JScrollPane to a JTextArea (but it doesn't show up).
Here's the code (or at least everything that has to deal with the JTextArea / JScrollPane, the whole code is a lot):
static JPanel contentPane; // This one got initialised in the constructor
static JTextArea tarMessages;
public void addTextArea{
tarMessages = new JTextArea();
tarMessages.setForeground(Color.WHITE);
tarMessages.setBackground(new Color(0, 0, 0, 0));
tarMessages.setEditable(false);
tarMessages.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
tarMessages.setBounds(600, 124, 200, 192);
tarMessages.setOpaque(false);
/*DefaultCaret dlcMessages = (DefaultCaret)tarMessages.getCaret();
dlcMessages.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);*/
tarMessages.addMouseListener(new MouseAdapter(){
#Override
public void mouseClicked(MouseEvent e) {
requestFocus();
}
});
JScrollPane scpMessages = new JScrollPane(tarMessages);
scpMessages.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scpMessages.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scpMessages.setPreferredSize(new Dimension(10, 192));
scpMessages.setEnabled(true);
contentPane.add(scpMessages);
contentPane.add(tarMessages);
}
Thank you for helping. Have nice holidays.
JScrollPane scpMessages = new JScrollPane(tarMessages);
...
contentPane.add(scpMessages);
contentPane.add(tarMessages);
A Swing component can only have a single parent.
First you add the text area to the scroll pane, which is correct.
But then you remove it from the scroll pane when you add it to the content pane.
Get rid of:
///contentPane.add(tarMessages);
Also, when you create the text area use code like:
tarMessages = new JTextArea(5, 20);
This will specify the rows/columns of the text area so it can size itself appropriately.
Don't use setBounds(...)
Executing this code produces very strange behavior.
When running, try resizing and typing to see what I mean.
import java.awt.BorderLayout;
import javax.swing.*;
public class FrameWithScrollPanel extends JFrame {
public static void main(String[] args) {
FrameWithScrollPanel myFrame = new FrameWithScrollPanel();
}
public FrameWithScrollPanel()
{
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JTextArea textArea1 = new JTextArea(5, 30);
JTextArea textArea2 = new JTextArea(5, 30);
JPanel jPanel = new JPanel();
jPanel.setSize(400,400);
jPanel.setLayout(new BorderLayout());
jPanel.add(textArea1, BorderLayout.NORTH);
jPanel.add(textArea2, BorderLayout.SOUTH);
JScrollPane scrollPane = new JScrollPane();
scrollPane.add(jPanel);
getContentPane().add(scrollPane, BorderLayout.CENTER);
pack();
setVisible(true);
}
}
Now, replace these 2 lines :
JScrollPane scrollPane = new JScrollPane();
scrollPane.add(jPanel);
With this one line and the behavior is as expected.
JScrollPane scrollPane = new JScrollPane(jPanel);
Based on the documentation the JScrollPane constructor accepts a Component and so does the add().
Why the difference in behavior?
This is wrong:
scrollPane.add(jPanel);
Since you're replacing the JScrollPane's all important viewport with this add, preventing it from functioning. You should instead be adding this to the JScrollPane's viewport as per the JScrollPane tutorial and JScrollPane API:
scrollPane.setViewportView(jPanel);
or
scrollPane.getViewport().add(jPanel);
Moral of the story: when in doubt, read the docs.
Note that if you pass the jPanel into the JScrollPane's constructor,
JScrollPane scrollPane = new JScrollPane(jPanel);
it automatically places the component into the viewport for you.
Per the API:
public JScrollPane(Component view)
Creates a JScrollPane that displays the contents of the specified component, where both horizontal and vertical scrollbars appear whenever the component's contents are larger than the view.
Parameters:
view - the component to display in the scrollpane's viewport
This is the code I am struggling with. It is refusing to amend the JTextArea with the new text. I create the window and set it to visible in the main function of the project.
Thanks ahead.
EDIT:
By refusing, I mean the JTextArea will simply not display the text. It just stays empty. I'm not getting and error or exception. It is all logical.
class Window extends JFrame{
protected JTextArea text;
public Window() {
setTitle("Create a list of names");
setSize(500,400);
Container containerPane = getContentPane();
JPanel jp = new JPanel();
text = new JTextArea(10,50);
text.setPreferredSize(new Dimension(256,256) );
text.setEditable(false);
JScrollPane scrollText = new JScrollPane(text);
scrollText.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
jp.add(scrollText);
containerPane.add(jp, BorderLayout.CENTER);
text.append("Test");
}
public static void main(String[] args) {
Window w = new Window();
w.setVisible(true);
}
}
The column width of 50 is greater than the width of the frame so the added text appears offscreen. Reduce its value to fit the parent window
textArea = new JTextArea(10, 35);
Don't use setPrerredSize. Let the layout manager do its job and call pack after all components have been added.
Here is my code. The button is the same size as the FlowLayout. How can I make the button smaller?
public class javalearning extends JFrame{{
FlowLayout f = new FlowLayout();
this.setSize(600,600);
JFrame j = new JFrame();
this.setTitle("this is a tittle");
JButton button = new JButton();
button.setText("Button");
this.add(button);
button.setBounds(10, 10, 10, 10);
this.setVisible(true);
}
}
I think you forget to setLayout, it works fine when I do. Don't use setBounds and put it in a javalearning constructor and I also suggest you setDefaultCloseOperation like
public javalearning() {
FlowLayout f = new FlowLayout();
this.setLayout(f);
this.setSize(600, 600);
this.setTitle("this is a tittle");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton();
button.setText("Button");
this.add(button);
// button.setBounds(10, 10, 10, 10);
this.setVisible(true);
}
public static void main(String[] args) {
javalearning m = new javalearning();
}
Finally, by convention, Java class names start with a capital letter and are camel case. Something like JavaLearning would follow that convention.
You never set the layout manager (FlowLayout) to the frame, therefore the JFrame is still using it's default layout manager of BorderLayout...
Try using something more like...
FlowLayout f = new FlowLayout();
setLayout(f);
this.setTitle("this is a tittle");
JButton button = new JButton();
button.setText("Button");
this.add(button);
this.pack();
this.setVisible(true);
instead...
Take a closer look at Laying Out Components Within a Container for more details
FlowLayout will lay out Components left-to-right (or right-to-left) wrapping them if required. If you wish to explicitly set the size of each JButton you should use setPreferredSize rather than setSize or setBounds as layout managers typically make use of the minimum, preferred and maximum sizes when performing a layout.
button.setBounds(x, y, height, width);
you can give height and width less than 10!
also you can use GridLayout for smaller buttons in one button with for loop
I am trying to put a text area onto a dialog box using Java Swing. I have a problem of setting the size of this JTextArea. The width of the text area is always equal to the whole width of the window and stretches with the window if I resize it.
private void arrangeComponents() {
JTextArea textArea = new JTextArea();
JPanel outerPanel = new JPanel();
outerPanel.setLayout(new BoxLayout(outerPanel, BoxLayout.PAGE_AXIS));
JScrollPane scrollPane = new JScrollPane(textArea);
outerPanel.add(scrollPane, BorderLayout.CENTER);
Container contentPane = getContentPane();
contentPane.add(outerPanel, BorderLayout.CENTER);
}
I want the JTextArea to be horizontally aligned to the centre of the window and does not change its size.
What did I do wrong?
Use the JTextArea(int rows, int columns) constructor that specifies rows and columns, as shown here, and don't neglect to pack() the enclosing Window.
outerPanel.add(scrollPane, BorderLayout.CENTER);
A BoxLayout doesn't take constraints, so the BorderLayout.CENTER is unnecessary.
The problem is that a BoxLayout respects the maximum size of the component which for a scrollpane is set very large.
Instead of using a BoxLayout, just use a panel with a FlowLayout.
Run the example below to see what you are currently doing. Then comment out the setLayout(...) statement and run again. By default the panel uses a FlowLayout so you will get what you want.
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
public class SSCCE extends JPanel
{
public SSCCE()
{
setLayout( new BoxLayout(this, BoxLayout.PAGE_AXIS));
JTextArea textArea = new JTextArea(5, 30);
JScrollPane scrollPane = new JScrollPane(textArea);
//scrollPane.setMaximumSize( scrollPane.getPreferredSize() );
add(scrollPane);
}
private static void createAndShowUI()
{
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new SSCCE() );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
Or if you really want to keep the BoxLayout then leave keep the setLayout(...) statement and then set the maximum size equal to the preffered size. Many people will say you should never invoke a "setXXX()" method directly and instead you should override the setMaximumSize() method of the scrollpane to just return the preferred size.
Note, when testing these two solutions make sure you make the window smaller than the scrollpane to see how each layout works differently.
i found this from a simple coding site. This code sample may be useful for you.
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class JTextAreaTest {
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("JTextArea Test");
frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String text = "A JTextArea object represents a multiline area for displaying text. "
+ "You can change the number of lines that can be displayed at a time, "
+ "as well as the number of columns. You can wrap lines and words too. "
+ "You can also put your JTextArea in a JScrollPane to make it scrollable.";
JTextArea textAreal = new JTextArea(text, 5, 10);
textAreal.setPreferredSize(new Dimension(100, 100));
JTextArea textArea2 = new JTextArea(text, 5, 10);
textArea2.setPreferredSize(new Dimension(100, 100));
JScrollPane scrollPane = new JScrollPane(textArea2,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
textAreal.setLineWrap(true);
textArea2.setLineWrap(true);
frame.add(textAreal);
frame.add(scrollPane);
frame.pack();
frame.setVisible(true);
}
}
Just call that method for ur text area: setLineWrap(true);
If JTextArea is initializated
JTextArea text = new JTextArea(int rows, int columns)
you just call the method text.setLineWrap(true)
then text'size is fixed.