2

I have two scroll panes that I am trying to set the scroll bars to always be visible. For some reason when I try to use the code (scroll code) below, I get an error that "The method setHorizontalScrollBarPolicy(int) is undefined for the type JTextPane". Any suggestions?

Scroll Code:

scrollPane.setHorizontalScrollBarPolicy(
   JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(
   JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

Code where I create the the scroll panes:

JPanel ScrollPanes = new JPanel();
frame.getContentPane().add(ScrollPanes, BorderLayout.CENTER);
ScrollPanes.setLayout(new GridLayout(0, 2, 0, 0));

JTextPane textPane_0 = new JTextPane();
ScrollPanes.add(textPane_0);
textPane_0.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
textPane_0.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

JTextPane textPane_1 = new JTextPane();
ScrollPanes.add(textPane_1);

Full Code:

package swing;

import java.awt.EventQueue;

public class Swing {

    private JFrame frame;
    private JTextField textField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Swing window = new Swing();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Swing() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 600, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel TopApps = new JPanel();
        frame.getContentPane().add(TopApps, BorderLayout.NORTH);
        TopApps.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));


        JComboBox comboBox = new JComboBox();
        comboBox.setPreferredSize(new Dimension(100, 20));
        TopApps.add(comboBox);

        textField = new JTextField();
        TopApps.add(textField);
        textField.setColumns(20);

        JScrollPane scrollPanes = new JScrollPane();
        frame.getContentPane().add(scrollPanes, BorderLayout.CENTER);
        scrollPanes.setLayout(new GridLayout(0, 2, 0, 0));

        JTextPane textPane_0 = new JTextPane();
        scrollPanes.add(textPane_0);
        textPane_0.setBorder(BorderFactory.createLoweredBevelBorder());
        scrollPanes.setHorizontalScrollBarPolicy(
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
            scrollPanes.setVerticalScrollBarPolicy(
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        JTextPane textPane_1 = new JTextPane();
        scrollPanes.add(textPane_1);
        textPane_1.setBorder(BorderFactory.createLoweredBevelBorder());
        scrollPanes.setHorizontalScrollBarPolicy(
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
            scrollPanes.setVerticalScrollBarPolicy(
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        JPanel bottomPanel = new JPanel();
        frame.getContentPane().add(bottomPanel, BorderLayout.SOUTH);
        bottomPanel.setLayout(new GridLayout(2, 1, 5, 0));
        bottomPanel.setBorder(BorderFactory.createLoweredBevelBorder());

        JTextPane bText = new JTextPane();
        bottomPanel.add(bText);
        bText.setBorder(BorderFactory.createLoweredBevelBorder());

        JLabel status = new JLabel("Status");
        bottomPanel.add(status);
        status.setHorizontalAlignment(JLabel.CENTER);

    }

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Kevin Schultz
  • 886
  • 3
  • 20
  • 42

2 Answers2

7

textPane_0 is instance of JTextPane not JScrollPane so it doesn't have setHorizontalScrollBarPolicy method.

You probably want to wrap your textPane_0 and textPane_1 in new instances of JScrollPane, set its scrollbar properties and add these to your frame.

In other words use

JTextPane textPane_0 = new JTextPane();
JScrollPane jScrollPane1 = new JScrollPane(textPane_0);//warp text pane
//and set properties of wrapper
jScrollPane1.setHorizontalScrollBarPolicy(
        JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jScrollPane1.setVerticalScrollBarPolicy(
        JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
ScrollPanes.add(jScrollPane1);


JTextPane textPane_1 = new JTextPane();
JScrollPane jScrollPane2 = new JScrollPane(textPane_1);
jScrollPane2.setHorizontalScrollBarPolicy(
        JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jScrollPane2.setVerticalScrollBarPolicy(
        JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
ScrollPanes.add(jScrollPane2);

DEMO:

class Demo {
    public static void main(String[] args) {

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500,400);

        JPanel ScrollPanes = new JPanel();
        frame.getContentPane().add(ScrollPanes, BorderLayout.CENTER);
        ScrollPanes.setLayout(new GridLayout(0, 2, 0, 0));

        JTextPane textPane_0 = new JTextPane();
        JScrollPane jScrollPane1 = new JScrollPane(textPane_0);
        jScrollPane1.setHorizontalScrollBarPolicy(
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        jScrollPane1.setVerticalScrollBarPolicy(
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        ScrollPanes.add(jScrollPane1);


        JTextPane textPane_1 = new JTextPane();
        JScrollPane jScrollPane2 = new JScrollPane(textPane_1);
        jScrollPane2.setHorizontalScrollBarPolicy(
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        jScrollPane2.setVerticalScrollBarPolicy(
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        ScrollPanes.add(jScrollPane2);


        frame.setVisible(true);


    }
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • That also gave the following error when compiled. I have edited with the full code above. . java.lang.ClassCastException: layout of JScrollPane must be a ScrollPaneLayout at javax.swing.JScrollPane.setLayout(Unknown Source) at swing.Swing.initialize(Swing.java:78) at swing.Swing.(Swing.java:52) at swing.Swing$1.run(Swing.java:39) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) – Kevin Schultz Jun 26 '14 at 23:11
  • 1
    Did you change `JPanel ScrollPanes = new JPanel();` to `JScrollPane ScrollPanes = new JScrollPane();`? If so then does changing it back to `JPanel` solves your problem? – Pshemo Jun 26 '14 at 23:13
4
JPanel ScrollPanes = new JPanel();

Should be:

JScrollPane ScrollPanes = new JScrollPane();

Or better, using correct nomenclature:

JScrollPane scrollPanes = new JScrollPane();

Then change:

textPane_0.setHorizontalScrollBarPolicy(
    JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
textPane_0.setVerticalScrollBarPolicy(
    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

To:

scrollPanes.setHorizontalScrollBarPolicy(
    JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPanes.setVerticalScrollBarPolicy(
    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Adding that appeared correct but gave the following error when compiled. I have edited with the full code above. . java.lang.ClassCastException: layout of JScrollPane must be a ScrollPaneLayout at javax.swing.JScrollPane.setLayout(Unknown Source) at swing.Swing.initialize(Swing.java:78) at swing.Swing.(Swing.java:52) at swing.Swing$1.run(Swing.java:39) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) – Kevin Schultz Jun 26 '14 at 23:08