Search code examples
javaswingjtextarea

How to start something on a new line in Java SWING?


I am trying to set my JTextArea to take up the max horz length of the screen, so that the next thing, in this case a button, will start on a new line, but I have no clue how to do it. I have messed around by setting the size of the JTextArea to change from, say, 20 to 1000 but that does not do anything.

How can I get my textarea to take up the entire first row and then have the next item that I add to begin on the following row? Here is what I have so far...

MyFrame(){//constructor

        super("Simple Calculator");
        p = new JPanel();
        grid = new GridLayout(4, 4, 3, 3);
        p.setLayout(grid);
        setSize(400, 500);
        setResizable(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setUpTextScreen();
        //create buttons
        for(int i = 0; i < buttonValues.length; i++){
            p.add(new JButton(buttonValues[i]));
        }
        add(p);
        setVisible(true);
    }

    private void setUpTextScreen() {

        textOnScreen = new JTextArea(7, 1000);
        textOnScreen.setText("0");//default
        textOnScreen.setEditable(false);
        p.add(textOnScreen);
    }

Solution

  • How can I get my textarea to take up the entire first row and then have the next item that I add to begin on the following row?

    Break your layout up into logical pieces. Start with your main panel using a BorderLayout.

    1. First I would use a JTextField for the calculator display, not a JTextArea. Then you can add the text field using: mainPanel.add(textField, BorderLayout.PAGE_START);

    2. Then you create a JPanel using a GridLayout for the buttons. Then you add the buttons to the button panel and use: maonPanel.add(buttonPanel, BorderLayout.CENTER);

    For example:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    
    public class CalculatorPanel extends JPanel
    {
        private JTextField display;
    
        public CalculatorPanel()
        {
            Action numberAction = new AbstractAction()
            {
                @Override
                public void actionPerformed(ActionEvent e)
                {
    //              display.setCaretPosition( display.getDocument().getLength() );
                    display.replaceSelection(e.getActionCommand());
                }
            };
    
            setLayout( new BorderLayout() );
    
            display = new JTextField();
            display.setEditable( false );
            display.setHorizontalAlignment(JTextField.RIGHT);
            add(display, BorderLayout.NORTH);
    
            JPanel buttonPanel = new JPanel();
            buttonPanel.setLayout( new GridLayout(0, 5) );
            add(buttonPanel, BorderLayout.CENTER);
    
            for (int i = 0; i < 10; i++)
            {
                String text = String.valueOf(i);
                JButton button = new JButton( text );
                button.addActionListener( numberAction );
                button.setBorder( new LineBorder(Color.BLACK) );
                button.setPreferredSize( new Dimension(30, 30) );
                buttonPanel.add( button );
    
                InputMap inputMap = button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
                inputMap.put(KeyStroke.getKeyStroke(text), text);
                inputMap.put(KeyStroke.getKeyStroke("NUMPAD" + text), text);
                button.getActionMap().put(text, numberAction);
            }
        }
    
        private static void createAndShowUI()
        {
            JFrame frame = new JFrame("Calculator Panel");
            frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            frame.add( new CalculatorPanel() );
            frame.pack();
            frame.setLocationRelativeTo( null );
            frame.setVisible(true);
        }
    
        public static void main(String[] args)
        {
            EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    createAndShowUI();
                }
            });
        }
    }