Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

dialog empty with JScrollPane und JPanel

i am trying to add the content to the mainPanel and mainpanel to the mainScrollPane. However, an empty dialog is displayed.

IMPORTANT: it must be implemented with JPanel and JScrollPane…

   `setModal(true);
    setTitle("Edit item");
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);

    JPanel mainPanel = new JPanel();
    mainPanel.setPreferredSize(new Dimension(400, 700));

    JScrollPane mainScrollPane = new JScrollPane();
    mainScrollPane.add(mainPanel);
    setLayout(new BorderLayout());
    add(mainScrollPane);

    mainPanel.add(new JLabel("ID: "));
    mainPanel.add(txtID = new JTextField(item.getID()));
    mainPanel.add(new JLabel("Description: "));
    mainPanel.add(txtDescription = new JTextField(item.getDescription()));


    pack();
    setVisible(true);`

thank you, guys

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Please provide a minimal reproducible example. It could look like what follows:

MyFrame.java

import javax.swing.*;
import java.awt.*;

class Item { 
    String id; 
    String description; 
    Item(String id, String description) {this.id = id;this.description = description;}
    String getId() {return id;}
    String getDescription() {return description;}
}

public class MyFrame extends JFrame {
    Item item = new Item("007", "Special watch");
    public MyFrame() {
        // setModal(true);
        setTitle("Edit item");
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        JPanel mainPanel = new JPanel();
        mainPanel.setPreferredSize(new Dimension(400, 700));

        JScrollPane mainScrollPane = new JScrollPane();
        mainScrollPane.add(mainPanel);
        setLayout(new BorderLayout());
        add(mainScrollPane);

        mainPanel.add(new JLabel("ID: "));
        mainPanel.add(new JTextField(item.getId()));
        mainPanel.add(new JLabel("Description: "));
        mainPanel.add(new JTextField(item.getDescription()));

        getContentPane().add(mainPanel); // <- This makes it shine!

        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        new MyFrame();
    }
}
$ javac MyFrame.java
$ java MyFrame   

Et voilà:

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading