Matt Westlake
Matt Westlake

Reputation: 3651

scrollpane not working

I am trying to output a lot of data (membership record) into a JTextArea, and then dumping that into a scrollPane. Here is the code.

package Ginfo;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.io.*;


public class ReviewAll extends JFrame implements ActionListener {
    Container RA;
    JPanel ReviewAll;
    JTextArea output;
    JButton back, quit;

    Message m;
    ConnectInfo c;
    ObjectOutputStream oout;
    ObjectInputStream oin;

    public ReviewAll(ObjectOutputStream oout2, ObjectInputStream oin2,
            Message m2, ConnectInfo a) {
        super("Full results");
        m = m2;
        c = a;
        oout = oout2;
        oin = oin2;
        try {
            oout.reset();
        } catch (IOException e) {

        }
        RA = getContentPane();
        BuildGUI();
        RA.add(ReviewAll);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);

        try {
            m.type = Message.REVIEWALL;
            oout.writeObject(m);
            while (!m.response.equals("End of list")) {
                m = (Message) oin.readObject();
                output.append("Member: " + m.main + "\n");
                output.append("Invited by: " + m.invitedBy + " on " + m.when
                        + "\n");
                output.append("Recommended by: " + m.HDTH + "\n\n");
            }
            m.response = "";

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }

    private void BuildGUI() {
        ReviewAll = new JPanel();
        output = new JTextArea();
        output.setPreferredSize(new Dimension(400, 400));
        output.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(output);
        scrollPane
                .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        back = new JButton("Close window");
        quit = new JButton("Exit Program");

        ReviewAll.add(scrollPane, BorderLayout.NORTH);
        ReviewAll.add(back, BorderLayout.SOUTH);
        ReviewAll.add(quit, BorderLayout.SOUTH);

        back.addActionListener(this);
        quit.addActionListener(this);
    }

    public void actionPerformed(ActionEvent re) {
        if (re.getSource() == back) {
            new WhatToDo(oout, oin, m, c);
            dispose();
        } else if (re.getSource() == quit) {
            if (oout != null) {
                try {
                    oout.flush();
                    oout.close();
                    oin.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            System.exit(0);
        }
    }
}

when I do this though, i only see the first 5 record (enough to fill my JTextArea) and cannot scroll down to see the rest. for the record, i have m.response = "End of List" on the server code before it gets transferred to the client (listed below) when the record being sent is saved at list.size() and I know that works correctly. I know this is an issue with the scroller. Thank you in advanced for your help.

Upvotes: 3

Views: 740

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

I know this is an issue with the scroller.

The issue isn't with the scroller but with how you're setting the size or preferredSize of the JTextArea which is preventing it from expanding properly. Instead set it's columns and rows as this will tell the JScrollPane how many rows and columns it should view in its viewport.

i.e., change this:

ReviewAll = new JPanel();
output = new JTextArea();
output.setPreferredSize(new Dimension (400,400));

to this:

ReviewAll = new JPanel();

// ROW_COUNT & COL_COUNT are int constants defined elsewhere
output = new JTextArea(ROW_COUNT, COL_COUNT); // using whatever numbers work best.

// don't set the text area's preferredSize:
// output.setPreferredSize(new Dimension (400,400));

Of course it would be best to avoid magic numbers and instead use constants for the row and col counts.

Upvotes: 5

Related Questions