0

I'm currently working on a Java program and having an issue with the program not displaying anything.

Within the main method is the following code:

WindowClient client = new WindowClient();
    client.pack();
    client.setVisible(true);

And WindowClient:

import javax.swing.JFrame;
import java.awt.GridBagLayout;
import javax.swing.SpringLayout;

import org.datacontract.schemas._2004._07.NaturalDisasterService.NaturalDisaster;
import org.tempuri.INaturalDisasterServiceProxy;

import java.awt.BorderLayout;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Font;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.awt.event.ActionEvent;

public class WindowClient extends JFrame {
    private INaturalDisasterServiceProxy ndsp;
    private JTextField textDisasterName; // text box on insertPanel for disaster names
    private JTextField textDisasterDescription; // text box on insertPanel for disaster descriptions
    public JPanel insertPanel, detailPanel; 
    public JList list; // the list
    public JLabel lblDisasterNameStaticInsert, //displays text "Disaster Name:" on insertPanel
        lblDisasterDescStaticInsert, // displays text "Disaster Description:" on insertPanel
        lblDisasterDescDataDetail, //  displays disaster description from database on detailPanel
        lblDisasterDescStaticDetail;  // label displays text "Disaster Desc:" on detailPanel
    public WindowClient() {
    buildWindow();
    ndsp = new INaturalDisasterServiceProxy();
    ndsp.setEndpoint("url redacted");
    updateList();

    }

    /**
     * @param args
     */

    public void getSelection() {
    showDetailPanel();
    }

    public void insertLoad() {
    if (detailPanel.isVisible()) {
        hideDetailPanel();
    }
    showInsertPanel();
    }

    public void saveToDB() {
    hideInsertPanel();

    }

    public void updateList() {

     NaturalDisaster nds[];
    try {
        nds = ndsp.getData();

        DefaultListModel model = new DefaultListModel();
        for(NaturalDisaster disaster : nds){
        model.addElement(disaster.getDisasterName());
        }
        getContentPane().remove(list);
        list = new JList(model);
        list.setBounds(81, 11, 247, 132);
        getContentPane().add(list);
        getContentPane().repaint();
        getContentPane().revalidate();


    } catch (RemoteException e) {
        // TODO Auto-generated catch block
        System.out.println(e.getMessage());
        //e.printStackTrace();
    }

    }

