whuff739
whuff739

Reputation: 147

Swing Component Difficulties

I just can't seem to get this to work at all. I'm trying to get a JTextField (input) to pass text to a JTextArea (reader) positioned above it when the enter key is pressed, but they're not even showing up in the JFrame. Any ideas how I can get this code snippit to work properly? I'm probably overlooking the obvious.

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


public class Gui extends JPanel implements ActionListener{

    public static JTextField input;
    public JTextArea reader;


    public Gui() {

        //layout to be repeatedly used
        SpringLayout s_layout = new SpringLayout();

        //masterpanel, everything's on it
        JPanel all = new JPanel();
        all.setPreferredSize(new Dimension(600,400));
        all.setLayout(s_layout);

        //left jpanel - holds reader and input areas
        JPanel mainleft = new JPanel();
        mainleft.setPreferredSize(new Dimension(500,400));
        mainleft.setLayout(s_layout);
        //input textfield
        input = new JTextField(45);
        input.addActionListener(this);
        //reader textarea
        reader = new JTextArea();
        reader.setEditable(false);
        reader.setLineWrap(true);
        reader.setWrapStyleWord(true);
        JScrollPane reader_sp = new JScrollPane(reader);
        reader_sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        reader_sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        reader_sp.setPreferredSize(new Dimension(480,350));

        //set layout constraints for reader and input
        s_layout.putConstraint(SpringLayout.SOUTH, input,
                -10,
                SpringLayout.SOUTH, mainleft);

        s_layout.putConstraint(SpringLayout.NORTH, reader_sp,
                5,
                SpringLayout.NORTH, mainleft);

        //add all of the components to left panel
        mainleft.add(reader_sp);
        mainleft.add(input);

        //add left panel to masterpanel
        all.add(mainleft);

        //add masterpanel to jframe
        add(all);

    }

    public void actionPerformed(ActionEvent VK_ENTER) {

        //get the command from input area
        String text = input.getText();
        //append input to reader plus a new line
        reader.append(text + "\n");
        //clear input area
        input.setText("");
        //reset reader's caret position for next append
        reader.setCaretPosition(reader.getDocument().getLength());


    }


    private static void createAndShowGUI() {

        JFrame f = new JFrame("why doesn't this work?");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new Gui());
        f.pack();
        f.setLocationRelativeTo(null); //centers it
        f.setResizable(true);
        f.setVisible(true);

    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

Upvotes: 1

Views: 198

Answers (2)

Mud
Mud

Reputation: 28991

You're using the same layout object for the all panel and mainLeft panel. Remove either the all.setLayout(s_layout) or the mainleft.setLayout(s_layout) line and take a look.

Upvotes: 3

Diogo
Diogo

Reputation: 548

your problem is in the LayoutManager, or on your layout constraints ... if you change s_layout to be a FlowLayout, for example, your components will show up.

Upvotes: 1

Related Questions