3 Replies - 220 Views - Last Post: 10 November 2019 - 08:45 AM Rate Topic: -----

#1 babzbkt   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 29-August 18

GridLayout Help

Posted 08 November 2019 - 04:08 AM

Hi, i have a program here that i have made (and obviously im new to this GUI stuff), i'm trying to add a few buttons but in the specified layout:
right now my program looks something like this:

https://ibb.co/VpZphM0

but i want it to look something more like this:

https://ibb.co/6J1MjFL

i know its not that hard and it requires inserting a grid layout inside my grid layout but how do i go about doing that?

can someone please help me thanks. heres my code:

package com.company.Lab3;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

public class CE203 extends JFrame {
    List<String> words = new ArrayList< >();

    JPanel panel1 = new JPanel();
    JTextField textField1 = new JTextField("Enter Text Here",30);
    JLabel label = new JLabel();
    JTextArea textArea =new JTextArea();
    JButton jb = new JButton("Enter");
    JScrollPane jsp = new JScrollPane();

    public CE203(){
    setTitle("CE203");
    setVisible(true);
    setSize(400,200);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    panel1.add(jb);
    panel1.add(new JScrollPane(textArea));
    panel1.add(textField1);
    panel1.setLayout(new GridLayout(3,1));


    textField1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
String input = textField1.getText();
//textArea.setText(input);

words.add(input);

                textArea.append("\n"+input + "\n");

        }
                                  }
    );


    jb.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            String input = textField1.getText();
        //    textArea.setText(input);

            words.add(input);
                textArea.append("\n"+input + "\n");


        }
    });
    add(panel1);
}}



Is This A Good Question/Topic? 0
  • +

Replies To: GridLayout Help

#2 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 6,062
  • Joined: 25-December 13

Re: GridLayout Help

Posted 08 November 2019 - 01:42 PM

Where is the code for building the desired top panel?
Was This Post Helpful? 0
  • +
  • -

#3 babzbkt   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 29-August 18

Re: GridLayout Help

Posted 10 November 2019 - 08:33 AM

View PostNormR, on 08 November 2019 - 01:42 PM, said:

Where is the code for building the desired top panel?



i used
    panel1.setLayout(new GridLayout(3,1));



to put the frame into a grid layout, then added the buttons etc. one by one
Was This Post Helpful? 0
  • +
  • -

#4 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 6,062
  • Joined: 25-December 13

Re: GridLayout Help

Posted 10 November 2019 - 08:45 AM

Have you solved it now? What does the new code show in the GUI?

The images you posted showed the current layout to be 3 vertical panels:
top panel - Enter button
center panel - text field
bottom panel - text entry field

The desired layout:
top panel - 4 horizontal buttons
center panel - text field
bottom panel - text entry field

The changes you needed to make were to change the top panel to hold 4 buttons.

The question I asked in my last port was about
Where is the code that does that?

The code in post#3 looks like it is for the three vertical panels, not for the 4 buttons in the top panel.

This post has been edited by NormR: 10 November 2019 - 08:57 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1