i'm making a small calculator program, and i want the display to look like this:
but i'm getting this:

this is the code i am currently using for the task:

public void actionPerformed(ActionEvent e){
        String currText = viewer.getText();
        String text = enter.getText();
        int len = currText.length();
        viewer.setText(currText + "\n" + text + "\n");
        currText = viewer.getText();
        len = currText.length();
            text = ""+p.evaluate(text);
        } catch (ParserException pe){
            text = pe.toString();
        viewer.setText(currText + text);

this action listener is registered with the textfield (fired by pressing enter) and is the only thing that can trigger it
p is a reference to a parser and does nothing to the format
left and right are instances of simpleattributeset with setalign to ALIGN_LEFT and ALIGN_RIGHT respectively
i cannot figure out how exactly i should fix this, and i could use advice


Recommended Answers

All 2 Replies

I haven't messed around with styled text panes much, so there may be a more appropriate way to do this, but I did get it to alternate the alignment with the following listener:
(txtInput is just a text field component and I pass the listener a reference to the text panes getStyledDocument() result)

class StyledInsertListener implements ActionListener {

    boolean alignLeft = true;
    StyledDocument doc;

    public StyledInsertListener(StyledDocument doc) {
        this.doc = doc;

        Style defaultStyle = StyleContext.getDefaultStyleContext().

        Style styleRef = doc.addStyle("left", defaultStyle);
        StyleConstants.setAlignment(styleRef, StyleConstants.ALIGN_LEFT);

        styleRef = doc.addStyle("right", defaultStyle);
        StyleConstants.setAlignment(styleRef, StyleConstants.ALIGN_RIGHT);

    public void actionPerformed(ActionEvent e) {
        String input = txtInput.getText()+"\n";
        try {
            if(alignLeft) {
                doc.setLogicalStyle(doc.getLength(), doc.getStyle("left"));
                doc.insertString(doc.getLength(), input, doc.getStyle("left"));
                alignLeft = false;
            } else {
                doc.setLogicalStyle(doc.getLength(), doc.getStyle("right"));
                doc.insertString(doc.getLength(), input, doc.getStyle("right"));
                alignLeft = true;
        } catch(BadLocationException ex) {

The insertString() call by itself would not respect the alignment of the style, so I presume that it was treating it as within the same paragragh. Adding the setLogicalStyle() call did the trick, though it seems redundant to specify that style twice. I couldn't find a mechanism to force a new paragraph prior to insertion, but I would assume there is a way to do that. I just didn't have time to delve much deeper into it.

commented: quite useful info +1

thank you that worked very well i had to add a bit but it works

although if anyone does know a simpler way it would be good to streamline

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.