Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Text Not displaying in JTextArea contained in JScrollPane

807603Nov 4 2007 — edited Nov 4 2007
Hi,

I have a JScrollPane that contains a JTextArea.

I have it working flawlessly in one app and essentially copied the code to a new program and I cant do anything with the text area. its almost as if it isnt there.

I have tried to only contain the code that matters to this problem but most of it is just hacked up for a school assignment and I would have to add about 8 classes to allow everything to compile..

I wonder if somehow in my app i may have blocked the event thread preventing the Text Areas from repainting or if there is some other stupid error.

thanks in advance for any help.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.util.*;
import javax.swing.*;

/**
 * 
 */
public class GUI extends JFrame {

    private static final long serialVersionUID = -8606510166811306649L;
    private static GUI instance = null;

    private JPanel jContentPane = null;
    private JDesktopPane jDesktopPane = null;
    private JScrollPane performedOpsPane = null;
    private JTextArea performedOpsTextArea = null;
    private JScrollPane qPane = null;
    private JTextArea qTextArea = null;
    private JScrollPane logPane = null;
    private JTextArea logTextArea = null;
    private JLabel numMembersLabel = null;
    private JLabel numMembersValueLabel = null;
    private JLabel sequencerLabel = null;
    private JLabel dataValueLabel = null;
    private JLabel dataLabel = null;
    private JLabel performedOpsLabel = null;
    private JLabel opsInQLabel = null;
    private JLabel logLabel = null;

    private Data d = null;
    private Connection c = null;

    private GUI() {
        super();
        this.setSize(new Dimension(500, 600));
        this.setTitle("Multicast GUI");
        this.setContentPane(getJContentPane());
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    public void setData(Data d) {
        this.d = d;
    }

    public void setConnection(Connection c) {
        this.c = c;
    }

    /**
     * This method initializes jContentPane
     * 
     * @return javax.swing.JPanel
     */
    private JPanel getJContentPane() {
        if (jContentPane == null) {
            jContentPane = new JPanel();
            jContentPane.setLayout(new BorderLayout());
            jContentPane.add(getJDesktopPane(), BorderLayout.CENTER);
        }
        return jContentPane;
    }

    /**
     * This method initializes jDesktopPane
     * 
     * @return javax.swing.JDesktopPane
     */
    private JDesktopPane getJDesktopPane() {
        if (jDesktopPane == null) {

            numMembersLabel = new JLabel();
            numMembersLabel.setBounds(new Rectangle(20, 20, 70, 18));
            numMembersLabel.setText("Members: ");
            numMembersLabel.setHorizontalTextPosition(JLabel.RIGHT);
            numMembersValueLabel = new JLabel();
            numMembersValueLabel.setBounds(new Rectangle(91, 20, 250, 18));
            numMembersValueLabel.setText("00");
            numMembersValueLabel.setHorizontalAlignment(JLabel.LEFT);
            numMembersValueLabel.setHorizontalTextPosition(JLabel.LEFT);
            sequencerLabel = new JLabel();
            sequencerLabel.setBounds(new Rectangle(300, 50, 200, 18));
            sequencerLabel.setText("Sequencer ID: XXX");
            dataValueLabel = new JLabel();
            dataValueLabel.setBounds(new Rectangle(20, 50, 100, 18));
            dataValueLabel.setText("Value of Data: ");
            dataLabel = new JLabel();
            dataLabel.setBounds(new Rectangle(110, 50, 30, 18));
            dataLabel.setText("000");
            dataLabel.setHorizontalAlignment(JLabel.LEFT);
            dataLabel.setHorizontalTextPosition(JLabel.LEFT);
            performedOpsLabel = new JLabel();
            performedOpsLabel.setBounds(new Rectangle(20, 90, 200, 18));
            performedOpsLabel.setText("Performed Operations");
            opsInQLabel = new JLabel();
            opsInQLabel.setBounds(new Rectangle(270, 90, 200, 18));
            opsInQLabel.setText("Operations In Queue");
            qTextArea = new JTextArea();
            qTextArea.setLineWrap(true);
            qTextArea.setWrapStyleWord(true);
            qTextArea.setEditable(true);
            qPane = new JScrollPane(qTextArea);
            qPane.setBounds(new Rectangle(20, 110, 200, 250));
            qPane.add(qTextArea);
            performedOpsTextArea = new JTextArea();
            performedOpsTextArea.setLineWrap(true);
            performedOpsTextArea.setWrapStyleWord(true);
            performedOpsTextArea.setEditable(true);
            performedOpsTextArea.append("WHY IS THIS NOT SHOWING UP");
            performedOpsPane = new JScrollPane(performedOpsTextArea);
            performedOpsPane.setBounds(new Rectangle(270, 110, 200, 250));
            performedOpsPane.add(performedOpsTextArea);

            logLabel = new JLabel();
            logLabel.setBounds(new Rectangle(20, 380, 75, 18));
            logLabel.setText("Event Log");
            logTextArea = new JTextArea();
            logTextArea.append("Blah");
            logTextArea.setLineWrap(true);
            logTextArea.setWrapStyleWord(true);
            logTextArea.setEditable(false);
            logPane = new JScrollPane();
            logPane.setBounds(new Rectangle(20, 400, 450, 130));
            logPane.add(logTextArea);

            jDesktopPane = new JDesktopPane();
            jDesktopPane.add(qPane, null);
            jDesktopPane.add(performedOpsPane, null);
            jDesktopPane.add(numMembersLabel, null);
            jDesktopPane.add(numMembersValueLabel, null);
            jDesktopPane.add(sequencerLabel, null);
            jDesktopPane.add(dataValueLabel, null);
            jDesktopPane.add(dataLabel, null);
            jDesktopPane.add(performedOpsLabel, null);
            jDesktopPane.add(opsInQLabel, null);
            jDesktopPane.add(logLabel, null);
            jDesktopPane.add(logPane, null);
        }
        return jDesktopPane;
    }

    public static GUI getInstance() {
        if (instance == null) {
            instance = new GUI();
        }
        return instance;
    }

    public void updateQ() {
        Vector<Operation> v = d.getQ();
        synchronized (v) {
            int size = v.size();
            qTextArea.setText("");
            for (int i = 0; i < size; i++) {
                qTextArea.append(v.elementAt(i).toString());
            }
        }
    }

    public void appendPerformedOperation(Operation o) {
        performedOpsTextArea.append(o.toString() + "\n");
    }

    public void updateSequencerId() {
        sequencerLabel.setText("Sequencer ID: " + d.getCurSeqNum());
        repaintLabelLater(sequencerLabel);
    }

    public void updateDataValue() {
        dataLabel.setText("" + d.getCurDataValue());
        repaintLabelLater(dataLabel);
    }

    public void updateNumberOfMembers() {
        Vector<Integer> ids = c.getIds();
        numMembersValueLabel.setText("Total Number: " + ids.size() + "  IDS: "
                + ids.toString());
        repaintLabelLater(numMembersValueLabel);
    }

    public void addToLog(String s) {
        logTextArea.append(s + "\n");
    }

    public void repaintLabelLater(JLabel label) {
        SwingUtilities.invokeLater(new PaintLater(label));
    }

    public void updateEverything() {
        updateSequencerId();
        updateDataValue();
        updateNumberOfMembers();
    }

    private class PaintLater implements Runnable {
        private JLabel label;

        PaintLater(JLabel label) {
            this.label = label;
        }

        public void run() {
            label.invalidate();
            label.repaint();
        }
    }
}
Adn the code for the Data Object.
import java.util.*;

public class Data extends Thread {