    public void buildWindow() {
    getContentPane().setLayout(null);

    list = new JList();
    list.setBounds(81, 11, 247, 132);
    getContentPane().add(list);

    insertPanel = new JPanel();
    insertPanel.setBounds(10, 150, 414, 67);
    getContentPane().add(insertPanel);
    insertPanel.setLayout(null);

    detailPanel = new JPanel();
    detailPanel.setBounds(10, 150, 414, 67);
    detailPanel.setLayout(null);

    // Label for disaster names on insertPanel
    lblDisasterNameStaticInsert = new JLabel("Disaster Name:");
    lblDisasterNameStaticInsert.setFont(new Font("Tahoma", Font.BOLD, 11));
    lblDisasterNameStaticInsert.setBounds(10, 5, 105, 14);
    insertPanel.add(lblDisasterNameStaticInsert);

    // label for disaster descripitons on the insertPanel
    lblDisasterDescStaticInsert = new JLabel("Disaster Description:");
    lblDisasterDescStaticInsert.setFont(new Font("Tahoma", Font.BOLD, 11));
    lblDisasterDescStaticInsert.setBounds(10, 30, 120, 14);
    insertPanel.add(lblDisasterDescStaticInsert);

    // text boxes on the insertPanel
    textDisasterName = new JTextField();
    textDisasterName.setBounds(138, 2, 266, 20);
    insertPanel.add(textDisasterName);
    textDisasterName.setColumns(10);

    textDisasterDescription = new JTextField();
    textDisasterDescription.setBounds(138, 27, 266, 20);
    insertPanel.add(textDisasterDescription);
    textDisasterDescription.setColumns(10);
    getContentPane().add(detailPanel);

    // label for the disaster description from database on display panel
    lblDisasterDescDataDetail = new JLabel("New label");
    lblDisasterDescDataDetail.setBounds(10, 30, 120, 14);
    detailPanel.add(lblDisasterDescDataDetail);

    // label displays text "Disaster Desc:" on display panel
    lblDisasterDescStaticDetail = new JLabel("Disaster Desc:");
    lblDisasterDescStaticDetail.setBounds(10, 5, 105, 14);
    detailPanel.add(lblDisasterDescStaticDetail);

    JButton btnGetSelection = new JButton("Get Selection");
    btnGetSelection.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        getSelection();
        }
    });
    btnGetSelection.setBounds(10, 228, 105, 23);
    getContentPane().add(btnGetSelection);

    JButton btnInsert = new JButton("Insert");
    btnInsert.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        insertLoad();
        }
    });
    btnInsert.setBounds(125, 228, 124, 23);
    getContentPane().add(btnInsert);

    JButton btnSave = new JButton("Save Info to DB");
    btnSave.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        saveToDB();
        }
    });

    btnSave.setBounds(259, 228, 165, 23);
    getContentPane().add(btnSave);
    //getContentPane().setVisible(true);
    }

    public void hideInsertPanel() {
    getContentPane().remove(insertPanel);
    insertPanel.setVisible(false);
    }

    public void showInsertPanel() {
    getContentPane().add(insertPanel);
    insertPanel.setVisible(true);
    insertPanel.getParent().revalidate();
    }

    public void hideDetailPanel() {
    getContentPane().remove(detailPanel);
    detailPanel.setVisible(false);
    }

    public void showDetailPanel() {
    getContentPane().add(detailPanel);
    detailPanel.setVisible(true);
    detailPanel.getParent().revalidate();
    }

}

Some of the other questions on the issue of JFrame malfunction mention the need to setVisible() and pack(), but I have made both of these calls. The only result is that the program runs, and a program does open in Windows task bar, but does not hav a corresponding window (the preview displays a thin white bar that doesn't display if the program is selected as active).

While not the primary issue, this is my first work with Swing in a few years, so feel free to point out other mistakes.

dluxcru
  • 397
  • 1
  • 5
  • 16
  • 2
    When I 1) comment out the custom classes 2) fix any resulting compilation errors by commenting out the lines they occur, & 3) add the 'code in main' as a main to that class, I see: A very small GUI. When I drag the size larger, I can see the components. For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Jan 30 '17 at 06:51
  • 2
    BTW: 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). **Note from [this image](https://i.stack.imgur.com/uKThv.png) how the text area is not centered, and the text of one button is truncated? That is the sort of stuff that *layouts were designed to handle.*** – Andrew Thompson Jan 30 '17 at 06:51
  • 2
    `pack` is only going to work if your content is making use of the layout management API – MadProgrammer Jan 30 '17 at 06:54
  • 1
    You code works fine (once I remove the reference to the external libraries), my guess is `nds = ndsp.getData();` is a blocking call and is waiting for a response from whatever it's dealing with, which could be preventing the UI from been made visible or it's throwing some kind of exception which isn't been caught – MadProgrammer Jan 30 '17 at 06:56
  • 1
    I would start with [Laying Out Components Within a Container](https://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) and then have a look at [Worker Threads and SwingWorker](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html) – MadProgrammer Jan 30 '17 at 07:25

1 Answers1

1
getContentPane().setLayout(null);

This line means that you've disabled the layout manager. Since you don't use any, your containers won't resize your components automatically, and that's why you don't see anything.

If you change this line to:

getContentPane().setLayout(new FlowLayout());

And remove other calls that null layouts of your JPanel objects, you'll see your components on run.

Here's the guide that briefly explains how each one of the layout managers work.

Dth
  • 1,916
  • 3
  • 23
  • 34