Results 1 to 2 of 2
  1. #1
    Flo
    Flo is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default Indentation not possible in JTextPane?

    Hello,
    Since you could help me so well last time I bring up a new problem of mine here.
    In my IDE-Editor I use a JTextPane.
    When selecting multiple lines and press tab afterwards, they all are replaced for a tabulator.
    But thats not supposed to happen. What I would like to happen is that every selected line indents.
    How can I make this possible?

    Flo

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    This example might get you started:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.text.*;
    
    public class TextPaneIndent extends JFrame
    {
    	public TextPaneIndent()
    	{
    		JTextPane textPane = new JTextPane();
    		textPane.setText("one\ntwo\nthree\nfour");
    		textPane.setPreferredSize( new Dimension(200, 220) );
    		JScrollPane scrollPane = new JScrollPane( textPane );
    		scrollPane.setPreferredSize( new Dimension( 200, 200 ) );
    		getContentPane().add( scrollPane );
    
    		//  Create a Button panel
    
    		JPanel buttons = new JPanel();
    		getContentPane().add(buttons, BorderLayout.SOUTH);
    
    		//  Add Indent button
    
    		JButton indent = new JButton( new LeftIndentAction("Indent", 10) );
    		buttons.add( indent );
    
    		//  Add Outdent button
    
    		JButton outdent = new JButton( new LeftIndentAction("Outdent", -10) );
    		buttons.add( outdent );
    
    
    //		InputMap im = textPane.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    //		InputMap im = textPane.getInputMap();
    //		KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
    //		im.put(enter, "none");
    	}
    
    	public static void main(String[] args)
    	{
    		TextPaneIndent frame = new TextPaneIndent();
    		frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
    		frame.pack();
    		frame.setVisible(true);
    	}
    
    	class LeftIndentAction extends StyledEditorKit.StyledTextAction
    	{
    		private float value;
    
    		public LeftIndentAction(String name, float value)
    		{
    			super(name);
    			this.value = value;
    		}
    
    		public void actionPerformed(ActionEvent e)
    		{
    			JEditorPane editor = getEditor(e);
    
    			if (editor != null)
    			{
    				StyledDocument doc = getStyledDocument( editor );
    				int offset = editor.getCaretPosition();
    				Element paragraph = doc.getParagraphElement( offset );
    				AttributeSet as = paragraph.getAttributes();
    				float indent = StyleConstants.getLeftIndent( as );
    				indent += value;
    
    				MutableAttributeSet attr = new SimpleAttributeSet();
    				StyleConstants.setLeftIndent(attr, indent);
    				setParagraphAttributes(editor, attr, false);
    			}
    		}
    	}
    
    }

Similar Threads

  1. JTextpane
    By Mash in forum AWT / Swing
    Replies: 1
    Last Post: 04-15-2010, 06:36 PM
  2. Indentation - plain text
    By barney75 in forum New To Java
    Replies: 1
    Last Post: 03-23-2009, 05:54 PM
  3. Indentation Size Reset While Coding JavaFX
    By JDCAce in forum NetBeans
    Replies: 0
    Last Post: 10-06-2008, 09:28 PM
  4. String Code Indentation
    By andysh in forum New To Java
    Replies: 1
    Last Post: 12-22-2007, 02:45 AM
  5. how to set indentation to a text
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 06:42 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •