1

The imports are okay, I just want to print out the result in the JTextArea in the second JFrame after the search button is clicked.

When I run this program, my whole JFrame panel shows a huge full screen button named 'search'.

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

public class Main extends JFrame {

   private static final long serialVersionUID = 1L;
   private static final String DB_PATH = "C:/Users/Abed/Documents/Neo4j/Movies2.graphdb";
   public static GraphDatabaseService graphDB;

   public static void main(String[] args) throws Exception {

      showFrame();
   }

   public static void showFrame() {
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
      frame.setSize(500, 500);

      final JTextField actorInput = new JTextField(10);
      final JTextField genreInput = new JTextField(10);

      frame.getContentPane().add(actorInput);
      frame.getContentPane().add(genreInput);

      JButton submit = new JButton("Search");
      submit.setSize(5, 5);
      frame.getContentPane().add(submit);
      submit.addActionListener(new ActionListener() {

         public void actionPerformed(ActionEvent event) {

            graphDB = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH);
            Transaction tx = graphDB.beginTx();

            String actor = actorInput.getText();
            String genre = genreInput.getText();

            final String query;

            query = "MATCH (Movie {genre:'"
                  + genre
                  + "'})<-[:ACTS_IN]-(Person {name:'"
                  + actor
                  + "'}) "
                  + "RETURN Person.name, Movie.genre, COUNT(Movie.title) AS cnt "
                  + "ORDER BY cnt DESC " + "LIMIT 100 ";

            try {
               JFrame frame2 = new JFrame();
               frame2.setVisible(true);
               JTextArea ta = new JTextArea();
               ta.setLineWrap(true);
               frame2.add(ta);
               ExecutionEngine engine = new ExecutionEngine(graphDB,
                     StringLogger.SYSTEM);
               ExecutionResult result = engine.execute(query);
               System.out.println(result);
               String resultArea = result.dumpToString();
               ta.setText(resultArea);
               tx.success();
            } finally {
               tx.close();
               graphDB.shutdown();
            }
         }
      });
   }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373

1 Answers1

4

Issues and suggestions:

  1. JFrame contentPanes use BorderLayout by default.
  2. If you add anything to a BorderLayout-using container without specifying where to place it, it will go by default to the BorderLayout.CENTER position.
  3. Something in the BorderLayout.CENTER position will cover up anything previously added there.
  4. You really don't want your GUI to have more than one JFrame. A separate dialog window perhaps, or a swapping of views via CardLayout perhaps, but not more than one main program window. Please check out The Use of Multiple JFrames, Good/Bad Practice?.
  5. You're calling setVisible(true) on your JFrame before adding all components to it, and this can lead to non-visualization or inaccurate visualization of your GUI. Call setVisible(true) after adding all components that you're adding initially.
  6. Why does your class extend JFrame when it's never used as a JFrame?
  7. Your critical code is contained all within a static method losing all the advantages of Java's use of the object-oriented programming paradigm. Myself, when I create a new project, I concentrate on creating classes first, and then GUI's second.

Solution for the button covering up your GUI -- read up on and use layout managers in a smart and pleasing way to create smart and pleasing GUI's. The tutorials can be found here:

For example:

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dialog.ModalityType;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.*;

public class MyMain extends JPanel {
   private JTextField actorInput = new JTextField(10);
   private JTextField genreInput = new JTextField(10);

   public MyMain() {
      // JPanels use FlowLayout by default

      add(actorInput);
      add(genreInput);
      add(new JButton(new SubmitAction("Submit")));
      add(new JButton(new ExitAction("Exit", KeyEvent.VK_X)));
   }

   private class SubmitAction extends AbstractAction {
      public SubmitAction(String name) {
         super(name);
      }

      @Override
      public void actionPerformed(ActionEvent evt) {
         String actor = actorInput.getText();
         String genre = genreInput.getText();

         // call database code here in a background thread
         // show a dialog with information

         String textToDisplay = "Text to display\n";
         textToDisplay += "Actor: " + actor + "\n";
         textToDisplay += "Genre: " + genre + "\n";

         InfoDialogPanel dlgPanel = new InfoDialogPanel();
         dlgPanel.textAreaSetText(textToDisplay);

         Window window = SwingUtilities.getWindowAncestor(MyMain.this);
         JDialog modalDialog = new JDialog(window, "Dialog", ModalityType.APPLICATION_MODAL);
         modalDialog.getContentPane().add(dlgPanel);
         modalDialog.pack();
         modalDialog.setLocationByPlatform(true);
         modalDialog.setVisible(true);
      }
   }

   private static void createAndShowGui() {
      MyMain mainPanel = new MyMain();

      JFrame frame = new JFrame("MyMain");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

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

class InfoDialogPanel extends JPanel {
   private JTextArea textArea = new JTextArea(10, 20);
   private JScrollPane scrollPane = new JScrollPane(textArea);

   public InfoDialogPanel() {
      JPanel btnPanel = new JPanel();
      btnPanel.add(new JButton(new ExitAction("Exit", KeyEvent.VK_X)));      

      setLayout(new BorderLayout());
      add(scrollPane, BorderLayout.CENTER);
      add(btnPanel, BorderLayout.PAGE_END);
   }

   public void textAreaSetText(String text) {
      textArea.setText(text);
   }

   public void textAreaAppend(String text) {
      textArea.append(text);
   }
}

class ExitAction extends AbstractAction {

   public ExitAction(String name, int mnemonic) {
      super(name);
      putValue(MNEMONIC_KEY, mnemonic);
   }

   @Override
   public void actionPerformed(ActionEvent evt) {
      Component component = (Component) evt.getSource();
      Window win = SwingUtilities.getWindowAncestor(component);
      win.dispose();
   }
}
Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Thank you for your kind reply/comment. This was very helpful, I already found a way to make it work, but yours is definitely a good one as well! – user3634133 Oct 30 '14 at 22:13