Search code examples
javaswingjtablejpanelflowlayout

Java Swing - JTable not showing


I'm having some troubles with Java Swing. I'm trying to make a frame with a control panel at the top with some buttons in it. and below that i want a JTable to show

I've been trying but the table is not showing. If I remove the controlPanel at the top, it sometimes shows and sometimes not. The code that I use inside my constructor of my JTable is provided in the same application, so it's no network error

public ServerMainFrame(GuiController gc){
    this.gc = gc;
    initGUI();
}

private void initGUI() {
    System.out.println("initiating GUI");
    createFrame();
    addContentPanel();
    addControls();
    //openPopUpServerSettings();
    addSongTable();
}

private void createFrame()
{
    this.setTitle("AudioBuddy 0.1");
    this.setVisible(true);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setSize(800, 600);
    this.setResizable(false);
    this.setLocationRelativeTo(null);
}

private void addContentPanel()
{
    JPanel p = new JPanel();
    p.setLayout(new FlowLayout());
    p.setSize(new Dimension(800, 600));
    this.setContentPane(p);
}

private void addControls()
{
    JPanel controlPanel = new JPanel();
    controlPanel.setLayout(new FlowLayout());
    controlPanel.setBorder(BorderFactory.createLineBorder(Color.black));
    controlPanel.setSize(700,100);

    // Buttons
    JButton play = new JButton("Play");
    JButton pause = new JButton("Pause");
    JButton stop = new JButton ("Stop");
    JButton next = new JButton("Next");
    JButton prev = new JButton("Previous");     
    controlPanel.add(play);
    controlPanel.add(pause);
    controlPanel.add(stop);
    controlPanel.add(next);
    controlPanel.add(prev);

    // Currently playing
    JLabel playing = new JLabel("Currently playing:");
    controlPanel.add(playing);

    JLabel current = new JLabel("Johnny Cash - Mean as Hell");
    controlPanel.add(current);

    this.getContentPane().add(controlPanel);
}

private void addSongTable()
{
    JTable songTable = new JTable(Server.getSongTableModel());
    songTable.setVisible(true);
    JPanel tablePanel = new JPanel();
    tablePanel.setVisible(true);
    tablePanel.add(songTable);
    songTable.repaint();
    this.getContentPane().add(tablePanel);

    JButton btnMulticastList = new JButton("send list to clients");
    btnMulticastList.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            Server.MulticastPlaylist();
        }
    });

    getContentPane().add(btnMulticastList);
}

Solution

  • if I remove the controlPanel at the top, it sometimes shows and sometimes not.

    • everything is hidden in Server.getSongTableModel(), nobody knows without posting an SSCCE with hardcoded value returns from

    • GUI has issue with Concurency in Swing

    • XxxModel loading data continiously with building GUi, then exception caused described problems

    The code that I use inside my constructor of my JTable is provided in the same application, so it's no network error

    • no idea what you talking about

    • have to create an empty GUI, see InitialTread

    • showing GUI, then to start loading data to JTable

    • then starting Workers Thread (Backgroung Task) from SwingWorker or (descr. Network issue) better Runnable#Thread (confortable for catching an exceptions and processing separate threads)

    • output from Runnable to the Swing GUI must be wrapped into invokeLater()