Java - Transparent Jscrollpane

Java - Transparent JScrollPane

Your colloquy with @Serplat suggests that you may be confounding opacity and transparency.

Opacity is a boolean property of Swing components used to optimize drawing:

  • true: The component agrees to paint all of the bits contained within its rectangular bounds.
  • false: The component makes no guarantees about painting all the bits within its rectangular bounds.

Transparency is a means of compositing digital images, as seen in this example.

Considering the distinction may help to clarify your question or focus your search for more information.

Addendum: Based on @camickr's example, the example below shows a blue square that "sticks" to the viewport, while the gray checkerboard may be scrolled over it.

ScrollPanePaint

import java.awt.*;
import javax.swing.*;

/** @see https://stackoverflow.com/questions/2846497 */
public class ScrollPanePaint extends JFrame {

private static final int TILE = 64;

public ScrollPanePaint() {
JViewport viewport = new MyViewport();
viewport.setView(new MyPanel());
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewport(viewport);
this.add(scrollPane);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
}

private static class MyViewport extends JViewport {

public MyViewport() {
this.setOpaque(false);
this.setPreferredSize(new Dimension(6 * TILE, 6 * TILE));
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.blue);
g.fillRect(TILE, TILE, 3 * TILE, 3 * TILE);
}
}

private static class MyPanel extends JPanel {

public MyPanel() {
this.setOpaque(false);
this.setPreferredSize(new Dimension(9 * TILE, 9 * TILE));
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.lightGray);
int w = this.getWidth() / TILE + 1;
int h = this.getHeight() / TILE + 1;
for (int row = 0; row < h; row++) {
for (int col = 0; col < w; col++) {
if ((row + col) % 2 == 0) {
g.fillRect(col * TILE, row * TILE, TILE, TILE);
}
}
}
}
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
new ScrollPanePaint();
}
});
}
}

How to set JScrollPane Background transparent and JScrollBarr Visible with JTextArea?

I don't know whats wrong with privious code, may be due to the use of drag and drop.
Here is code that worked. Thank you Camickr and MadProgrammer for you suggestion. :)

import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.*;

public class TransparentBackground1 extends JFrame {
private javax.swing.JScrollPane jScrollPane;
private javax.swing.JTextArea jTextArea;
private javax.swing.JLabel lblBackground;

public TransparentBackground1() {
setPreferredSize(new Dimension(675, 375));
jScrollPane = new JScrollPane();
jTextArea = new JTextArea();
lblBackground = new JLabel();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new FlowLayout());

jTextArea.setColumns(20);
jTextArea.setRows(5);
jScrollPane.setViewportView(jTextArea);

//Code To make transparent
jScrollPane.getViewport().setOpaque(false);
jScrollPane.setOpaque(false);
jTextArea.setOpaque(false);

lblBackground.setIcon(new ImageIcon(getClass().getResource("/bg.png"))); // NOI18N

pack();
}

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TransparentBackground().setVisible(true);
}
});
}
}

Here is Output

JScrollPane with transparent background and content

Taking the window out of the equation for the momement.

The JScrollPane contains a JViewport which then contains you content. So you need to set your content pane to transparent, the viewport to transparent and then the scroll pane to transparent.

You can achieve this by using setOpaque(false) on each of these containers.

This will ensure that the repaint manager will now paint through the background.

The next problem is, Swing doesn't actually support "semi-transparent" components (that is, either it's opaque or transparent).

You can implement this by overriding the paintComponent method of the main component (the one on the viewport is probably sufficient)

Java: how to make container's Jscrollpane background not opaque? (ie. transparent)

Give it a try:

    scrollPane.getViewport().setOpaque(false);
scrollPane.setBorder(null);

How to make JScrollPane Transparent in Java Swing

  1. you can set the JScrollPane view port's opaque to false by asking the JScrollPane instance to return it's view port:

     jScrollPane1.getViewport().setOpaque(false);
  2. In your code why you are setting size using setSize(Dimension) method. Don't tell me you working with NullLayout. Learn to use Layout Mangers. They will make you happy.

Edit:

Ok, use an extension of JViewPort, you can paint anything inside if necessary:

class MyViewPort extends JViewport
{

public MyViewPort() {
setOpaque(false);
}

}

Then set an instance of MyViewPort as your JScrollPane's view port invoking:

setViewport(new MyViewPort())

Transparent JEditorPane in a JScrollPane over a background JPanel

The Background Panel class has code that attempts to make all component non-opaque so the background will show through.

Edit:

If you want to play with transparent background then you can check out Backgrounds With Transparency for an explanation of the problem and a couple of solutions.

In this case the BackgroundPane is the easier solution.

Transparent JList in JScrollPane all in one class

You need to make the JList, JScrollPane AND the JScrollPane's JViewport all transparent, for example...

list.setOpaque(false);
JScrollPane sp = new JScrollPane(list);
sp.setOpaque(false);
sp.getViewport().setOpaque(false);

Oh, and one last trick, you need to replace the JList's cell renderer with one which will make it transparent when it's not selected, for example

public class TransparentListCellRenderer extends DefaultListCellRenderer {

@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
setOpaque(isSelected);
return this;
}

}

Transparent List

package javaapplication1.pkg295;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

public static void main(String[] args) {
new Test();
}

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new TestPane());

DefaultListModel model = new DefaultListModel();
model.addElement("Apple");
model.addElement("Banana");
model.addElement("Pear");
model.addElement("Peach");
model.addElement("Grap");
JList list = new JList(model);
list.setCellRenderer(new TransparentListCellRenderer());
list.setOpaque(false);
JScrollPane sp = new JScrollPane(list);
sp.setOpaque(false);
sp.getViewport().setOpaque(false);

frame.add(sp);

frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TransparentListCellRenderer extends DefaultListCellRenderer {

@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
setForeground(Color.WHITE);
setOpaque(isSelected);
return this;
}

}

public class TestPane extends JPanel {

private BufferedImage bgImg;

public TestPane() {
setLayout(new BorderLayout());
try {
bgImg = ImageIO.read(new File("C:\\Users\\shane\\Dropbox\\MegaTokyo\\Aqua\\200x200\\aria_manga_wallpaper-5541.jpg"));
} catch (IOException ex) {
ex.printStackTrace();
}
}

@Override
public Dimension getPreferredSize() {
return bgImg == null ? new Dimension(200, 200) : new Dimension(bgImg.getWidth(), bgImg.getHeight());
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (bgImg != null) {
Graphics2D g2d = (Graphics2D) g.create();
int x = (getWidth() - bgImg.getWidth()) / 2;
int y = (getHeight() - bgImg.getHeight()) / 2;
g2d.drawImage(bgImg, x, y, this);
g2d.dispose();
}
}

}

}


Related Topics



Leave a reply



Submit