Hye guys...im trying to create ScrollBar Horizontal on whole frame, i keep trying and do some research but still fail...Please help me...What should i add. This is the code.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class listOfMovies extends JFrame implements ActionListener {



	private JButton jbtBatman, jbtAngels, jbtFaster, jbtDueDate, jbtHuman, jbtFour, jbtWarrior, jbtWater;
	private JLabel jlbWelcome, jlbBatman, jlbAngels, jlbFaster, jlbDueDate, jlbHuman, jlbFour, jlbWarrior, jlbWater;
	private JPanel panel = new JPanel ();
//	private JScrollPane scroll = new JScrollPane (panel, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneContants.HORIZONTAL_SCROLLBAR_NEVER);
//	private container con;

	Font f = new Font("Times",Font.BOLD,20);

		public listOfMovies () {

			setLayout(null);

	//		con.add(scroll);
			// initialize the label
    		jlbWelcome = new JLabel ("Movies");
			add(jlbWelcome);
			jlbWelcome.setBounds(130,10,500,100);
    		jlbWelcome.setHorizontalAlignment(jlbWelcome.CENTER );
    		jlbWelcome.setFont(f);

			// initialize the button
			jbtBatman = new JButton ();
			jbtAngels = new JButton ();
			jbtFaster = new JButton ();
			jbtDueDate = new JButton ();
			jbtHuman = new JButton ();
			jbtFour = new JButton ();
			jbtWarrior = new JButton ();
			jbtWater = new JButton ();

			// set icon for button
        	jbtBatman.setIcon(new ImageIcon("C:\\Users\\User\\Desktop\\OOP project\\img\\icon\\1.jpg")); // NOI18N
        	jbtAngels.setIcon(new ImageIcon("C:\\Users\\User\\Desktop\\OOP project\\img\\icon\\1.jpg"));
        	jbtFaster.setIcon(new ImageIcon("C:\\Users\\User\\Desktop\\OOP project\\img\\icon\\1.jpg"));
        	jbtDueDate.setIcon(new ImageIcon("C:\\Users\\User\\Desktop\\OOP project\\img\\icon\\1.jpg"));
        	jbtHuman.setIcon(new ImageIcon("C:\\Users\\User\\Desktop\\OOP project\\img\\icon\\1.jpg"));
        	jbtFour.setIcon(new ImageIcon("C:\\Users\\User\\Desktop\\OOP project\\img\\icon\\1.jpg"));
        	jbtWarrior.setIcon(new ImageIcon("C:\\Users\\User\\Desktop\\OOP project\\img\\icon\\1.jpg"));
        	jbtWater.setIcon(new ImageIcon("C:\\Users\\User\\Desktop\\OOP project\\img\\icon\\1.jpg"));

			// add all button in the frame
        	add(jbtBatman);
        	add(jbtAngels);
        	add(jbtFaster);
        	add(jbtDueDate);
        	add(jbtHuman);
        	add(jbtFour);
        	add(jbtWarrior);
        	add(jbtWater);

			// set positions for buttons
        	jbtBatman.setBounds(67, 100, 180, 288);
        	jbtAngels.setBounds(290, 100, 180, 288);
        	jbtFaster.setBounds(508, 100, 180, 288);
        	jbtDueDate.setBounds(67, 450, 180, 288);
        	jbtHuman.setBounds(290, 450, 180, 288);
        	jbtFour.setBounds(508, 450, 180, 288);
        	jbtWarrior.setBounds(67, 100, 180, 288);
        	jbtWater.setBounds(290, 100, 180, 288);


			// register listener
        	jbtBatman.addActionListener(this);
        	jbtAngels.addActionListener(this);
        	jbtFaster.addActionListener(this);
        	jbtDueDate.addActionListener(this);
        	jbtHuman.addActionListener(this);
        	jbtFour.addActionListener(this);
        	jbtWarrior.addActionListener(this);
        	jbtWater.addActionListener(this);

			// initialize all labels
        	jlbBatman = new JLabel ("Batman : The Dark Knight");
        	jlbAngels = new JLabel ("Angels and Demons");
        	jlbFaster = new JLabel ("Faster");
        	jlbDueDate = new JLabel ("Due Date");
        	jlbHuman = new JLabel ("Human Target");
        	jlbWarrior = new JLabel ("Warrior Way");
        	jlbFour = new JLabel ("I am Number Four");
        	jlbWater = new JLabel ("Water");

			// add labels in the frame
        	add(jlbBatman);
        	add(jlbAngels);
        	add(jlbFaster);
        	add(jlbDueDate);
        	add(jlbHuman);
        	add(jlbWarrior);
        	add(jlbFour);
        	add(jlbWater);

			// set positions for the labels
        	jlbBatman.setBounds(90, 393, 148, 18);
        	jlbAngels.setBounds(331, 393, 126, 18);
        	jlbFaster.setBounds(584, 393, 126, 18);
        	jlbDueDate.setBounds(90, 700, 126, 18);
        	jlbHuman.setBounds(331, 700, 126, 18);
        	jlbWarrior.setBounds(584, 700, 126, 18);
        	jlbFour.setBounds(331, 393, 126, 18);
        	jlbWater.setBounds(584, 393, 126, 18);


    		}


    		public void actionPerformed(ActionEvent e) {
       			 if ((e.getSource() == jbtBatman)) {

    				bookBatman frame = new bookBatman ();
   					frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
					frame.setTitle("Book Batman : The Dark Knight"); //set title of thewindow
					frame.setSize(800,600); //size of the window
					frame.setVisible(true); //visible the window
					frame.setLocationRelativeTo (null); //center the window

  					}

  				if((e.getSource() == jbtAngels))  {

  					bookAngels frame=new bookAngels ();
   					frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
					frame.setTitle("Booking Movie"); //set title of thewindow
					frame.setSize(800,600); //size of the window
					frame.setVisible(true); //visible the window
					frame.setLocationRelativeTo (null); //center the window

  					}

  				if((e.getSource() == jbtFaster))  {

  					bookFaster frame=new bookFaster ();
   					frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
					frame.setTitle("Booking Movie"); //set title of thewindow
					frame.setSize(800,600); //size of the window
					frame.setVisible(true); //visible the window
					frame.setLocationRelativeTo (null); //center the window

					}

}

    			public static void main (String [] args) {

				/*	listOfMovies frame=new listOfMovies ();
   					frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
					frame.setTitle("List of Movies"); //set title of thewindow
					frame.setSize(800,600); //size of the window
					frame.setVisible(true); //visible the window
					frame.setLocationRelativeTo (null); //center the window

					JPanel panel = new JPanel ();
    				JScrollPane spane = new JScrollPane(frame);
    				panel.add(frame); */


					listOfMovies frame = new listOfMovies ();
    				JPanel panel = new JPanel();
        			JScrollPane scrollBar = new JScrollPane(panel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        			//JFrame frame = new JFrame("AddScrollBarToJFrame");
        			frame.add(scrollBar);
        			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        			frame.setSize(800,600);
        			frame.setVisible(true);


    				}
}

Recommended Answers

All 3 Replies

Just reading the not-commented stuff I see around line 165 you create a JPanel, create a JScrollPane using that panel, add the JScrollPane to you (main?) frame.
Problem is, I don't see you adding anything to the panel inside the scrollpane.

If you want a window with everything in it scrollable you need to:
create a JPanel
put everything into that panel
create a JScrollPane using that panel
add the scrollpane to your window

http://download.oracle.com/javase/tutorial/uiswing/components/scrollpane.html

erm...should i do like this? sry im newbie...im learning...

JPanel panel = new JPanel();
listOfMovies frame=new listOfMovies ();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("List of Movies"); //set title of thewindow
frame.setSize(800,600); //size of the window
frame.setVisible(true); //visible the window
frame.setLocationRelativeTo (null); //center the window
JScrollPane scrollBar = new JScrollPane (panel);
scrollBar.add(panel);

1/ put Image or ImageIcon to the Java Package,
- if you are using some IDE then just create new Java Package
- otherwise http://download.oracle.com/javase/tutorial/deployment/index.html

2/ for this AbsoluteLayoutZOO you can use GridLayot or BoxLaout http://download.oracle.com/javase/tutorial/uiswing/layout/index.html

3/ you can set different Layout for each JPanel

for public listOfMovies() {.... }

1/ remove JFrame + JSchrollPane + JPanel from main method and replace that just with

public static void main(String[] args) {
        Runnable doRun = new Runnable() {

            public void run() {
                listOfMovies jf = new listOfMovies();
            }
        };
        SwingUtilities.invokeLater(doRun);
    }

2/ create a JPanel put here JLabels + JButtons

3/ create JScrollPane

scrollBar.setViewportView(panel);

and so on

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.