lovely_rosa
asked on
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
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
ASKER
my question is how can I achieve that
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
one thisn is you ca dd some lines and spaces and position to the place where you need
ASKER
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);
}
}
);
}
}
I guees this padding shoul help you to put the text to the place you want
ASKER
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);
}
}
);
ASKER
but how can I get the curser position ??
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
as caret position cannot be bigger than the length of the string of text in the componenet
ASKER
thanks for you
You are always welcome.
can you post the code?