How to make jTextArea Transparent background - java

I want to make a transparent background jTextArea.
I try to setBackground(new color(0,0,0,0));
jTextField is Working, jTextArea didn't working.
like this code.
// Not working.. Just remains gray.
jScrollPane1.setOpaque(false);
jScrollPane1.setBackground(new Color(0,0,0,0));
jTextArea1.setOpaque(false);
jTextArea1.setBackground(new Color(0,0,0,0));
// Working.. As it wants to be transparent.
jTextField1.setOpaque(false);
jTextField1.setBackground(new Color(0,0,0,0));
How can I jTextArea transparent background?
Thanks & Regards.

A JScrollPane is a composed component, it controls/contains a JViewport which is the component that does the drawings. See API:
A common operation to want to do is to set the background color that
will be used if the main viewport view is smaller than the viewport,
or is not opaque. This can be accomplished by setting the background
color of the viewport, via scrollPane.getViewport().setBackground().
The reason for setting the color of the viewport and not the
scrollpane is that by default JViewport is opaque which, among other
things, means it will completely fill in its background using its
background color. Therefore when JScrollPane draws its background the
viewport will usually draw over it.
So you should change the opaque and color properties of the JViewportas well. You can access it with jScrollPane1.getViewport().

The following worked for me.
JTextArea textArea = new JTextArea();
textArea.setOpaque(false);
textArea.setBackground(new Color(red, green, blue, alpha));
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.getViewport().setOpaque(false);
scrollPane.setOpaque(false);

This is example include 50% transparent
JTextArea textArea = new JTextArea();
textArea.setOpaque(false);
JScrollPane scrollPane = new JScrollPane(textArea) {
#Override
protected void paintComponent(Graphics g) {
try {
Composite composite = ((Graphics2D)g).getComposite();
((Graphics2D)g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
((Graphics2D)g).setComposite(composite);
paintChildren(g);
}
catch(IndexOutOfBoundsException e) {
super.paintComponent(g);
}
}
};
scrollPane.getViewport().setOpaque(false);
scrollPane.setOpaque(false);
edit sorry for the bug. This is work.

Related

How add a jtextfield to a jpanel that has paint component?

I'm making a game and I want to add a JTextField to a JPanel that has Paint Component. I repaint the JPanel every 16 milliseconds.
I add() the textfield to the panel but it show up only for a single frame when i click on it.
Then I tried to repaint() the textfield but now it is flashing.
public class Screen extends JPanel {
public Screen() {
JTextField txt = new JTextField();
txt.setBounds(10, 10, 300, 50);
this.add(txt);
}
#Override
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
g2D.setColor(Color.BLACK);
g2D.fillRect(0, 0, this.getWidth(), this.getHeight());
g2D.setColor(Color.WHITE);
g2D.fillRect(0, 0, this.getWidth(), 20);
txt.repaint();
}
}
I want to show the textfield on the top of the panel
JTextField txt = new JTextField();
When you create a JTextField you should use code like:
JTextField txt = new JTextField(10);
Now the text field can calculate its own preferred size.
//txt.setBounds(10, 10, 300, 50);
Don't use setBounds() to give a component a size. Again each Swing component is responsible for determining its own preferred size. Then the layout manager will set the size/location of the component on the panel.
//public void paint(Graphics g) {
public void paintComponent(Graphics g)
{
super.paintComponent(g);
// add custom painting here
}
Don't override paint(). Custom painting is done by overriding the paintComponent() method. And the first statement in the method should be super.paintComopnent(g)
//g2D.setColor(Color.BLACK);
//g2D.fillRect(0, 0, this.getWidth(), this.getHeight());
Don't paint the background of the panel. That is the job of the panel and that is why you need to super.paintComponent(), to make sure the background is painted.
Then in the constructor of your JPanel class you simply use setBackground( Color.BLACK )
//txt.repaint();
Don't ever invoke repaint() on any component in a painting method.
Read the section from the Swing tutorial on Custom Painting for working examples to get you started. Use the demo code as the starting point for you program. Then you simply add a JTextField to the panel, so it will be a single line of code that is needed to display the text field.
It seems like you want to have a JTextField on a black panel. You don't need to set the colour of the panel every time in paint() method. Instead add this to the constructor:
public Screen() {
setOpaque(true);
setBackground(Color.BLACK);
//...
}
and remove paint() method.
Also, if you want to use absolute positioning with setBounds() method then you should set the layout to null setLayout(null) in constructor. If you use absolute positioning you will also need to specify the size of the panel explicitly. However, I would still suggest you use a layout manager that takes care of panel sizing as well.
See this post for more information about absolute positioning.

Issue on label and panel's transparency

