-2

I was wondering if is possible to use JPanels without JFrames because I am making a program that is asking to write a failure report.

I am using JOptionpane for new window but every time I try to add a scroll bar to the textArea, it just disappears from the screen but if I do that in JFrames it doesn't. I need to use JPanels because I have 2 Jpanel in one frame and everyone of them has to have a scroll bar separately.

public class aa {
    private static JScrollPane pane;

    public static void main(String[] arg){
        JPanel panel = new JPanel();
        panel.setPreferredSize(new Dimension(425, 400));
        panel.setLayout(null);
        JLabel labelName= new JLabel("Name and Surname");
        JTextField textFieldName = new JTextField();
        JLabel labelFailSubject= new JLabel("Fail Subject");
        JTextField textFieldFailSubject = new JTextField(15);
        JLabel labelTime= new JLabel("Fail Time");
        JTextField textFieldFailTime = new JTextField(8);
        JLabel labelFailDate = new JLabel("Fail Date");
        JTextField textFieldFailDate= new JTextField(8);
        JLabel labelFailReport= new JLabel("Fail Report");
        JTextArea textBoxFailReport = new JTextArea();
        pane = new JScrollPane(textBoxFailReport);

        panel.add(labelName);
        panel.add(textFieldName);   
        panel.add(labelFailDate);
        panel.add(textFieldFailDate);
        panel.add(labelTime);
        panel.add(textFieldFailTime);
        panel.add(labelFailSubject);
        panel.add(textFieldFailSubject);
        panel.add(labelFailReport);
        panel.add(textBoxFailReport);
        textBoxFailReport.setLineWrap(true);
        
        JOptionPane.showMessageDialog(null, panel, "Fail Report", JOptionPane.PLAIN_MESSAGE);
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Monveu
  • 1
  • 4
  • 1
    *"I am using null as lay out because i used setBounds for the all items location"* Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Sep 12 '15 at 12:24
  • *"every time i try to add a scroll bar to the textArea , it just disappears from the screen"* I'm not surprised, because of this.. `JTextArea textBoxFailReport = new JTextArea();`. Suggest a size in columns and rows (then put it in a layout that will respect that size). Something like this.. `JTextArea textBoxFailReport = new JTextArea(40,10);` – Andrew Thompson Sep 12 '15 at 12:27

2 Answers2

1

You create a JScrollPane containing the text area, but instead of adding this scrollPane to your main panel, you're adding the textarea directly. So that removes the textarea from its scrollpane parent and adds it to the main panel. And the scrollpane is thus not used at all.

Using a null layout is also not something you should do.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • if i write like, panel.add(pane); it disappears from the main panel. I am using null as lay out because i used setBounds for the all items location – Monveu Sep 12 '15 at 12:12
0

Wrap the JPanel in a JScrollPane and then add it to the JOptionPane. This should solve the problem.

Update:

Here is a sample code. I have used GridLayout here.

private static JScrollPane pane;

public static void main(String[] arg) {

    JPanel panel = new JPanel();
    panel.setPreferredSize(new Dimension(425, 400));
    panel.setLayout(new GridLayout(5, 2));//Grid Layout with 5 rows, 2 columns used
    JLabel labelName = new JLabel("Name and Surname");
    JTextField textFieldName = new JTextField();
    JLabel labelFailSubject = new JLabel("Fail Subject");
    JTextField textFieldFailSubject = new JTextField(15);
    JLabel labelTime = new JLabel("Fail Time");
    JTextField textFieldFailTime = new JTextField(8);
    JLabel labelFailDate = new JLabel("Fail Date");
    JTextField textFieldFailDate = new JTextField(8);
    JLabel labelFailReport = new JLabel("Fail Report");
    JTextArea textBoxFailReport = new JTextArea();

    panel.add(labelName);
    panel.add(textFieldName);
    panel.add(labelFailDate);
    panel.add(textFieldFailDate);
    panel.add(labelTime);
    panel.add(textFieldFailTime);
    panel.add(labelFailSubject);
    panel.add(textFieldFailSubject);
    panel.add(labelFailReport);
    panel.add(textBoxFailReport);
    textBoxFailReport.setLineWrap(true);

    pane = new JScrollPane(panel);//JPanel is wrapped with JScrollPane

    JOptionPane.showMessageDialog(null, pane, "Fail Report", JOptionPane.PLAIN_MESSAGE);//JScrollPane is added to the JOptionPane
}
shan1024
  • 1,389
  • 7
  • 17