Hi everyone, i need your help, i've used a panel and i've a label(which acts as a background image).. There's a long text which is placed in a label. now i want to view this text and the background image but in this attempt i cant see any scrollbars even though i'm using JScrollPane.. Thanks in advance

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

public class Trial2 extends JFrame implements ActionListener
{
String a="<html><body><font color=blue>Description:<br><br><u>College Profile:</u><br><p>One of the oldest colleges affiliated to the University of Mumbai and the first of the twenty four Institutions established <br>and managed by the Hyderabad (Sind) National Collegiate Board (HSNCB), National College was established in <br>Independent India in 1949 under the leadership of Late Principal M. Kundnani and Late Barrister H. G. Advani -<br> two visionaries who had migrated from Sind, Pakistan to Mumbai after Independence. The college, however, traces its <br>origin to D. G. National College, which was established in Hyderabad (Sind) in 1922 with the support and guidance of <br>Dr. Annie Besant and Rishi Dayaram Gidumal.</p><br><p>R. D. National College caters to the educational needs of varied sections of the society, especially the minorities and the <br>under privileged. Fifty percent of its students belong to the minority communities like Sindhi, Muslim, Christian and Sikh.<br>Today, in its 64th year, the College has become an excellent centre of higher learning that aims at academic excellence <br>and emphasizes secular and community education.</p><br><p>Over the years, the college has grown to meet the challenges and needs of time and provides high quality education to <br>students at the Pre Degree Plus two level and the Degree level in the Science, Arts and Commerce streams. <br>The college also offers career oriented integrated Degree Courses in Mass Media, Management, Accounts and Finance, <br>Information Technology, Bio Technology and Computer Science and Post Graduate and Research Courses in Chemistry, <br>Physics, English Literature, Psychology Computer Science Information Technology and Bio Technology and Dual Degree <br>programs in Bio-Technology, Heritage Management and Music Appreciation.</p><br><p>The infrastructural facilities of the college include spacious laboratories and Multimedia classrooms for academic activities, <br>a multipurpose, air conditioned gymkhana and a floodlit multi Sports-Basketball / Throw ball / Volleyball Court for <br>sports activities as well as a well equipped air conditioned Auditorium and Conference Halls for co-curricular and <br>extracurricular activities. A botanical garden with more than 100 species of plants is a proud possession of the college. <br>The well equipped and spacious library with more than 80,000 volumes of books, journals and numerous periodicals<br> and newspapers and internet facility, provides excellent opportunities for academic exploration and research to the <br>students.</p><br><p>The college has created various academic and community management instruments which include the House System, <br>the Tutor System, the Honors' Program, and other community outreach programs which include the adoption of a village <br>and collaboration with NGOs, Recycling of water, solar energy & recycling of college water are some of the activities to<br> inculcate social awareness amongst the students.<br>The Community outreach programs of the college enable students to work and interact with people with varied <br>backgrounds. Working with NGOs has exposed students to meaningful community welfare activities and has helped <br>develop their managerial skills.</p><br><p>The path breaking initiatives such as the Naturalists' Meet, Community Awareness and Assessment Survey Program <br>(CAASP), Gratitude Week and Network of Academic Administrators were milestone events initiated during the Diamond <br>Jubilee year.<br><br><u>Official Website:<br></u>www.rdnational.ac.in<br></font></p></body></html>";
JLabel l1,l2;
JButton b1;
JPanel p1;
JDialog jd1;
JScrollPane jsp;
int vv=JScrollPane.VERTICAL_SCROLLBAR_ALWAYS;
int hh=JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS;
ImageIcon m1;

    public Trial2()
    {
    Container con=getContentPane();
    con.setLayout(new FlowLayout());

    b1=new JButton("Click");
    con.add(b1);


    b1.addActionListener(this);
    }

        public void actionPerformed(ActionEvent ae)
        {
            if(ae.getSource()==b1)
            {
            p1=new JPanel();

            m1=new ImageIcon("new2014(1).jpg");
            l1=new JLabel(m1);

            jd1=new JDialog();
            jsp=new JScrollPane(l2,vv,hh);
            //jsp.setViewportView(l2); Also used this but not working

            l2=new JLabel();
            l2.setText(a);

            jd1.add(l2);
            jd1.setSize(720,550);
            jd1.setVisible(true);

            p1.add(l1);
            p1.add(jsp,BorderLayout.CENTER);
            p1.setVisible(true);
            p1.setPreferredSize(new Dimension(720,550));

            jd1.add(p1);
            jd1.setResizable(false);
            }
        }

public static void main(String args[])
{
Trial2 t=new Trial2();
t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t.setSize(300,300);
t.setVisible(true);
}
}

Dani AI

Generated

As noted, the immediate bug is creating the JScrollPane with l2 still null. That is only part of the problem: l2 gets added directly to the JDialog, then later a panel containing the JScrollPane is added. Also p1 is left with its default FlowLayout while adding the scroll pane with a BorderLayout constraint — so the scroll pane never becomes the properly laid-out viewport component. Those ordering and layout mistakes prevent any scrollbar from ever being used.

Why scrollbars still do not appear: a JScrollPane only shows scrollbars when its viewport actually contains the view component and that view’s preferred size is larger than the viewport. A JLabel with embedded HTML is not ideal for long scrollable text — it either expands to fit or gets placed outside the scroll hierarchy. Move the text-component initialization before creating the JScrollPane (as suggested) and put the text component into the scrollpane (either via the constructor or setViewportView, as mentions). Use a proper scrollable text component (JEditorPane/JTextPane for HTML, or JTextArea) and set the container layout to BorderLayout so the scrollpane fills the dialog.

A workable approach (paint the background in a custom panel, use a JEditorPane for HTML, make the editor and viewport non-opaque so the background shows):

class ImagePanel extends JPanel {
    private final Image img;
    ImagePanel(ImageIcon icon) { img = icon.getImage(); setLayout(new BorderLayout()); }
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
    }
}

JEditorPane editor = new JEditorPane("text/html", htmlString);
editor.setEditable(false);
editor.setOpaque(false);

JScrollPane scroll = new JScrollPane(editor);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
scroll.getViewport().setOpaque(false);

ImagePanel panel = new ImagePanel(new ImageIcon("new2014(1).jpg"));
panel.add(scroll, BorderLayout.CENTER);

JDialog dialog = new JDialog();
dialog.setContentPane(panel);
dialog.setSize(720, 550);
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);

Quick checklist: initialize the text component before the JScrollPane; do not add the same component to two containers; use p1.setLayout(new BorderLayout()) when adding the scrollpane with CENTER; call pack()/setSize() and only then setVisible(true); if scrollbars still fail, compare getPreferredSize() of the view versus the scroll viewport and call revalidate()/repaint() after changes.

Recommended Answers

All 5 Replies

Line 39, l2 is uninitialised, so its value is null - you create the scroll pane with null as its content

Lines 42,43 you initialise l2, but it's too late.

So what am i suppose to do?? And how am i suppose to do?

Move

l2=new JLabel();
            l2.setText(a);

before line 39

i did, but i still cant see my scrollbars

To set a Scrollbar to some other widget I have an example:

JTextArea directionJA = new JTextArea();
JScrollPane directionJS = new JScrollPane();
directionJS.setViewportView(directionJA);
directionJS.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
directionJS.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

I'm setting a vertical scrollbar to a text area. I see you are setting both types of scrollbar. If giving it to constructor class doesn't work try to set scrollbar policy to your scrollpane.
Try and tell me if it worked.

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.