javaswingjpaneljcomponentjslider

Jslider reflected in pannel


I don't know why, although I am trying to make a color picker and for some reason the slider gets reflected in the panel, if I use one of the sliders and resize the frame the thing disappears.

Here is what I see:

Image

I tried to change how different components are rendered, I tried to change the change listener, I tried to change as well the panel, but nothing changed.

I used repaint and tried not to use repaint.

This is my code:

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JSlider;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.FlowLayout;
import javax.swing.BoxLayout;
import net.miginfocom.swing.MigLayout;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class ThemeBuilder extends JFrame {

    private JPanel contentPane;
    private JPanel colorShow;
    private JPanel pnlButtons;
    private JPanel pnlSldG;
    private JPanel pnlSldB;
    private JPanel pnlSldAlpha;
    private JPanel pnlSldR;
    private JButton btnBackground;
    private JButton btnText;
    private JButton btnPanels;
    private JButton btnButtonsText;
    private JButton btnSideList;
    private JButton btnButtonLabels;
    private JButton btnButtonContours;
    private JButton btnSideListForeground;
    private JButton btnForeground;
    private JLabel label;
    private JSlider sldG;
    private JSlider sldB;
    private JSlider sldAlpha;
    private JSlider sldR;
    private JMenuBar menuBar;
    private JMenuItem mntmFile;



    /**
     * Create the frame.
     */
    public ThemeBuilder() {
        makeSliders();
        makeBtns();
        makePnls();
        makeFrame();
    }

    private void makeFrame(){
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 431, 476);
        menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        mntmFile = new JMenuItem("File");
        menuBar.add(mntmFile);
    }


    private void makePnls(){

        pnlButtons = new JPanel();
        GridBagConstraints gbc_pnlButtons = new GridBagConstraints();
        gbc_pnlButtons.gridwidth = 2;
        gbc_pnlButtons.anchor = GridBagConstraints.NORTH;
        gbc_pnlButtons.gridx = 0;
        gbc_pnlButtons.gridy = 5;
        pnlButtons.setLayout(new MigLayout("", "[97px][71px][95px][113px][79px][89px][111px]", "[23px][][][][][][][][]"));

        pnlSldB = new JPanel();
        GridBagConstraints gbc_pnlSldB = new GridBagConstraints();
        gbc_pnlSldB.anchor = GridBagConstraints.NORTH;
        gbc_pnlSldB.insets = new Insets(0, 0, 5, 5);
        gbc_pnlSldB.fill = GridBagConstraints.HORIZONTAL;
        gbc_pnlSldB.gridx = 0;
        gbc_pnlSldB.gridy = 3;

        colorShow = new JPanel();
        colorShow.setBackground(Color.GRAY);
        GridBagConstraints gbc_colorShow = new GridBagConstraints();
        gbc_colorShow.gridheight = 3;
        gbc_colorShow.insets = new Insets(0, 0, 5, 0);
        gbc_colorShow.fill = GridBagConstraints.BOTH;
        gbc_colorShow.gridx = 1;
        gbc_colorShow.gridy = 1;
        colorShow.setOpaque(true);
        colorShow.setDoubleBuffered(true);


        pnlSldG = new JPanel();
        GridBagConstraints gbc_pnlSldG = new GridBagConstraints();
        gbc_pnlSldG.gridheight = 2;
        gbc_pnlSldG.insets = new Insets(0, 0, 5, 5);
        gbc_pnlSldG.fill = GridBagConstraints.BOTH;
        gbc_pnlSldG.gridx = 0;
        gbc_pnlSldG.gridy = 1;


        pnlSldR = new JPanel();
        GridBagConstraints gbc_pnlSldR = new GridBagConstraints();
        gbc_pnlSldR.insets = new Insets(0, 0, 5, 5);
        gbc_pnlSldR.fill = GridBagConstraints.BOTH;
        gbc_pnlSldR.gridx = 0;
        gbc_pnlSldR.gridy = 0;


        pnlSldAlpha = new JPanel();
        GridBagConstraints gbc_pnlSldAlpha = new GridBagConstraints();
        gbc_pnlSldAlpha.insets = new Insets(0, 0, 5, 5);
        gbc_pnlSldAlpha.fill = GridBagConstraints.BOTH;
        gbc_pnlSldAlpha.gridx = 0;
        gbc_pnlSldAlpha.gridy = 4;

        pnlSldR.add(sldR);
        pnlSldG.add(sldG);
        pnlSldB.add(sldB);
        pnlSldAlpha.add(sldAlpha);
        pnlButtons.add(btnButtonLabels, "cell 0 0,alignx left,aligny center");
        pnlButtons.add(btnButtonContours, "cell 1 0,alignx left,aligny center");
        pnlButtons.add(label, "cell 4 0,alignx left,aligny center");
        pnlButtons.add(btnButtonsText, "cell 0 1,alignx left,aligny center");
        pnlButtons.add(btnText, "cell 0 3,alignx left,aligny center");
        pnlButtons.add(btnPanels, "cell 1 3,alignx left,aligny center");
        pnlButtons.add(btnSideListForeground, "cell 0 5");
        pnlButtons.add(btnSideList, "cell 1 5,alignx left,aligny center");
        pnlButtons.add(btnBackground, "cell 0 8,alignx left,aligny center");
        pnlButtons.add(btnForeground, "cell 1 8");

        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        GridBagLayout gbl_contentPane = new GridBagLayout();
        gbl_contentPane.columnWidths = new int[]{0, 0, 0};
        gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0};
        gbl_contentPane.columnWeights = new double[]{1.0, 1.0, Double.MIN_VALUE};
        gbl_contentPane.rowWeights = new double[]{1.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
        contentPane.setLayout(gbl_contentPane);

        contentPane.add(pnlButtons, gbc_pnlButtons);
        contentPane.add(pnlSldB, gbc_pnlSldB);
        contentPane.add(colorShow, gbc_colorShow);
        contentPane.add(pnlSldG, gbc_pnlSldG);
        contentPane.add(pnlSldR, gbc_pnlSldR);
        contentPane.add(pnlSldAlpha, gbc_pnlSldAlpha);

        pnlButtons.repaint();
        pnlSldAlpha.repaint();
        pnlSldR.repaint();
        pnlSldB.repaint();
        pnlSldG.repaint();
        btnPanels.repaint();
        contentPane.repaint();

    }

    private void makeSliders(){
        sldR = new JSlider();
        sldR.setMaximum(240);
        sldR.setMajorTickSpacing(50);
        sldR.setSnapToTicks(true);
        sldR.setPaintTicks(true);
        sldR.setPaintLabels(true);
        sldR.setValue(104);

        sldG = new JSlider();
        sldG.setMaximum(240);
        sldG.setSnapToTicks(true);
        sldG.setPaintTicks(true);
        sldG.setPaintLabels(true);
        sldG.setMajorTickSpacing(50);
        sldG.setValue(104);

        sldB = new JSlider();
        sldB.setMaximum(240);
        sldB.setSnapToTicks(true);
        sldB.setPaintTicks(true);
        sldB.setPaintLabels(true);
        sldB.setMajorTickSpacing(50);
        sldB.setValue(104);

        sldAlpha = new JSlider();
        sldAlpha.setMaximum(240);
        sldAlpha.setSnapToTicks(true);
        sldAlpha.setPaintTicks(true);
        sldAlpha.setPaintLabels(true);
        sldAlpha.setMajorTickSpacing(50);
        sldAlpha.setValue(240);
    }


    private void makeBtns(){
        btnButtonLabels = new JButton("Button Labels");
        btnButtonContours = new JButton("Button Contours");
        label = new JLabel("");
        btnButtonsText = new JButton("Buttons Text");
        btnText = new JButton("Top Menu Labels");
        btnPanels = new JButton("Top Menu");
        btnSideListForeground = new JButton("Side List Foreground");
        btnSideList = new JButton("Side List Background");
        btnBackground = new JButton("Background");
        btnForeground = new JButton("Foreground");
    }


    public JPanel getContentPane() {
        return contentPane;
    }

    protected JPanel getColorShow() {
        return colorShow;
    }

    protected JPanel getPnlButtons() {
        return pnlButtons;
    }

    protected JPanel getPnlSldG() {
        return pnlSldG;
    }

    protected JPanel getPnlSldB() {
        return pnlSldB;
    }

    protected JPanel getPnlSldAlpha() {
        return pnlSldAlpha;
    }

    protected JPanel getPnlSldR() {
        return pnlSldR;
    }

    protected JButton getBtnBackground() {
        return btnBackground;
    }

    protected JButton getBtnText() {
        return btnText;
    }

    protected JButton getBtnPanels() {
        return btnPanels;
    }

    protected JButton getBtnButtonsText() {
        return btnButtonsText;
    }

    protected JButton getBtnSideList() {
        return btnSideList;
    }

    protected JButton getBtnButtonLabels() {
        return btnButtonLabels;
    }

    protected JButton getBtnButtonContours() {
        return btnButtonContours;
    }

    protected JButton getBtnSideListForeground() {
        return btnSideListForeground;
    }

    protected JButton getBtnForeground() {
        return btnForeground;
    }

    protected JLabel getLabel() {
        return label;
    }

    protected JSlider getSldG() {
        return sldG;
    }

    protected JSlider getSldB() {
        return sldB;
    }

    protected JSlider getSldAlpha() {
        return sldAlpha;
    }

    protected JSlider getSldR() {
        return sldR;
    }
}

