Changing the orientation of a JSlider

You can either specify the orientation when you create your JSlider object or afterwards with the method setOrientation.

Main.java:

import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
   
public class Main extends JFrame implements ActionListener {
   private JSlider slider;
 
   public Main() {
      getContentPane().setLayout(new FlowLayout());
 
      JMenuBar menuBar = new JMenuBar();
      JMenu menu = new JMenu("JSlider Orientation");
      JMenuItem menuItem1 = new JMenuItem("HORIZONTAL");
      JMenuItem menuItem2 = new JMenuItem("VERTICAL");
      menu.add(menuItem1);
      menu.add(menuItem2);
      menuItem1.addActionListener(this);
      menuItem2.addActionListener(this);
      menuBar.add(menu); 
      setJMenuBar(menuBar);
      
      slider = new JSlider(JSlider.HORIZONTAL, 0, 30, 15);
 
      getContentPane().add(slider);

      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent event) {
            System.exit(0);   
         }      
      });
 
      setSize(400, 400);
   }
 
   public static void main(String[] args) {
      (new Main()).show();
   }
 
   public void actionPerformed(ActionEvent ae) {
      if (ae.getActionCommand().equals("HORIZONTAL"))
         slider.setOrientation(JSlider.HORIZONTAL);
      else if (ae.getActionCommand().equals("VERTICAL"))
         slider.setOrientation(JSlider.VERTICAL);
   }
}