Fairly simple Java GUI question

Associate
Joined
26 Oct 2003
Posts
1,107
Hey,

I've got a morsecode project, but I'm having some problems putting together the GUI for it, specificly making the buttons work and inserting text into the text boxes. (The project is marked solely on the dictionary morsecode class, rather than the GUI so this isnt cheating, I just need a little bit of advice!)

I have 3 tabbed panes, 'Convert to morse', 'Convert from morse' and 'About'. I want the input in the textbox 'field' to go through my dictionary class (not shown here), then the output returned into the textarea 'fieldBig'. However when I do this it says the variable cannot be found. I'm just completely confused about the whole thing, any help or suggestions would be greatly appreciated :)

Alex

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

class MorseGUI
        extends     JFrame
        
        {
    private     JTabbedPane tabbedPane;
    private     JPanel      panel1;
    private     JPanel      panel2;
    private     JPanel      panel3;
    public      JTextArea   field;

    public MorseGUI()
    {

        setTitle( "Morse Code Converter" );
        setSize( 400, 320 );
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBackground( Color.gray );

        JPanel topPanel = new JPanel();
        topPanel.setLayout( new BorderLayout() );
        getContentPane().add( topPanel );

        // Create the tab pages
        createPage1();
        createPage2();
        createPage3();
        
        // Create a tabbed pane
        tabbedPane = new JTabbedPane();
        tabbedPane.addTab( "Convert to Morse", panel1 );
        tabbedPane.addTab( "Convert from Morse", panel2 );
        tabbedPane.addTab( "About", panel3 );
        topPanel.add( tabbedPane, BorderLayout.CENTER );
        
        dictionary test = new dictionary();
        System.out.println(test.inputString("hello"));
        }
        
     public void createPage1()
    {
        panel1 = new JPanel();
        panel1.setLayout( null );

        JLabel label1 = new JLabel( "Text to convert:" );
        label1.setBounds( 10, 15, 150, 20 );
        panel1.add( label1 );
        
        JTextField field = new JTextField();
        field.setBounds( 10, 35, 150, 20 );
        panel1.add( field );

        JLabel label2 = new JLabel( "Output:" );
        label2.setBounds( 10, 60, 150, 20 );
        panel1.add( label2 );

        JTextField fieldOutput = new JTextField();
        fieldOutput.setBounds( 10, 80, 150, 20 );
        panel1.add( fieldOutput );
        
        JButton buttonOne = new JButton("Convert to Morse");
        buttonOne.setBounds( 190, 35, 150, 20 );
        buttonOne.setToolTipText("Click this button to convert the text entered in the textbox into Morse Code.");
        buttonOne.addActionListener(new ButtonListener());
               
        panel1.add( buttonOne );
        
        JButton buttonTwo = new JButton("Play Morse");
        buttonTwo.setBounds( 190, 80, 150, 20 );
        panel1.add( buttonTwo );
                
        JTextArea fieldBig = new JTextArea();
        fieldBig.setBounds( 10, 120, 300, 80 );
        panel1.add( fieldBig );
    
    }
    
    public void createPage2()
    {
        panel2 = new JPanel();
        panel2.setLayout( null );

        JLabel label1a = new JLabel( "Morse to convert:" );
        label1a.setBounds( 10, 15, 150, 20 );
        panel2.add( label1a );

        JTextField field2 = new JTextField();
        field2.setBounds( 10, 35, 150, 20 );
        panel2.add( field2 );

        JLabel label2a = new JLabel( "Output:" );
        label2a.setBounds( 10, 60, 150, 20 );
        panel2.add( label2a );

        JTextField fieldOutput2 = new JTextField();
        fieldOutput2.setBounds( 10, 80, 150, 20 );
        panel2.add( fieldOutput2 );
        
        JButton buttonOne2 = new JButton("Convert to Text");
        buttonOne2.setBounds( 190, 35, 150, 20 );
        panel2.add( buttonOne2 );
                 
        JButton buttonTwoa = new JButton("Play Morse");
        buttonTwoa.setBounds( 190, 80, 150, 20 );
        panel2.add( buttonTwoa );
                
        JTextArea fieldBig2 = new JTextArea();
        fieldBig2.setBounds( 10, 120, 300, 80 );
        panel2.add( fieldBig2 );
    }    
        
    public void createPage3()  {
 
        panel3 = new JPanel();
        panel3.setLayout( null );

        JLabel aboutText = new JLabel( "About text goes here" );
        aboutText.setBounds( 10, 15, 150, 20 );
        panel3.add( aboutText );
    }

    // Main method to get things started
    public static void main( String args[] )
    {
        // Create an instance of the test application
        MorseGUI mainFrame  = new MorseGUI();
        mainFrame.setVisible( true );
    }
    
        public class ButtonListener implements ActionListener {
        public ButtonListener()
        {
        }

        public void actionPerformed(ActionEvent e)
        {
        field.setText("test");
        }
    }
}
 
at a guess I'd say that you're bigtextarea is being created as a member variable of the class MorseGUI and you're dictioanry class is then trying to set it to a value. The dictionary class won't have access to a variable with the default access rights either mark it as a public variable, use an event to pass the return value back or call a method in the dictionary class that returnss a string. Please note I haven't read ALL of your code

HT
 
Back
Top Bottom