Question

Though the statements for producing a slider has been put in the code, the slider does not appear in the GUI. What do I have to modify or add in the code for the slider to appear in the GUI? All the other Swing components defined in the program appear except for the slider.

package pkTopic5T15;

import javax.swing.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Topic5T15 {
    public int top1 = 60;
    public int top2 = 300;
    public JPanel pnlLeft;
    public JPanel pnlRight;
    public int Initialht1 = 100;
    public int Initialht2 = 100;
    public JSlider sldChangePanels;

    public static void main(String[] args) {
        Topic5T15 My515 = new Topic5T15();
        My515.go();
    }

    public void go() {
        GUI515 My515 = new GUI515();
    }

    class GUI515 extends JFrame {
        private JLabel lblHeading;
        private JButton btnPanLeft;
        private JButton btnPanRight;
        public int ht1;
        public int ht2;

        protected GUI515() {
            this.setSize(800, 900);
            this.setLocation(100, 100);
            this.setTitle("515");
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setLayout(null);
            Font f1 = new Font("Monospaced", Font.BOLD, 16);
            Font f2 = new Font("Dialog", Font.BOLD, 24);
            lblHeading = new JLabel("Testing    Panels");
            lblHeading.setBounds(10, 10, 300, 50);
            lblHeading.setFont(f2);
            pnlLeft = new JPanel();
            pnlLeft.setLayout(null);
            pnlLeft.setBounds(10, top1, 300, Initialht1);
            pnlLeft.setBackground(Color.RED);
            btnPanLeft = new JButton("One");
            btnPanLeft.setFont(f1);
            btnPanLeft.setBounds(0, 0, 100, 50);
            ClickOne c = new ClickOne();
            btnPanLeft.addActionListener(c);
            pnlLeft.add(btnPanLeft);
            this.add(pnlLeft);
            pnlRight = new JPanel();
            pnlRight.setLayout(null);
            pnlRight.setBounds(350, top2, 300, Initialht2);
            pnlRight.setBackground(Color.GREEN);
            btnPanRight = new JButton("Two");
            btnPanRight.setBounds(0, 0, 100, 50);
            btnPanRight.setFont(f1);
            btnPanRight.addActionListener(c);
            pnlRight.add(btnPanRight);
            this.add(pnlRight);
            sldChangePanels = new JSlider(-100, 100, 1);
            sldChangePanels.setBounds(50, 800, 400, 50);
            sldChangePanels.setMajorTickSpacing(20);
            sldChangePanels.setPaintLabels(true);
            SliderListener slis = new SliderListener();
            sldChangePanels.addChangeListener(slis);
            this.add(sldChangePanels);
            this.add(lblHeading);
            this.setVisible(true);
        }

        private class ClickOne implements ActionListener
        // This is an inner class; contained within GUI
        {
            protected ClickOne() {
            }

            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == btnPanLeft) {
                    btnPanLeft.setText("OUCH1");
                    top1 = top1 + 20;
                    pnlLeft.setBounds(10, top1, 300, 100);
                }
                if (e.getSource() == btnPanRight) {
                    btnPanRight.setText("OUCH2");
                    top2 = top2 - 20;
                    pnlRight.setBounds(350, top2, 300, 100);
                }
            }
        }

        private class SliderListener implements ChangeListener
        // This is an inner class; contained within GUI
        {
            protected SliderListener() {
            }

            public void stateChanged(ChangeEvent e) {
                ht1 = Initialht1 + sldChangePanels.getValue();
                ht2 = Initialht2 - sldChangePanels.getValue();
                pnlLeft.setBounds(10, top1, 300, ht1);
                pnlRight.setBounds(350, top2, 300, ht2);
            }
        }
    }
}
Was it helpful?

Solution

With GUI programming, many times, the elements are being rendered but are not seen. For example they are rendered outside of the screen, or outside of the visible portion of the window, or they are rendered but they are hidden by other elements, or they are rendered at a zero size.

Check if any of these are true for you.

HTH.

OTHER TIPS

Don't use null layout at all if used then set the bounds for each component that is missing for slider in your code.that's why slider does not appear.

Use proper layout and redesign your application again.

It's worth reading A Visual Guide to Layout Managers

EDIT

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top