Results 1 to 4 of 4
  1. #1
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Working with text panes

    I am getting pretty close to having a fairly decent text editor but I am still running into trouble with some capabilities I would like to implement.

    One of the first questions I have is in regards to button groups. I have a button which changes the text to sub script and a button for super script. However, it doesn't make sense to allow both to be set at the same time. I added both buttons to a button group which allows easy switching between types. The problem is, once selected I can't unselect either of these buttons, instead I can only change them. My first thought was to add a 3rd button which does nothing, but it seems like a bad approach. Is there a way to allow button group items to be unselected?

    The next thing comes down to some more advanced font editing, lets say I have a sentence with many different styles mixed in, like this
    Java Code:
    [FONT="Arial Black"]HI THERE[/FONT][SIZE="6"]How are your doing?[/SIZE][COLOR="Red"][SIZE="5"]Today?[/SIZE][/COLOR]
    I would like to be able to highlight a sentence like this and apply changes. The code I have works in changing it but it sets all the text to be exactly the same in every way. If I change the color, the sizes, and fonts all become the same as well. My thought was to loop through the selected text and set each individual item with setCharacterAttributes, using an attribute set which has the texts original value + the change. But how can I query what the texts value is?


    Next, I am using the default cut/copy/paste techniques supplied by the editor kit, extracted with this method(taken from oracle tutorials)
    Java Code:
    private HashMap<Object, Action> createActionTable(JTextComponent textComponent) {
            HashMap<Object, Action> actions = new HashMap<Object, Action>();
            Action[] actionsArray = textComponent.getActions();
            for (int i = 0; i < actionsArray.length; i++) {
                Action a = actionsArray[i];
                actions.put(a.getValue(Action.NAME), a);
            }
    		return actions;
        }
    I use the RTFEditorKit cut Action, it works, but it doesn't keep the items maintaining there previous format. Everything becomes the same font size, color, font, and other modifiers. How can I make it maintain the style?

    My final question is this, I want the program to keep track of the number of lines in the text, which seems easy enough. My thought was originally to count the previous 10 characters input, if any of them are \n it should increment a count. This was originally 1 previous character and it never worked. I think I understand the problem(windows uses carriage return line feed, right?) Would a better approach be to extract the text with getText and then loop through counting how many new lines there are?

    Thanks in advance for any help, I really hope this is clear. I believe I supplied information which is more than enough, if you would like me to make some small snippets for some of them I can.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    I don't know if it's kosher, but you can subclass ButtonGroup.

    Java Code:
    import java.awt.GridLayout;
    
    import javax.swing.ButtonGroup;
    import javax.swing.ButtonModel;
    import javax.swing.JFrame;
    import javax.swing.JToggleButton;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    
    public class ToggleGroup extends JFrame {
    	
        public ToggleGroup() {
            super("Group of toggle buttons");
            setLayout(new GridLayout(0, 1, 3, 3));
    
            MyButtonGroup group = new MyButtonGroup();
            JToggleButton but = new JToggleButton("Subscript");
            group.add(but);
            add(but);
            but = new JToggleButton("Superscript");
            group.add(but);
            add(but);
        }
    	
        public static void main(String[] args) {
            try {
                UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName());
            } catch (Exception e) {}
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    ToggleGroup frame = new ToggleGroup();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    }
    
    class MyButtonGroup extends ButtonGroup {
        @Override
        public void setSelected(ButtonModel m, boolean b) {
            if(!b) {
                System.out.printf(
                        "button being turned off... currently it %s selected%n",
                        isSelected(m) ? "is" : "is not");
                if(isSelected(m)) {
                    clearSelection();
                    return; // ???
                }
            }
            super.setSelected(m, b);
        }
    }

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Thanks for that pbrock. Also thanks for the previous link, it's been fairly helpful for a lot of the editor. Any ideas on my other concerns?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    I reported this smithadam576 because he drools his random drivel all over the place.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. JTab Panes
    By Nidi in forum New To Java
    Replies: 2
    Last Post: 03-02-2011, 05:27 AM
  2. Replies: 8
    Last Post: 08-06-2010, 09:41 AM
  3. working with Text in PDFBox
    By juehv in forum Advanced Java
    Replies: 0
    Last Post: 03-31-2010, 03:58 PM
  4. Layered Panes
    By teckno101 in forum AWT / Swing
    Replies: 1
    Last Post: 10-04-2009, 12:16 AM
  5. Working with Tooltip Text
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-26-2008, 08:41 PM

Posting Permissions

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