Domanda

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);
}
È stato utile?

Soluzione

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()

Altri suggerimenti

If you want controls at the top of your window, and the table filling the majority of the window, then I'd suggest you try using BorderLayout instead of FlowLayout. Create it like this...

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

And add the components by specifying the location in the BorderLayout. In this case, the controls should be added to the top in their minimal size...

this.getContentPane().add(controlPanel,BorderLayout.NORTH);

And the table should be in the center, filling the remaining window space...

this.getContentPane().add(tablePanel,BorderLayout.CENTER);

In your case, you also have a button at the bottom...

getContentPane().add(btnMulticastList,BorderLayout.SOUTH);

For the layout you're after, BorderLayout is much more appropriate. The benefit of using BorderLayout here is that the components should be automatically resized to the size of the window, and you're explicitly stating where each component resides, so panels shouldn't not appear.

It would also be my recommendation that you find an alternative to calling getContentPane() in all your methods. Maybe consider keeping a global variable for the main panel, like this...

private mainPanel;

private void addContentPanel()
{
    mainPanel = new JPanel(new BorderLayout());
    mainPanel.setSize(new Dimension(800, 600));
    this.setContentPane(mainPanel);
}

Then you can reference the panel directly when you want to add() components to it.

Finally, I'd also suggest using GridLayout for your controls, as it will allow you to place all your buttons in it, and they'll be the same size for consistency. Define it like this to allow 5 buttons in a horizontal alignment...

JPanel controlPanel = new JPanel(new GridLayout(5,1));

then you just add the buttons normally using controlPanel.add(button) and they'll be added to the next slot in the grid.

For more information, read about GridLayout or BorderLayout, or just see the Java Tutorial for a Visual Guide to Layout Managers to see what alternatives you have and the best one for your situation. In general, I try to avoid FlowLayout, as I find that there are other LayoutManagers that are more suitable in the majority of instances.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top