0

I'm currently working with JTextArea. My question is what should I do with the JTextArea so I can set a specific size and also if the text its too much to fit then add a slider on the JTextArea?

First example image enter image description here

This is the method that is creating my JTextArea:

public JPanel create_Output_Panel(){

    //Setup Main Panel of the Chat Application
    JPanel panel = new JPanel();
    title = BorderFactory.createTitledBorder("Server Screen");
    title.setTitleJustification(TitledBorder.CENTER);
    title.setTitleColor(Color.BLACK);
    panel.setBorder(title); //Set title to the Panel

    panel.setLayout(new BorderLayout());

    //Store IP Address in a String variable
    String ip_Address = new ChatServerViewer().getServer_IP_Addres();

    JLabel label = new JLabel("You are connected to Server : " + ip_Address, SwingConstants.CENTER);
    label.setFont(new Font("Serif", Font.PLAIN, 17));
    panel.add(label,BorderLayout.NORTH);

    JLabel label2 = new JLabel("Use .bye to log-out ", SwingConstants.CENTER);
    label2.setFont(new Font("Serif", Font.BOLD, 20));
    panel.add(label2);

    //CREATE TEXT AREA FOR THE USER MESSAGES
    textArea = new JTextArea(12,1);
    textArea.setFont(new Font("Serif", Font.PLAIN, 25));
    textArea.setEditable(false); //Block User from Editing the Text Area

    textArea.setText("\n\n     Server:");
    textArea.append("\n          Hello User !");

    panel.add(textArea, BorderLayout.SOUTH);

    return panel;
}
nkr
  • 3,026
  • 7
  • 31
  • 39
Loizos Vasileiou
  • 674
  • 10
  • 37
  • 1- Look to the constructor of JTextArea, it allows you to specify the rows/columns in a platform independent way; 2- Embedded the JTextArea in a JScrollPane – MadProgrammer Jan 04 '18 at 00:35
  • I have tried using it in different ways such as (12,1), (12,10), (40,40), (100,0 ) – Loizos Vasileiou Jan 04 '18 at 00:42
  • You need a layout manager which can honour the preferred size of the text area, maybe GridBagLayout – MadProgrammer Jan 04 '18 at 00:44
  • You need to remember, this information is all just hints that the system can use to make determinations about how components should be laid out, the system can also ignore them if it needs to – MadProgrammer Jan 04 '18 at 00:48

1 Answers1

0

I used the following method to fix the problem!!

/**
 * Reference : https://stackoverflow.com/questions/10177183/java-add-scroll-into-text-area
 * Reference : https://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html#sizing
 * @return a panel of a JTextArea inside an ScrollPane
 */
public JPanel create_Output_Panel(){

    //Setup Main Panel of the Chat Application
    JPanel panel = new JPanel();
    title = BorderFactory.createTitledBorder("Server Screen");
    title.setTitleJustification(TitledBorder.CENTER);
    title.setTitleColor(Color.BLACK);
    panel.setBorder(title); //Set title to the Panel

    panel.setLayout(new BorderLayout());

    //Store IP Address in a String variable
    String ip_Address = new ChatServerViewer().getServer_IP_Addres();

    JLabel label = new JLabel("You are connected to Server : " + ip_Address, SwingConstants.CENTER);
    label.setFont(new Font("Serif", Font.PLAIN, 17));
    panel.add(label,BorderLayout.NORTH);

    JLabel label2 = new JLabel("Use .bye to log-out ", SwingConstants.CENTER);
    label2.setFont(new Font("Serif", Font.BOLD, 20));
    panel.add(label2);

    //CREATE TEXT AREA FOR THE USER MESSAGES
    textArea = new JTextArea(15,0);
    textArea.setFont(new Font("Serif", Font.BOLD, 20));
    textArea.setEditable(false); //Block User from Editing the Text Area

    textArea.setLineWrap(true);

    JScrollPane areaScrollPane = new JScrollPane(textArea);
    areaScrollPane.setVerticalScrollBarPolicy(
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

    System.out.println("textArea height = " + textArea.getSize().width);
    textArea.setText("    Server>>>   ");
    textArea.append("Connection Succesful !");

    panel.add(areaScrollPane, BorderLayout.SOUTH);

    return panel;
}
Loizos Vasileiou
  • 674
  • 10
  • 37