This is what I want to achieve:
What I tried:
jTextArea.setOpaque(false);
this makes JTextArea
transparent.jScrollPane.setOpaque(false);
this gives no effectthen I tried this which hides both JScrollPane
and JTextArea
.
jScrollPane.getViewPort().setOpaque(false);
jScrollPane.setOpaque(false);
then I tried this which hides both JScrollPane
and JTextArea
.
jScrollPane.setViewPort(new MyViewPort());
class MyViewPort() extends JViewPort{
public MyViewPort(){
setOpaque(false);
}
}
What I want to achieve is JScrollPane
background transparent and transparent JTextArea
where I should able to add text and visible JScrollPane
.
Update: I did like this I can add text in textArea but the jscrollPane is not transparent:
public class TransparentBackground extends javax.swing.JFrame {
public TransparentBackground() {
jScrollPane = new javax.swing.JScrollPane();
jTextArea = new javax.swing.JTextArea();
lblBackground = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jTextArea.setColumns(20);
jTextArea.setRows(5);
jScrollPane.setViewportView(jTextArea);
jScrollPane.getViewport().setOpaque(false);
jScrollPane.setOpaque(false);
jTextArea.setOpaque(false);
getContentPane().add(jScrollPane, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 40, 580, 300));
lblBackground.setIcon(new javax.swing.ImageIcon(getClass().getResource("/bg.png"))); // NOI18N
getContentPane().add(lblBackground, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 680, 390));
pack();
}
And When I use
jScrollPane.setViewPort(new MyViewPort());
class MyViewPort() extends JViewPort{
public MyViewPort(){
setOpaque(false);
}
}
Both textArea and JScrollPane disappears(transparent) but need jTextArea transparent and editable or can add text in it.
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);
}
});
}
}