    private long value = 0;
    private int curSeqNum = -1;
    private Vector<Operation> q;

    public static Data instance = null;
    
    private GUI gui = null;

    public Data() {
    	q = new Vector<Operation>();
    	gui = GUI.getInstance();
    	gui.setData(this);
    	gui.addToLog("Data has registered.");
    	this.start();
    }
    
    public Vector<Operation> getQ(){
        return q;
    }
    
    public int getCurSeqNum() {
    	return curSeqNum;
    }
    
    public long getCurDataValue(){
        return value;
    }
    
    public synchronized void addOperation(Operation o) {
        q.add(o);
        gui.addToLog("Added Operation to Q: " + o);
        gui.updateQ();
    }

    public void run() {
        boolean initted = false;
        Operation o;
        while (!initted) {
            delay();
            for (int i = 0; i < q.size(); i++) {
                synchronized (q) {
                    if (q.elementAt(i).getOperation() == Operation.operation.START) {
                        o = q.remove(i);
                        curSeqNum = o.getSeqNum();
                        initted = true;
                        break;
                    }
                }
            }
        }

        while (true) {
            // check Q for operation
            for (int i = 0; i < q.size(); i++) {
                synchronized (q) {
                    if (q.elementAt(i).getSeqNum() == curSeqNum + 1) {
                        o = q.elementAt(i);
                        q.remove(i);
                        // perform operation
                        if (o.getOperation() == Operation.operation.ADD)
                            add(o.getValue(), o);
                        else if (o.getOperation() == Operation.operation.SUB)
                            sub(o.getValue(), o);
                        else if (o.getOperation() == Operation.operation.MUL)
                            mul(o.getValue(), o);
                        curSeqNum++;
                        break;
                    }
                }
            }
            delay();
        }
    }

    private synchronized void add(int x, Operation o) {
        gui.addToLog("Added: " + x + " To " + value + " Seq:" + curSeqNum);
        value += x;
        gui.addToLog("Value Now: " + value);
        gui.appendPerformedOperation(o);
        gui.updateDataValue();
    }

    private synchronized void mul(int x, Operation o) {
        gui.addToLog("Multed: " + x + " To " + value + " Seq:" + curSeqNum);
        value *= x;
        gui.addToLog("Value Now: " + value);
        gui.appendPerformedOperation(o);
        gui.updateDataValue();
    }

    private synchronized void sub(int x, Operation o) {
        gui.addToLog("Subbed: " + x + " To " + value + " Seq:" + curSeqNum);
        value -= x;
        gui.addToLog("Value Now: " + value);
        gui.appendPerformedOperation(o);
        gui.updateDataValue();
    }

    private void delay() {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
        }
    }

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 2 2007
Added on Nov 4 2007
4 comments
534 views