Here there is my frame:
As you can see, there are some components (JLabel, JPanel, JTable, JScrollPane) on which I called the metod setBackgroundColor(new Color(r, g, b, a)).
At each update of the frame, these components show new "shadows" of other components of the frame.
How can I eliminate these "shadows"?
At each update of the frame, these components show new "shadows" of other components of the frame
Swing does not support semi transparent colors correctly since Swing expects components to be either fully opaque or fully transparent.
Therefore you need to make sure the parent component is painted first to reset the background and then manually paint the background of the component:
JPanel panel = new JPanel()
{
protected void paintComponent(Graphics g)
{
g.setColor( getBackground() );
g.fillRect(0, 0, getWidth(), getHeight());
super.paintComponent(g);
}
};
panel.setOpaque(false); // background of parent will be painted first
panel.setBackground( new Color(255, 0, 0, 20) );
frame.add(panel);
See Backgrounds With Transparency for more information and a reusable class that will do this painting for you.

Paint Like JComponent Drawing Pad

I have a class Pad_Draw extending JComponent. Constructor is
public Pad_Draw()
{
this.setDoubleBuffered(true);
this.setLayout(null);
};
The paintComponent methpod is :
public void paintComponent( Graphics g )
{
graphics2D = (Graphics2D)g;
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics2D.setPaint(Color.white);
graphics2D.fillRect(0, 0, getSize().width, getSize().height);
}
In a JFrame I am adding this by a ScrollPane:
JScrollPane Padscroller = new JScrollPane();
Padscroller.setWheelScrollingEnabled(true);
Padscroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
Padscroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
Padscroller.setPreferredSize(new Dimension(200, 100));
Padscroller.setMinimumSize(new Dimension(200, 100));
Padscroller.setMaximumSize(new Dimension(200, 100));
Padscroller.setViewportView(drawPad);
content.add(Padscroller, BorderLayout.CENTER);
But as soon as I add the content in the Frame and set the size of the frame. The drawing pad is taking whole size whatever it needs.
I want my specified size (200,100) to be maintained and I actually want something like Windows Paint application has. I should be able to increase the size by extending a corner. As soon as I extend corner the scrollbar gets activated. Can anyone give me any idea how to achieve it?
The default layout manager for JFrame, BorderLayout does not respect preferred sizes of its components.
You could a layout manager that does, such as BoxLayout, and override getPreferredSize in your Padscroller component:
JScrollPane padScroller = new JScrollPane() {
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 100);
}
};
Regarding increasing the size, have a look at using a MouseAdapter and updating this property in the mouseDragged method.

JSplitPane Background image on right side component

I have a JSplitPane. i want to add background image to the left side of the JSplitPane.
mySplitPane.getLeftComponent
this returs the left side component. and then i want to add an image background to the left side. i think i can use the Paint()
to set background image to the JSplitPane. but how can i set on the only left side component.
I have a JTable on left side of my JSplitPane. i want to have JTable transparent. and then showing the background image.
Right now i have set background to my JTable. i dont want the background to be scrolled with JTable Scroll. Thats why i want to add background image to the Split Pane not the Table.
Could this be what you're looking for?
class ImagePanel extends JPanel {
private Image image;
public ImagePanel(Image image) {
this.image = image;
}
#Override
protected void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
}
}
Usage:
BufferedImage myImage = ImageIO.load(...);
JPanel leftPanel = new ImagePanel(myImage);
//Add panel to splitpanel
JSplitPane mySplitPane= new JSplitPane(JSplitPane.HORIZONTAL_SPLIT)
mySplitPane.setLeftComponent(leftPanel);
Above I have created a subclass of JComponent. Override the
paintComponent(Graphics g) method to paint the image that I want to
display. I then set the content pane of the JPanel and then finally
pass the panel to the left of the split pane
For more on Watermark background, see here for examples and code samples.
Edited

Java: Animated GIF with transparent window

I'm trying to display an animated gif on a transparent JDialog using a simple JLabel:
JDialog dialog = new JDialog();
AWTUtilities.setWindowOpaque(dialog,false);
JLabel label = new JLabel();
ImageIcon ii = new ImageIcon("animation.gif");
label.setIcon(ii);
JPanel panel = new JPanel();
panel.setBackground(new Color(0,0,0,0));
panel.add(label);
dialog.add(panel);
dialog.setVisible(true);
This almost works. It displays the animation smoothly and it does have transparency.
The problem is that all the animation frames are overlaid instead of getting a cleared canvas and the current frame every frame step.
I assume that the JLabel's canvas doesn't get cleared every repaint step.
Does anyone know how I can fix this?
Edit:
I figured out a solution.
I had to override the ImageIcon's paintIcon function and manually clear the canvas:
class ClearImageIcon extends ImageIcon{
public ClearImageIcon(String filename){super(filename);}
#Override
public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
Graphics2D g2 = (Graphics2D)g.create();
g2.setBackground(new Color(0,0,0,0));
g2.clearRect(0, 0, getIconWidth(), getIconHeight());
super.paintIcon(c, g2, x, y);
}
}
this draws every frame nicely on to the screen.
this http://download.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html contains GradientPaint, that's is similair as add Image or ImageIcon

Categories

Resources