This is the wrapper (just for readability):

import java.awt.Color;

import javax.swing.JComponent;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class ThemeWrapper {

    private Theme theme;
    private ThemeBuilder builder;
    private int r, g, b, a;

    public ThemeWrapper(Theme theme) {
        this.builder = new ThemeBuilder();
        this.builder.setVisible(true);
        this.theme = new Theme();
        this.r = builder.getSldR().getValue();
        this.g = builder.getSldG().getValue();
        this.b = builder.getSldB().getValue();
        this.a = builder.getSldAlpha().getValue();
        initializeBehaviors();
    }

    public void initializeBehaviors(){

        builder.getSldR().addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {

                if(e.getSource() == builder.getSldR()){
                    changeColor();
                }
            }
        });

        builder.getSldG().addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {

                if(e.getSource() == builder.getSldG()){
                    changeColor();
                }
            }
        });

        builder.getSldB().addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {

                if(e.getSource() == builder.getSldB()){
                    changeColor();
                }
            }
        });

        builder.getSldAlpha().addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {

                if(e.getSource() == builder.getSldAlpha()){
                    changeColor();
                }
            }
        });
    }

    private void changeColor(){
        r = builder.getSldR().getValue();
        g = builder.getSldG().getValue();
        b = builder.getSldB().getValue();
        a = builder.getSldAlpha().getValue();
        applyColor(builder.getColorShow());
    }

    private void applyColor(JComponent component){
        component.setBackground(new Color(r, g, b, a));
        component.repaint();
    }


    public void disposeFrames(){
        builder.setVisible(false);
        builder.dispose();
    }


    public Theme getTheme() {
        return theme;
    }
}

Solution

  • No I just figured it out, it's because you have to repaint the frame!

    private void changeColor(){
        r = builder.getSldR().getValue();
        g = builder.getSldG().getValue();
        b = builder.getSldB().getValue();
        a = builder.getSldAlpha().getValue();
        applyColor(builder.getColorShow());
        builder.repaint();
    }