Hello,
I have a problem with slider which i want to create jlabels in a panel by sliding the slider and get the value but the jlabel doesn't show in jpanel.below is the code :
JPanel PanelBoxes; JPanel panel; JLabel c; public static void main(String[] args){ DividePanel div = new DividePanel(); div.go(); } public void go(){ JFrame frame = new JFrame("My Frame"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JSlider slide = new JSlider(); slide.addChangeListener(new SlidersListener()); JSlider slidev = new JSlider(JSlider.VERTICAL); //slidev.addChangeListener(new SlideListener()); PanelBoxes = new JPanel(); PanelBoxes.setPreferredSize(new Dimension(200,150)); panel = new JPanel(); panel.setBorder(BorderFactory.createLineBorder(Color.RED,2,false)); panel.add(BorderLayout.CENTER,PanelBoxes); frame.getContentPane().add(BorderLayout.NORTH,slide); frame.getContentPane().add(BorderLayout.WEST,slidev); frame.getContentPane().add(BorderLayout.EAST,new JPanel()); frame.getContentPane().add(BorderLayout.SOUTH,new JPanel()); frame.getContentPane().add(BorderLayout.CENTER,panel); frame.setSize(400, 300); frame.setVisible(true); } public class SlidersListener implements ChangeListener{ @Override public void stateChanged(ChangeEvent e) { JSlider source = (JSlider) e.getSource(); if(!source.getValueIsAdjusting()){ int count=source.getValue(); JOptionPane.showConfirmDialog(source,count); for(int i=0; i<2; i++){ c = new JLabel("asdasd"); c.setPreferredSize(new Dimension(100,30)); c.setBackground(Color.red); c.setOpaque(true); PanelBoxes.add(c); } } }
Thanks in advance guys