• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 275
  • Last Modified:

Add in specific position in text area

I  create  simple editor  ,  when the user click one of sub-menu  another frame appear

which ask the user  to enter some information , when the user click add the information will be

added to JTextArea in specific position  (the position is in the place of curser   )  by the way the
 JtextArea is in another fram

please look at  the picture to understand the problem
P.png
0
lovely_rosa
Asked:
lovely_rosa
  • 7
  • 5
1 Solution
 
for_yanCommented:
So waht is your question?
can you post the code?
0
 
lovely_rosaAuthor Commented:
my question is  how  can I  achieve  that  
0
 
for_yanCommented:
JTextArea has methods setCaretPosition getCaretPosition() but it should be insie the text - if you don;t have any text you cannot set the caret position
one thisn is you ca dd some lines and spaces and position to the place where you need
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
lovely_rosaAuthor Commented:
here is  the  code  for  my sub- menu  

class AsserAlways extends AbstractAction
  {

     public AsserAlways() {
      super("Use Always Operator ");
    }
      public void actionPerformed(ActionEvent ev)
      {
          blackline = BorderFactory.createLineBorder(Color.black);
          loweredbevel = BorderFactory.createTitledBorder(
                          loweredbevel, "Example : // 1.2.a  Assert  Always (condition))",
                          TitledBorder.CENTER,
                          TitledBorder.BELOW_BOTTOM);
         // loweredbevel = BorderFactory.createLoweredBevelBorder();

          JFrame fre = new JFrame();
          fre.setName("Always Assertion Operator Form");
          fre.setSize(300, 300);
          JPanel panel = new JPanel ();
            JPanel panel3 = new JPanel ();
              JLabel l3 = new JLabel ();
              l3.setText("Assertion use always ");
              l3.setBorder(loweredbevel);
          panel.setBackground(Color.ORANGE );
          panel.setLayout(new GridLayout(1,2));
          JPanel panel2 = new JPanel ();
          panel2.setBackground(Color.ORANGE  );

          panel3.setLayout(new GridLayout(1,1));
          panel3.add(l3);
          panel.setLayout(new GridLayout(1,2));
         panel2.setLayout(new GridLayout(2,2));
         //-----------------------------------------
          JLabel l1 = new JLabel ();
          l1.setText("Enter The Label ");
          l1.setBorder(blackline);
           f = new JTextField();
           f.setText("1.2.A");
          f.setSize(10, 10);
           f.setBorder(blackline);
            panel.add(l1);
          panel.add(f);
      //----------------------------------------------
           JLabel l2 = new JLabel ();
          l2.setText("Enter The Condition ");
           l2.setBorder(blackline);
           f2 = new JTextField();
            f2.setText("(condition)");
          f2.setSize(10, 10);
          f2.setBorder(blackline);
          panel2.add(l1);
          panel2.add(f);
            panel2.add(l2);
          panel2.add(f2);


      //-----------------------------------------------
          JButton jb = new JButton ();
          jb.setBounds(10, 10, 10, 10);
          jb.setText("Add");


       //-------------------------------------

         fre.getContentPane().add(panel3,BorderLayout.NORTH);
         fre.getContentPane().add(panel2,BorderLayout.CENTER);
         fre.getContentPane().add(jb,BorderLayout.SOUTH);



           fre.setVisible(true);



          jb.addActionListener(
                  new ActionListener ()
          {
             public void actionPerformed(ActionEvent ev)
             {
                 String strin="//"+"   " +  f.getText()+"  "+"Assert"+" "+"Always"+"  "+"("+f2.getText()+" )";
   textComp.insert(strin, 0);
             }
          }
            );

         
      }

  }

Open in new window

0
 
for_yanCommented:
0
 
for_yanCommented:
I guees this padding shoul help you to put the text to the place you want
0
 
lovely_rosaAuthor Commented:
here  in the  code  I  add action listener  to the button its name (jb)  when click add button the string will be post  in texcomp   which is the jtextarea   but  is not  in  the cusrer position   I want  to add in the curser position
jb.addActionListener(
                  new ActionListener ()
          {
             public void actionPerformed(ActionEvent ev)
             {
                 String strin="//"+"   " +  f.getText()+"  "+"Assert"+" "+"Always"+"  "+"("+f2.getText()+" )";
   textComp.insert(strin, 0);
             }
          }
            );

Open in new window

0
 
lovely_rosaAuthor Commented:
but  how  can I  get  the  curser position ??
0
 
for_yanCommented:
getCaretPosition() - it will return the charcater number witihin the string which represents the text in JTextArea
0
 
for_yanCommented:
But there would not be any caret position until theere is text in JTextArea, this position would be 0,
as caret position cannot be bigger than the length of the string of text in the componenet
0
 
lovely_rosaAuthor Commented:
thanks  for  you
0
 
for_yanCommented:
You are always welcome.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now