Search code examples
javaswingjframejtextareakeylistener

How to set Scrollable TextArea in Java for group Layout?


I have a problem. Every time, when I add more Symbols(Numbers) to TextArea, it doesn't make it scrollable. EDIT: now it works as I want. I only needed to change 2 words. Thanks.

class NumOnly extends KeyAdapter {  

    private String Atlauts = "[^0-9]";  //Allowed Buttons.
    public void keyReleased(KeyEvent e) {   //Key event. What happens when the button is pressed
        String curText = ((JTextComponent) e.getSource()).getText();  //Current text
        curText = curText.replaceAll(Atlauts, ""); 

        ((JTextComponent) e.getSource()).setText(curText);  
    }  
}  

public class kursadarbs{

    public static void main(String[] args) {

        JFrame frame= new JFrame();
        JPanel panel= new JPanel();

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //components
        JLabel label1= new JLabel("Insert first number: ");

        final JTextField textbox1= new JTextField(10);
        textbox1.addKeyListener(new NumOnly());

        JLabel label2= new JLabel("Insert second number: ");

        final JTextField textbox2= new JTextField(10);
        textbox2.addKeyListener(new NumOnly());

        JButton button= new JButton("Calculate");

        final JTextArea textarea= new JTextArea(20,20); //Result is stored in there
        textarea.setEditable(false);
        textarea.setLineWrap(true);
        JScrollPane scroll= new JScrollPane(textarea);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        textarea.setWrapStyleWord(true);
        textarea.setBorder(new TitledBorder(new EtchedBorder(), "Result"));




        GroupLayout groupLayout = new GroupLayout(panel);
        panel.setLayout(groupLayout);  
        groupLayout.setAutoCreateGaps(true);      
        groupLayout.setAutoCreateContainerGaps(true); 

        GroupLayout.SequentialGroup HorSGroup= groupLayout.createSequentialGroup(); 
        GroupLayout.SequentialGroup VerSGroup= groupLayout.createSequentialGroup(); 

        GroupLayout.ParallelGroup HParallelGroup1= groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING);
        GroupLayout.ParallelGroup HParallelGroup2= groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING);
        GroupLayout.ParallelGroup HParallelGroup3= groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING);

        HParallelGroup1.addComponent(label1); //adding components to the group
        HParallelGroup1.addComponent(label2);
        HParallelGroup2.addComponent(textbox1);
        HParallelGroup2.addComponent(textbox2);
        HParallelGroup2.addComponent(scroll);
        HParallelGroup3.addComponent(button);

        HorSGroup.addGroup(HParallelGroup1);
        HorSGroup.addGroup(HParallelGroup2);
        HorSGroup.addGroup(HParallelGroup3);

        GroupLayout.ParallelGroup VerPGroup1= groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE); //Vertical group
        GroupLayout.ParallelGroup VerPGroup2= groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE);
        GroupLayout.ParallelGroup VerPGroup3= groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE);

        VerPGroup1.addComponent(label1); //adding components to groups
        VerPGroup1.addComponent(textbox1);
        VerPGroup1.addComponent(button);
        VerPGroup2.addComponent(label2);
        VerPGroup2.addComponent(textbox2);
        VerPGroup3.addComponent(scroll);

        VerSGroup.addGroup(VerPGroup1);
        VerSGroup.addGroup(VerPGroup2);
        VerSGroup.addGroup(VerPGroup3);

        groupLayout.setHorizontalGroup(HorSGroup);
        groupLayout.setVerticalGroup(VerSGroup);




        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (int) (dimension.getWidth()/4);
        int y = (int) (dimension.getHeight()/4);
        frame.setLocation(x, y); //Places the program almost in the middle

        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
               //what happens when "calculate" is pressed.
                String first = null, second = null; 
                int first1=0, second1=0;

               first= textbox1.getText();  //getting textbox1 value.
               second= textbox2.getText();

               if(!first.isEmpty() && !second.isEmpty()) //If fields are not empty..
               {
                   first1= Integer.parseInt(first);  //string to integer.
                   second1= Integer.parseInt(second);

                   if(first1<second1){ //Check, if the first number is bigger than second.
                   System.out.println(first1);                 
                   textarea.append(first+"\n");
                   }// Ja ir pareizi 
                   else 
                   {
                       JOptionPane.showMessageDialog(null,"Incorrect data. " );
                   }
               } else 
               {
                   JOptionPane.showMessageDialog(null,"Incorrect data." );
               }
            }
        });


        frame.add(panel); //add the panel
        frame.setSize(500, 500); //program size in pix
        frame.setResizable(false); //putting that the frame can't change size
        frame.setTitle("Kursa darbs");
        frame.setVisible(true);


    }

}

Well, The main problem is the Textarea. As you can see, English is not my native language and I'm a beginner for JFrame. I have tried everything... Please help.

Thank you Already.


Solution

  • Instead of adding the JTextArea to the groups, add the JScrollPane that contains it!

        //HParallelGroup2.addComponent(textarea);
        HParallelGroup2.addComponent(scroll);
        HParallelGroup3.addComponent(button);
    
        HorSGroup.addGroup(HParallelGroup1); //Horizontālās grupas tiek pievienotas sakārtotajai grupai
        HorSGroup.addGroup(HParallelGroup2);
        HorSGroup.addGroup(HParallelGroup3);
    
        GroupLayout.ParallelGroup VerPGroup1= groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE); //Izveido Vertikālās grupas.
        GroupLayout.ParallelGroup VerPGroup2= groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE);
        GroupLayout.ParallelGroup VerPGroup3= groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE);
    
        VerPGroup1.addComponent(label1); //Pievieno objektus
        VerPGroup1.addComponent(textbox1);
        VerPGroup1.addComponent(button);
        VerPGroup2.addComponent(label2);
        VerPGroup2.addComponent(textbox2);
        //VerPGroup3.addComponent(textarea);
        VerPGroup3.addComponent(scroll);