0

My issue is that whenever my JTextPane is highlighted by the user or the user scrolls, the text within the JTextPane overlaps itself many times and the color frequently changes.

I've tried looking for other answers but my current code allows the JTextPane to get added to a JScrollPane, which is added to a JLayeredPane which is then added to a class that extends JFrame

public class View extends JFrame {
    private static final long serialVersionUID = 1L;
    final Dimension SIZE = new Dimension(2661, 1663);

    /**
     * @return the pre-determined size of the JFrame
     */
    public Dimension getDimensionSize() {
        return SIZE;
    }

    public View(){
    setTitle("Warframe Alert/Invasion Tracker - Project 4");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);
    createPanel();
    pack();
    setLocation(0,0);
    setVisible(true);
    }

    public void createPanel(){
        JTextPane textPane = new JTextPane();
        Font font = new Font("Apple Casual", Font.PLAIN, 35);
        textPane.setEditable(false);
        textPane.setBackground(new Color(0, 200, 255, 50));
        textPane.setText(Content.getAlertText()); //Content class is a class that creates the content to show on the textPane
        textPane.setForeground(Color.black);
        textPane.setFont(font);

        JScrollPane scrollPane = new JScrollPane(textPane, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setBounds(50, 50, 1331, 1331);

        JLabel background = new JLabel(new ImageIcon("..\\Project 4\\img\\bg.jpg"));
        background.setBounds(0, 0, 2661, 1663);

        JLayeredPane layeredPane = new JLayeredPane();
        layeredPane.setPreferredSize(getDimensionSize());
        layeredPane.add(background);
        layeredPane.add(scrollPane);

        this.getContentPane().add(layeredPane, BorderLayout.CENTER);
    }
}

Here is an image of the result I am getting:

Image

halfer
  • 19,824
  • 17
  • 99
  • 186
Tyler
  • 21
  • 6
  • It seems as if it has to do with the opacity, when the textPane.setBackground(...), and when the alpha is removed the issue goes away – Tyler Apr 22 '19 at 00:57
  • 1
    `textPane.setBackground(new Color(0, 200, 255, 50));` Swing components don't support alpha based colours, they are either opaque or transparent (using `setOpaque`), [for example](https://stackoverflow.com/questions/31105099/jpanel-not-keeping-color-alpha-when-changing-background/31105289#31105289) – MadProgrammer Apr 22 '19 at 01:03
  • 1
    And [example](https://stackoverflow.com/questions/14177340/remove-jtextpanes-white-background-without-setopaque-over-a-translucent-jfram/14179477#14179477), [example](https://stackoverflow.com/questions/14761598/write-text-into-a-jtextpane-with-an-image-background/14763273#14763273) – MadProgrammer Apr 22 '19 at 01:13
  • See: [Backgrounds With Transparency](https://tips4java.wordpress.com/2009/05/31/backgrounds-with-transparency/) for the problem and solutions. – camickr Apr 22 '19 at 04:25

1 Answers1

0

As @MadProgrammer said, problem is with using the alpha parameter in the Color selection. Test the code below with each setBackground command commented out.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.AbstractDocument.Content;

public class View extends JFrame {

    private static final long serialVersionUID = 1L;
    private static final float DIM_WIDTH = 2661f;
    private static final float DIM_HEIGHT = 800; //R1663f;
    final Dimension SIZE = new Dimension(Math.round(DIM_WIDTH), Math.round(DIM_WIDTH));

    /**
     * @return the pre-determined size of the JFrame
     */
    public Dimension getDimensionSize() {
        return SIZE;
    }

    public View() {
        setTitle("Warframe Alert/Invasion Tracker - Project 4");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        createPanel();
        pack();
        setLocation(0, 0);
        setVisible(true);
    }

    public void createPanel() {
        JTextPane textPane = new JTextPane();
        Font font = new Font("Apple Casual", Font.PLAIN, 35);
        textPane.setEditable(false);
//        textPane.setBackground(new Color(0, 200, 255, 50));
        textPane.setBackground(new Color(0, 204, 255)); // web safe 
//        textPane.setText(Content.getAlertText()); //Content class is a class that creates the content to show on the textPane
        textPane.setText("1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n");
        textPane.setForeground(Color.black);
        textPane.setFont(font);

        JScrollPane scrollPane = new JScrollPane(textPane, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setBounds(50, 50, Math.round(DIM_HEIGHT * 0.8f), Math.round(DIM_HEIGHT * 0.8f));

        JLabel background = new JLabel(new ImageIcon("..\\Project 4\\img\\bg.jpg"));
        background.setBounds(0, 0, Math.round(DIM_WIDTH), Math.round(DIM_HEIGHT));

        JLayeredPane layeredPane = new JLayeredPane();
        layeredPane.setPreferredSize(getDimensionSize());
        layeredPane.add(background);
        layeredPane.add(scrollPane);

        this.getContentPane().add(layeredPane, BorderLayout.CENTER);
    }

    public static void main(String[] args) {
        new View();
    }
}
  • Hi, that had fixed the problem. I got rid of the Alpha all together instead of rebuilding my code. Thanks. – Tyler Apr 27 '19 at 06:40