user1347198
user1347198

Reputation:

java swing setBorder speed issue

i have a problem with the "speed" of setting up a border. i have a display with multiple JTextPane´s (around 450, what is required) which are updated quite often(depending on user input). here is the setting border function:

    private void setBorder(int top, int left, int bottom, int right, Color color)
    {
        Args.checkForNull(color);
        this.setBorder(BorderFactory.createMatteBorder(top, left, bottom, right, color));
    }

can you give me some tipps on, how to improve the speed of the border changing?? i mean this part:

this.setBorder(BorderFactory.createMatteBorder(top, left, bottom, right, color));

something like:

tmp = this.getStyledDocument();
        this.setDocument(blank);
        if(onOff){
            tmp.setParagraphAttributes(0, tmp.getLength(), underlinedAttr, false);
        }
        else{
            tmp.setParagraphAttributes(0, tmp.getLength(), notUnderlinedAttr, false);
        }

        this.setDocument(tmp);

thanks!

Upvotes: 1

Views: 392

Answers (1)

Walter Laan
Walter Laan

Reputation: 2976

This runs acceptable fast for me, so your problem is unlikely to be in setBorder(). Better measure again, possible problem is you are updating the border in separate events or have a really complex layout. It might be you have a bad video card (driver), you could try to see if running with -Dsun.java2d.d3d=false works better.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.swing.*;

public class TestBorderSpeed {
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                int amount = 22;

                final JPanel panel = new JPanel(new GridLayout(amount, amount));
                for (int row = 0; row < amount; row++) {
                    for (int column = 0; column < amount; column++) {
                        JTextPane pane = new JTextPane();
                        pane.setText("Row " + row + "; Column " + column);
                        panel.add(pane);
                    }
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                frame.getContentPane().add(panel);
                frame.getContentPane().add(new JButton(
                  new AbstractAction("Change borders") {

                    private final Random random = new Random();
                    private final Color[] colors =
                      { Color.RED, Color.GREEN, Color.BLUE };

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        for (Component component : panel.getComponents()) {
                            ((JComponent) component).setBorder(
                              BorderFactory.createMatteBorder(
                                    random.nextInt(3) + 1, random.nextInt(3) + 1,
                                    random.nextInt(3) + 1, random.nextInt(3) + 1,
                                    colors[random.nextInt(colors.length)]));
                        }
                    }
                }), BorderLayout.PAGE_END);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

Upvotes: 2

Related Questions