user3075454
user3075454

Reputation: 49

JtextField display not functioning

Evening everyone,

So Im trying to create a simple topic board using a Javaspace and I appear to have hit a snag with my JTextFields, originally the AddTopic and AddMessage buttons sent the information to the appropriate JTextField (though only once they need to update constantly), now they just crash the java application and freeze it, if anyone could look at the "addMessage, AddTopic, Processtopic and ProcessMessage functions that would be excellent.

Thanks.

Mr Smith

import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JButton;

import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JTextArea;

import net.jini.core.lease.Lease;
import net.jini.space.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class MainInterface extends  javax.swing.JFrame{
//Variable names
    private JavaSpace space;
    private JPanel jPanel1;
private JTextArea MessageList1;
    private JTextField MessageSenderField;
private JScrollBar TopicScrollBar, MessageScrollBar;
private JTextField topicauthorfield;
private JButton RefreshMessages1;
private JButton Refreshtopic1;
private JTextField MessageAdderField1;
    private JTextField TopicAdderField;
private JButton AddMessage1;
    private JButton AddTopic1;
private JTextArea TopicList1;

public MainInterface() {
    space = SpaceUtils.getSpace();
    if (space == null){
        System.err.println("Failed to find the javaspace");
        System.exit(1);}
    {
//The Names of methods to be found further in the program.
        MainComponents ();
        pack ();
        setVisible(true);
        processtopics();
        processmessages();

    }}


private void MainComponents () {
    setTitle ("Topic Board");
    addWindowListener (new java.awt.event.WindowAdapter () {
        public void windowClosing (java.awt.event.WindowEvent evt) {
            System.exit(0);
        }
    }   );

    Container cp = getContentPane();
    cp.setLayout (new BorderLayout ());

    jPanel1 = new JPanel();
    jPanel1.setLayout(null);
    jPanel1.setPreferredSize(new java.awt.Dimension(997, 527));

    TopicList1 = new JTextArea();
    jPanel1.add(TopicList1);
    TopicList1.setBounds(39, 12, 400, 400);

    TopicScrollBar = new JScrollBar();
    TopicList1.add(TopicScrollBar);

    MessageList1 = new JTextArea();
    jPanel1.add(MessageList1);
    MessageList1.setBounds(564, 12, 400, 400);

    MessageScrollBar = new JScrollBar();
    MessageList1.add(MessageScrollBar);

    AddTopic1 = new JButton();
    jPanel1.add(AddTopic1);
    AddTopic1.setText("Add Topic");
    AddTopic1.setBounds(144, 489, 120, 22);
    AddTopic1.addActionListener (new java.awt.event.ActionListener () {
        public void actionPerformed (java.awt.event.ActionEvent evt) {
    addTopic (evt);            }
    }  );


    AddMessage1 = new JButton();
    jPanel1.add(AddMessage1);
    AddMessage1.setText("Add Message");
    AddMessage1.setBounds(709, 489, 140, 22);       
    AddMessage1.addActionListener (new java.awt.event.ActionListener () {
        public void actionPerformed (java.awt.event.ActionEvent etc) {
    addMessage (etc);            }
    }  );



    TopicAdderField = new JTextField();
    jPanel1.add(TopicAdderField);
    TopicAdderField.setText("Please Enter Topic Title");
    TopicAdderField.setBounds(39, 461, 400, 22);


    MessageAdderField1 = new JTextField();
    jPanel1.add(MessageAdderField1);
    MessageAdderField1.setText("Please Enter your Message");
    MessageAdderField1.setBounds(564, 455, 400, 22);

    Refreshtopic1 = new JButton();
    jPanel1.add(Refreshtopic1);
    Refreshtopic1.setText("Refresh Topics");
    Refreshtopic1.setBounds(306, 489, 106, 22);


    RefreshMessages1 = new JButton();
    jPanel1.add(RefreshMessages1);
    RefreshMessages1.setText("Refresh Message");
    RefreshMessages1.setBounds(564, 489, 122, 22);

    topicauthorfield = new JTextField();
    jPanel1.add(topicauthorfield);
    topicauthorfield.setText("Enter Author name");
    topicauthorfield.setBounds(39, 433, 400, 22);

    MessageSenderField = new JTextField();
    jPanel1.add(MessageSenderField);
    MessageSenderField.setText("Senders Name");
    MessageSenderField.setBounds(564, 427, 400, 22);

    cp.add(jPanel1,"Center");
    pack();}

public void addMessage(java.awt.event.ActionEvent etc){
    try {
        MessageQueueStatus qsTemplate = new MessageQueueStatus();
        MessageQueueStatus qStatus = (MessageQueueStatus)space.take(qsTemplate,null,Long.MAX_VALUE);


        String messagename = MessageAdderField1.getText();
        String messagesender = MessageSenderField.getText();
        JHB3MessageItem newMessage = new JHB3MessageItem(messagename, messagesender);
        space.write( newMessage, null, Lease.FOREVER);

        qStatus.addMessage();
        space.write( qStatus, null, Lease.FOREVER);
    }  catch ( Exception e) {
        e.printStackTrace();
    }
    }

public void addTopic(java.awt.event.ActionEvent evt){
    try {
        JHB3QueueStatus qsTemplate = new JHB3QueueStatus();
        JHB3QueueStatus qStatus = (JHB3QueueStatus)space.take(qsTemplate,null,Long.MAX_VALUE);


        String topicname = TopicAdderField.getText();
        String topicauthor= topicauthorfield.getText();
        JHB3TopicItem newTopic = new JHB3TopicItem(topicname, topicauthor);
        space.write( newTopic, null, Lease.FOREVER);

        qStatus.addTopic();
        space.write( qStatus, null, Lease.FOREVER);
    }  catch ( Exception e) {
        e.printStackTrace();}
    }

public void processtopics(){
        try {
            JHB3TopicItem qiTemplate = new JHB3TopicItem();
            JHB3TopicItem nextTopic = (JHB3TopicItem)space.readIfExists(qiTemplate,null,Long.MAX_VALUE);
            String nextTopicName = nextTopic.topicname;
            String nextTopicAuthor = nextTopic.topicauthor;
            TopicList1.append(" Author: " + nextTopicAuthor + " Topic Name: " + nextTopicName + "\n" );
        }  catch ( Exception e) {
            e.printStackTrace();}
        }

public void processmessages(){
        try {
            JHB3MessageItem qiTemplate = new JHB3MessageItem();
            JHB3MessageItem newMessage = (JHB3MessageItem)space.readIfExists(qiTemplate,null,Long.MAX_VALUE);
            String nextMessage = newMessage.messagename;
            String nextSender = newMessage.messagesender;
            MessageList1.append(" Sender: " + nextSender + " Message: " + nextMessage + "\n" );
        }  catch ( Exception f) {
            f.printStackTrace();}
        }

    public static void main(java.lang.String[] args) {
        new MainInterface();

}}

Upvotes: 1

Views: 73

Answers (2)

user3075454
user3075454

Reputation: 49

So it appeared to be an error caused by the names associated with my classes and the Java Space I am currently sharing with some other programmers, several of us ended up using the same name to define the object templates for our classes causing it to freeze up during use.

Renaming the classes resolved the issue.

Thanks

Mr Smith

Upvotes: 0

camickr
camickr

Reputation: 324078

now they just crash the java application and freeze it, if anyone could look at the "addMessage, AddTopic, Processtopic and ProcessMessage functions that would be excellent.

We don't have access to your 3rd party API's so we can't really help you.

Whenever I see the word "freeze" I assume you are blocking the Event Dispatch Thread. Read the section from the Swing tutorial on Concurrency in Swing, for more information and a way to solve the problem by using Threads or a SwingWorker.

Also:

  1. Use proper Java variable names if you want people to take the time to read your code. Variable names should NOT start with an upper case character.

  2. Use Swing the way way it was designed to be used. That is use layout managers. You should NOT be using setBounds(). I gave you a link to the tutorial. There is also a section on using layout managers you should read.

Upvotes: 2

Related Questions