Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3601222
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:35:40+00:00 2026-05-18T20:35:40+00:00

I just can’t seem to get this to work at all. I’m trying to

  • 0

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();
            }
        });
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:35:40+00:00Added an answer on May 18, 2026 at 8:35 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just can't seem to get this working its been weeks I've been trying.
I just can't seem to figure this out - I'm trying to get the
I just can't seem to get this API working. My eyes are missing the
Just can't get this to work. I want to check if this user bought
I just can't seem to get localization to work. I have a class library.
I just can't get Multi Value settings to work in my Ipad app. This
I just can't get this query to work when updating records in a mySQL
I just can't seem to get this query figured out. I need to combine
Just can't seem to get this happening ! $(.fancy).click(function(event) { event.preventDefault(); var pic =
I just can't seem to figure this out. I've tried text-align: center; , display:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.