-4

Can someone tell me how to add 3 buttons under the txtArea in the TextLine class present in this link : https://stackoverflow.com/a/18780743/3649438

I am new to GUI programming and I have problems in adding them. I tried to add JButtons to pane1 or contentPane, but it seems that the code automatically resizes the window and it is not possible to add the buttons. I just need to add simple JButton which I will edit with my own code.

My possible solution was the one of making a button (repeating the procedure for each button) in this way:

JButton cancelBtn = new JButton("Cancel");

and then add it to the panels in this way.

cancelBtn.setVisible(true);
contentPane.add(cancelBtn);

but when I run it, it doesn't display it. I tried many other different ways with no success.

Community
  • 1
  • 1
  • 3
    Show us your best attempt. At the moment, it sounds like you're just using SO as your personal coding service. – Andrew Thompson Jun 06 '14 at 11:27
  • It's not like that. If I posted the question here it's because I've spent one whole day in trying to figure out why, when I add buttons on my panel it doesn't work. My possible solution was the one of making a button (repeating the procedure for each button) in this way : JButton cancelBtn = new JButton("Cancel"); and then add it to the panels in this way. cancelBtn.setVisible(true); contentPane.add(cancelBtn); but when I run it, it doesn't display it. I tried many other different ways with no success. I bet it's something stupid I can't figure out and that's why I asked for your help. – user3649438 Jun 06 '14 at 11:29
  • Edit your question and add these explanations and some code – Pablo Lozano Jun 06 '14 at 11:51
  • 1
    This is the second time you have posted this question. I see you deleted your other posting. There is nothing special about the code in the link you provided. All you need to do is create a JTextArea, add the text area to a JScrollPane and then add the scroll pane to the "CENTER" of the frame. Then you create a JButton and add it to the "SOUTH" of the frame. If you don't know what I mean by "CENTER" and "SOUTH", then read the Swing tutorial on [How to Use BorderLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/border.html) for more information and a working example. – camickr Jun 06 '14 at 14:17
  • Instead of being rude and reaching personal conclusions,you could simply not answer if you didn't want to.I've been polite and I was just asking for your suggestions on how to solve the issue. Just stating "go on this page (linking a tutorial)"like Arijit did,it would have been helpful.I don't understand why some people are very aggressive with newbie who do not have a high ranking/knowledge or have just started to code for the first time.I thought this was a community of programmers willing to help each other and my intention was not to ask you to do the work for me like Andrew Thompson said. – user3649438 Jun 06 '14 at 17:31
  • *"I don't understand.."* Signal to noise ratio. If it drops too far, the experts abandon the place, and your post was drifting towards noise. BTW - please don't confuse 'direct' with 'rude' or 'aggressive'. None of us have been trying to be anything but direct, and we do that in the hope that the post will improve. – Andrew Thompson Jun 07 '14 at 02:38

2 Answers2

1

Read Layout Manager from here

Run this code. It will solve your problem. But first read the basic tutorials of Swing.

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import javax.swing.JScrollPane;

public class TextLine extends JFrame {

    private JPanel contentPane;


    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TextLine frame = new TextLine();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public TextLine() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JLabel lblTextLineExample = new JLabel("Text Line Example");
        lblTextLineExample.setHorizontalAlignment(SwingConstants.CENTER);
        contentPane.add(lblTextLineExample, BorderLayout.NORTH);

        JButton btnCancel = new JButton("Cancel");
        contentPane.add(btnCancel, BorderLayout.SOUTH);

        JTextArea textArea = new JTextArea();
        JScrollPane scrollPane = new JScrollPane(textArea, 
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
                   JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        textArea.setRows(30);
        textArea.setColumns(50);
        TextLineNumber tln1 = new TextLineNumber(textArea);
        scrollPane.setRowHeaderView( tln1 );
        contentPane.add(scrollPane, BorderLayout.CENTER);
        pack();
    }

}
Arijit
  • 1,633
  • 2
  • 21
  • 35
  • Thank you so much Arijit. That solved my problem. I then added other buttons using another panel. I spent a whole day in trying to figure out how to fix my problem. I'm not a big fun of front end, in particular I've been spending so much time with swing(just started to learn) with no success. – user3649438 Jun 06 '14 at 17:27
0
public test() {
        super("Title");
        JPanel panel = new JPanel();
        add(panel);

        JButton mybutton = new JButton("TEST BUTTON");
        getContentPane().add(mybutton);
    }