I have a JInternalFrame that I am applying a custom UI to. The UI paints the component, but when I add a JPanel to the JInternalFrame it doesn't show up. I think the UI is paining over the whole component, but how do I paint the UI THEN paint the components?
But if anyone has a better way of doing this, please let me know! Thanks!
public class ClassInternalFrame extends JInternalFrame {
public static Color currentColor;
public static final Color CLASS_TYPE = new Color(148, 227, 251);
public ClassInternalFrame(String title, Color classType) {
super(title, true, true, false, true);
currentColor = classType;
super.setUI(new ClassFrameUI());
Container pane = super.getContentPane();
pane.setLayout(new BorderLayout());
JPanel titlePanel = new JPanel();
titlePanel.setPreferredSize(new Dimension(0, 20));
pane.add(titlePanel, BorderLayout.NORTH);
titlePanel.setBorder(new MatteBorder(1, 1, 1, 1, Color.yellow));
}
}
class ClassFrameUI extends InternalFrameUI {
private final static ClassFrameUI frmUI = new ClassFrameUI();
public static ComponentUI createUI(JComponent c) {
return frmUI;
}
#Override
public void paint(Graphics g, JComponent c)
{
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.LIGHT_GRAY);
g2d.fillRect(0, 0, c.getWidth(), c.getHeight());
g2d.setColor(ClassInternalFrame.currentColor);
g2d.fillRect(0, 0, c.getWidth(), 20);
g2d.setColor(Color.DARK_GRAY);
g2d.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[] { 1, 0 }, 0));
g2d.drawRect(0, 0, c.getWidth()-1, c.getHeight()-1);
g2d.drawLine(0, 20, c.getWidth(), 20);
}
}
The problem is not that you're painting over anything, but that InternalFrameUI does absolutely nothing (if it did, you would also need to call super.paint(g, c);). Normally, painting of the components is done by a subclass such as BasicInternalFrameUI. It looks like you're trying to paint a custom title bar, a task that BasicInternalFrameUI delegates to BasicInternalFrameTitleBar.
Related
I have a transparent JList and JScrollPanel on top of a gradient JPanel the code for each of those looks like this:
JPanel midPanel = new JPanel() {
protected void paintComponent(Graphics g) {
Paint p = new GradientPaint(0.0f, 0.0f, new Color(233, 220, 0, 0),
getWidth(), getHeight(), new Color(239, 129, 91, 255), true);
Graphics2D g2d = (Graphics2D)g;
g2d.setPaint(p);
g2d.fillRect(0, 0, getWidth(), getHeight());
}
};
List code
ArrayList<String> songs = new ArrayList<>(Arrays.asList(new String[] {elements...}));
DefaultListModel<String> model = new DefaultListModel<>();
model.addAll(songs);
JList<String> songList = new JList<String>(model);
songList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
songList.setLayoutOrientation(JList.VERTICAL);
songList.setVisibleRowCount(-1);
songList.setOpaque(false);
songList.setCellRenderer(new TransparentListCellRenderer());
JScrollPane scroller = new JScrollPane(songList);
scroller.setPreferredSize(new Dimension(400, 250));
scroller.setOpaque(false);
scroller.getViewport().setOpaque(false);
scroller.setBorder(BorderFactory.createEmptyBorder());
midPanel.add(scroller);
Before anything is touched it looks like this:
And after stuff gets selected or scrolling the elements of the list all smear and create this mess:
Does anyone know how to fix this? if I had to guess what was wrong it would be an issue with the gradient because the paintComponet() method is overridden so it's not getting redrawn properly but if that is the case I do not know how to fix it. Any help is much appreciated.
The answer is to wrap the panel in a Container that is a sort of transparent.
public class AlphaContainer extends JComponent {
private JComponent component;
public AlphaContainer(JComponent component) {
this.component = component;
setLayout( new BorderLayout() );
setOpaque( false );
component.setOpaque( false );
add( component );
}
/**
* Paint the background using the background Color of the
* contained component
*/
#Override
public void paintComponent(Graphics g) {
g.setColor( component.getBackground() );
g.fillRect(0, 0, getWidth(), getHeight());
}
}
i have a rendering problem with a JLabel in Java:
i use the pattern oberver observable and when my Model notify to my View that JLabel has changed the content of JLabel or specifically the content that is showed in my JLabel area is random. Sometimes it renders a part of a button that is in another panel or sometimes it renders colors that i set in other components of my view!
But if i minimize and then maximize my frame, all is correctly rendered.
Sorry for my bad english.
This is the correct rendering of numbers on the right and on the left
This is the rendering when I click some button and then executing some OnUpdate()
EDIT
this is the class of my JPanel that contains the JLabels:
class InformationPanel extends JPanel {
private static final int DEFAULT_INFO_WIDTH = Configuration.DEFAULT_INFO_PANEL_WIDTH;
private static final int DEFAULT_INFO_HEIGHT = Configuration.DEFAULT_INFO_PANEL_HEIGHT;
private String playerId;
private int numNaviVive;
private int numNaviAffondate;
private JLabel JLabelNumNaviAffondate;
private JLabel JLabelNumNaviVive;
public InformationPanel(String playerId) {
this.playerId = playerId;
numNaviVive = 0;
numNaviAffondate = 0;
JButton JButtonPlayerId = new JButton(playerId);
JLabelNumNaviAffondate = new JLabel(String.valueOf(numNaviAffondate));
JLabelNumNaviVive = new JLabel(String.valueOf(numNaviVive));
JLabel JlblNumNaviVive = new JLabel("Navi disponibili:");
JLabel JlblNumNaviAffondate = new JLabel("Navi affondate: ");
JButtonPlayerId.setBackground(new Color(0, 0, 0, 0));
JButtonPlayerId.setFont(new Font("Serif", Font.BOLD, 16));
JButtonPlayerId.setForeground(Color.YELLOW);
JButtonPlayerId.setFocusable(false);
JButtonPlayerId.setEnabled(false);
JlblNumNaviVive.setBackground(new Color(0, 0, 0, 0));
JlblNumNaviVive.setFont(new Font("Serif", Font.BOLD, 16));
JlblNumNaviVive.setForeground(Color.YELLOW);
JlblNumNaviVive.setDoubleBuffered(true);
JlblNumNaviAffondate.setBackground(new Color(0, 0, 0, 0));
JlblNumNaviAffondate.setFont(new Font("Serif", Font.BOLD, 16));
JlblNumNaviAffondate.setForeground(Color.YELLOW);
JlblNumNaviAffondate.setDoubleBuffered(true);
JLabelNumNaviAffondate.setBackground(new Color(0, 0, 0, 0));
JLabelNumNaviAffondate.setFont(new Font("Serif", Font.BOLD, 16));
JLabelNumNaviAffondate.setForeground(Color.YELLOW);
JLabelNumNaviAffondate.setFocusable(false);
JLabelNumNaviAffondate.setEnabled(false);
JLabelNumNaviVive.setBackground(new Color(0, 0, 0, 0));
JLabelNumNaviVive.setFont(new Font("Serif", Font.BOLD, 16));
JLabelNumNaviVive.setForeground(Color.YELLOW);
JLabelNumNaviVive.setFocusable(false);
JLabelNumNaviVive.setEnabled(false);
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
setPreferredSize(new Dimension(DEFAULT_INFO_WIDTH, DEFAULT_INFO_HEIGHT));
//setBackground(new Color(0, 0, 0, 0));
setBackground(new Color(0,0,0,40));
add(JButtonPlayerId);
add(JlblNumNaviAffondate);
add(JLabelNumNaviAffondate);
add(JlblNumNaviVive);
add(JLabelNumNaviVive);
setVisible(true);
}
public String getId() {
return playerId;
}
public int getNumNaviAffondate() {
return numNaviAffondate;
}
public int getNumNaviVive() {
return numNaviVive;
}
public void setNumNaviVive(int numNaviVive) {
this.numNaviVive = numNaviVive;
this.JLabelNumNaviVive.setText(String.valueOf(numNaviVive));
this.validate();
}
public void setNumNaviAffondate(int numNaviAffondate) {
this.numNaviAffondate = numNaviAffondate;
this.JLabelNumNaviAffondate.setText(String.valueOf(numNaviAffondate));
this.validate();
}
}
Frist of all variable names should NOT start with an upper case character. You code is hard to read because the forum thinks all your variables are class names and highlights them as such. Follow Java conventions and don't make up your own.
JLabelNumNaviAffondate.setBackground(new Color(0, 0, 0, 0));
Variable are transparent by default so you can't set a background Color to them. The fact that you are trying to set a transparent color may or may not be your problem.
setBackground(new Color(0,0,0,40));
This may be the problem. You can't just set transparency on a Swing components as this breaks the painting chain. That is an opaque component guarantees to paint the background of itself, but when you use transparency the background is not completely painted so you get painting artifacts.
So basically you need to manage painting of the background yourself. So you actuallyl need to make your component non-opaque to the parent component is first painted, then you can paint your transparent background.
JPanel panel = new JPanel()
{
protected void paintComponent(Graphics g)
{
g.setColor( getBackground() );
g.fillRect(0, 0, getWidth(), getHeight());
super.paintComponent(g);
}
};
panel.setOpaque(false);
panel.setBackground( new Color(255, 0, 0, 20) );
frame.add(panel);
See Background With Transparency for more information and a simpler solution so that you don't need to do custom painting on all your components.
I cannot get the code linked below to do exactly what I want it to do.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Gui {
static JFrame frame;
static JLabel label;
public static void main (String[] args) {
Gui gui = new Gui();
gui.go();
}
public void go () {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,300);
frame.setVisible(true);
MyDrawPanel panel = new MyDrawPanel();
frame.getContentPane().add(BorderLayout.CENTER, panel);
label = new JLabel("I'm a label");
frame.getContentPane().add(BorderLayout.WEST, label);
JButton colorButton = new JButton("Change Colors");
ColorButtonListener colorButtonListener = new ColorButtonListener();
colorButton.addActionListener(colorButtonListener);
frame.getContentPane().add(BorderLayout.SOUTH, colorButton);
JButton labelButton = new JButton("Change Label");
LabelButtonListener labelButtonListener = new LabelButtonListener();
labelButton.addActionListener(labelButtonListener);
frame.getContentPane().add(BorderLayout.EAST, labelButton);
}
}
class ColorButtonListener implements ActionListener {
JFrame frame = Gui.frame;
public void actionPerformed (ActionEvent event) {
frame.repaint();
}
}
class LabelButtonListener implements ActionListener {
JLabel label = Gui.label;
public void actionPerformed (ActionEvent event) {
if (label.getText() == "That hurt") {
label.setText("I'm a label");
} else {
label.setText("That hurt");
}
}
}
class MyDrawPanel extends JPanel {
public void paintComponent (Graphics g) {
Graphics2D g2d = (Graphics2D) g;
int red = (int) (Math.random() * 256);
int green = (int) (Math.random() * 256);
int blue = (int) (Math.random() * 256);
Color startColor = new Color(red, green, blue);
red = (int) (Math.random() * 256);
green = (int) (Math.random() * 256);
blue = (int) (Math.random() * 256);
Color endColor = new Color(red, green, blue);
GradientPaint gradient = new GradientPaint(70, 70, startColor, 150, 150, endColor);
g2d.setPaint(gradient);
g2d.fillOval(0, 0, this.getWidth(), this.getHeight());
}
}
There is a panel class used to draw a circle and then the panel is positioned in the center region of the frame.
A label is positioned in the west region of the frame, two buttons colorButton(positioned south) and labelButton(positioned east) should control the circle and the label respectively. 2 classes ColorButtonListener and LabelButtonListener implement the ActionListener interface and are registered with the colorButton and labelButton respectively. The color button when clicked should paint a circle with random colors and the label button when clicked should toggle between the text "I'm a label" and "That hurt".
Now, the issue I am having is with the label button. When clicking it, it not only changes the text(as expected), but also redraws the circle. This button should not be redrawing the circle. The color button works as expected.
The problem is you don't control when a repaint might occur. Instead of changing the color every time paintComponent is called, which you can't control, change the color only when you want to and reference from within the paintComponent method, for example
class MyDrawPanel extends JPanel {
private Color startColor;
private Color endColor;
// And setters or some other way
// to randomise the colors
public void paintComponent (Graphics g) {
Graphics2D g2d = (Graphics2D) g;
GradientPaint gradient = new GradientPaint(70, 70, startColor, 150, 150, endColor);
g2d.setPaint(gradient);
g2d.fillOval(0, 0, this.getWidth(), this.getHeight());
}
}
Check out Painting in AWT and Swing for more details
This is because setText() methods internally calls repaint() if new text is not same as old text. So results in color change whenever you click that button too.
I am having a little bit of trouble drawing components from an ArrayList I have created. If I screw around with it, I can either get the first element or the second element and if I am lucky, neither, to show up!
import java.awt.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
public class FaceShortCode {
ArrayList<CreateCircles> faceCircles = new ArrayList<CreateCircles>();
public FaceShortCode() {
JFrame main = new JFrame();
main.setTitle("Face Frame");
main.setSize(new Dimension(600, 600));
main.setLocationRelativeTo(null);
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main.setVisible(true);
Container c = main.getContentPane();
// c.setLayout(null);
faceCircles.add(0, new CreateCircles(100, 50, 400, 350, Color.red));
faceCircles.add(1, new CreateCircles(200, 100, 65, 65, Color.black));
c.add(faceCircles.get(0));
c.add(faceCircles.get(1));
}
class CreateCircles extends JComponent {
double x, y, width, height;
Color myColor;
public CreateCircles(double x, double y, double width, double height,
Color myColor) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.myColor = myColor;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Ellipse2D circle = new Ellipse2D.Double(x, y, width, height);
g2.setColor(myColor);
g2.fill(circle);
}
}
static class Run {
public static void main(String[] args) {
new FaceShortCode();
}
}
}
I have tried throwing in a main.repaint() after every addition to the Container c,tried a repaint() in my painting method but nothing seems to be working. Is there any were else to put a repaint() that I am just missing?
You need to put all your logic before you setVisible() or else your frame will become visible before the logic is performed.
public FaceShortCode() {
Container c = main.getContentPane();
// c.setLayout(null);
faceCircles.add(0, new CreateCircles(100, 50, 400, 350, Color.red));
faceCircles.add(1, new CreateCircles(200, 100, 65, 65, Color.black));
c.add(faceCircles.get(0));
c.add(faceCircles.get(1));
JFrame main = new JFrame();
main.setTitle("Face Frame");
main.setSize(new Dimension(600, 600));
main.setLocationRelativeTo(null);
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main.setVisible(true);
}
Consider making FaceShortCode extends JFrame.
public class FaceShortCode extends JFrame {
private ArrayList<CreateCircles> faceCircles = new ArrayList<CreateCircles>();
public FaceShortCode {
setLayout(new Girdlayout(1, 2));
faceCircles.add(0, new CreateCircles(100, 50, 400, 350, Color.red));
faceCircles.add(1, new CreateCircles(200, 100, 65, 65, Color.black));
add(faceCircles.get(0));
add(faceCircles.get(1))
setTitle("Face Frame");
setSize(new Dimension(600, 600));
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
Your design is a bit confusing. There is no need for an ArrayList. When you use components you can just add the components directly to the panel. Normally you would only use an ArrayList when you are doing custom painting by painting Objects that are not components, for example when you want to paint a Shape. For an example of this approach take a look at Custom Painting Approaches.
When using components, a component needs a size and location in order to be painted automatically by Swing. Normally you would let a layout manager determine these properties. In your case you are doing random placement of your components so you would need to use a null layout and then set the size/location of each component.
So what you need to do is change how you paint your custom component. All painting should be done at location (0, 0) of your component. Then you would set the location of the component to be the x/y value. This means the component will be painted at the x/y location relative to the panel you add the component to. Then you need to set the size of your component which would be the width/height.
Hi how to set gradient background to column header ? I have Jtable:
JTable table = new JTable(4, 5);
and try to setColor: table.getTableHeader().setBackground(Color.blue);
but with no succes color of column header is still same and these is just one color and I need gradient
thx for help
Are you coding this with NetBean's code generation or are you coding your Swing by hand?
Have you tried creating a class that extends the JTableHeader and overrides its paintComponent method? Give it a try and in that method create a GradientPaint object or one of its variants use it to set the Graphics2D paint's property and then call fillRect using the current dimensions of the component to fill it with gradient color.
For example here are two ways to do this, one with a custom JTableHeader and one with a custom cell renderer that is used by the table header:
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public class GradientHeader {
private static void createAndShowGui() {
JTable table1 = new JTable();
GradientTableHeader gradientTableHeader = new GradientTableHeader();
gradientTableHeader.setColumnModel(table1.getColumnModel());
table1.setTableHeader(gradientTableHeader);
DefaultTableModel model = new DefaultTableModel(new Integer[][] {
{ 1, 2 }, { 3, 4 } }, new String[] { "A", "B" });
table1.setModel(model);
JTable table2 = new JTable(model);
table2.getTableHeader().setDefaultRenderer(new MyCellRenderer());
JPanel mainPanel = new JPanel(new GridLayout(1, 0));
mainPanel.add(new JScrollPane(table1));
mainPanel.add(new JScrollPane(table2));
JFrame frame = new JFrame("GradientHeader");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class GradientTableHeader extends JTableHeader {
private static final Color COLOR_1 = new Color(255, 0, 0, 240);
private static final Color COLOR_2 = new Color(0, 0, 255, 120);
private static final float SIDE = 50;
private GradientPaint gradientPaint = new GradientPaint(0, 0, COLOR_1, SIDE,
SIDE, COLOR_2, true);
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(gradientPaint);
g2.fillRect(0, 0, getWidth(), getHeight());
}
}
class MyCellRenderer extends JPanel implements TableCellRenderer {
private static final Color COLOR_1 = new Color(255, 0, 0, 200);
private static final Color COLOR_2 = new Color(0, 0, 255, 200);
private static final float SIDE = 50;
private GradientPaint gradientPaint = new GradientPaint(0, 0, COLOR_1, SIDE,
SIDE, COLOR_2, true);
private JLabel label = new JLabel();
public MyCellRenderer() {
setOpaque(true);
setLayout(new BorderLayout());
add(label, BorderLayout.CENTER);
label.setHorizontalAlignment(SwingConstants.CENTER);
}
#Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocused, int arg4, int arg5) {
label.setText(value.toString());
return this;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(gradientPaint);
g2.fillRect(0, 0, getWidth(), getHeight());
}
}