Jack
Jack

Reputation: 5

show graphical interface of a jpanel in a jframe

What I did is: I created a subclass of java.swing.JPanel

public class TableInterfaceBDD extends javax.swing.JPanel 

I then created all of my textfield areas, my Jtable, etc. in that class.

Then I created a subclass of java.swing.JFrame, which will be my main class

public class MainTableInterface extends javax.swing.JFrame

and I wrote this code in MainTableInterface:

public MainTableInterface() {
    initComponents();
    TableInterfaceBDD pan = new TableInterfaceBDD();
    this.setContentPane(pan);
    this.setVisible(true);
    this.validate();
}

But my graphical interface that I worked on TableInterfaceBDD doesn't show up. Can you help me with that?

Upvotes: 0

Views: 196

Answers (2)

dic19
dic19

Reputation: 17971

My very first suggestion is to start with How to Make Frames (Main Windows) tutorial.

That being said, since your TableInterfaceBDD class is a JPanel, you can add it to another components, for instance a frame's content pane:

public class MyGui() {

    public void createAndShowGui() {

        JPanel yourCustomPanel = new TableInterfaceBDD();

        JFrame frame = new JFrame("Demo");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);        
        frame.add(yourCustomPanel );
        frame.pack(); // don't forget to pack() the frame
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

Then in a main method:

public static void main(String[] args) {
   SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new MyGui().createAndShowGui();
        }
    });
}

Upvotes: 2

cyber-monk
cyber-monk

Reputation: 5560

public class Main {

  public static void main(String[] args) {
    SwingUtilites.invokeLater(new Runnable() {
      public void run() {
        JFrame frame = new YourFrame(title, new YourPanel());
        frame.pack();
        frame.setVisible(true);
      }
    };
  }
}

public class YourFrame extends JFrame {
  public YourFrame(String title, JPanel panel) {
    super(title);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    add(panel, BorderLayout.CENTER);
  }
}

public class YourPanel extends JPanel {
  // your panel
}

Upvotes: 0

Related Questions