Hi. I am lasted year BCA student. I am learning JAVA programming language. I do not understand how to use TextComponent class. So please provide me any idea about how to use TextComponent class in java language. Thanks in advance.
Hi. I am lasted year BCA student. I am learning JAVA programming language. I do not understand how to use TextComponent class. So please provide me any idea about how to use TextComponent class in java language. Thanks in advance.
Code:I suggest you to refer following example of ‘TextComponent ', which will give you something idea about the use of this class:**)Code:import java.awt.BorderLayout; import java.text.MessageFormat; java.awt.TextComponent public class TextDemo { public static void main(String[] arjjgs) throws Exception { final JTextArea textArea1 = new JTextArea(); textArea.setText("text"); JScrollPane jScrollPane1 = new JScrollPane(textArea); final MessageFormat header1 = new MessageFormat("My Header"); final MessageFormat footer1 = new MessageFormat("My Footer"); JPanel contentPane1 = new JPanel(); contentPane.setLayout(new BorderLayout()); contentPane.add(jScrollPane, BorderLayout.CENTER); JFrame frame1 = new JFrame(); frame1.setTitle("Text-component Printing Demo"); frame1.setSize(300, 200); frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame1.setContentPane(contentPane); frame1.setVisible(true); textArea.print(header1, footer1, true, null, null, true); } }
I have tried the given code but it didn’t solve my problem. Please try to provide some other solution.
Following are methods of TextComponent class:
• setCaretPosition(int pos1)
• setText(String strg)
• setSelectionStart(int selectionStrt1)
• setSelectionEnd(int selectionEnd1)
• setEditable(boolean bln)
The code that I have mentioned will definitely help you. Just try to understand following code. It is very simple code.
Code:import java.awt.*; import java.awt.event.*; public class TextEntryBox extends java.applet.Applet implements ActionListener { TextArea area; TextField field; public void init() { setLayout( new BorderLayout() ); add( "Center", area = new TextArea() ); area.setFont( new Font("TimesRoman",Font.BOLD,18) ); area.setText("Howdy!n"); add( "South", field = new TextField() ); field.addActionListener ( this ); } public void actionPerformed(ActionEvent e) { area.append( field.getText() + 'n' ); field.setText(""); } }
Bookmarks