Member Avatar for loserspearl

Im working on a Search index that searches through files that you manually add to the index, I'm having trouble with my GUI right now. Never used any java IDE before and its giving some unfamiliar suggestions. Netbeans wanted to make my main class abstract (but for no reason stopped) The frame open but is as small as possible and with no contents shown. What am I doing wrong?

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package project3;

/**
 *
 * @author OJOE
 */
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;

public class SearchEngine extends JFrame implements ActionListener {
    
    /**
     * @param args the command line arguments
     */
    
    
    JFrame f = new JFrame("File Search Engine");
    LayoutManager lo;
    JButton srchBtn, fileBtn, aboutBtn;
    JLabel titleLbl, srchLbl, infoLbl;
    
    public void start() {

        lo = new FlowLayout();
        f.setLayout(lo);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        titleLbl = new JLabel("File Search Engine");
        titleLbl.setFont(new Font("SansSeriff", Font.BOLD, 20));

        Font stdFont = new Font("SanSeriff", Font.PLAIN, 14);
        srchLbl = new JLabel("Search: ");
        srchLbl.setFont(stdFont);

        infoLbl = new JLabel("FileSearch v1.0");
        infoLbl.setFont(new Font("SanSeriff", Font.PLAIN, 12));

        srchBtn = new JButton("Search the criteria");
        srchBtn.setActionCommand("srchAct");
        srchBtn.setToolTipText("Search files");
        srchBtn.addActionListener(this);

        fileBtn = new JButton("Files");
        fileBtn.setActionCommand("fileAct");
        fileBtn.setToolTipText("Search files");
        fileBtn.addActionListener(this);

        aboutBtn = new JButton("About");
        aboutBtn.setActionCommand("aboutAct");
        aboutBtn.setToolTipText("Search files");
        aboutBtn.addActionListener(this);

        JTextArea srchTxt = new JTextArea(1, 40);
        srchTxt.setFont(stdFont);
        srchTxt.setEditable(true);

        String andStr = "And";
        String orStr = "Or";
        String phraseStr = "Phrase";
        JRadioButton andRB = new JRadioButton(andStr);
        JRadioButton orRB = new JRadioButton(orStr);
        JRadioButton phraseRB = new JRadioButton(phraseStr);

        ButtonGroup btnGroup = new ButtonGroup();
        btnGroup.add(andRB);
        btnGroup.add(orRB);
        btnGroup.add(phraseRB);
//these might need to be mnemonic
        andRB.addActionListener(this);
        orRB.addActionListener(this);
        phraseRB.addActionListener(this);

        JPanel title = new JPanel(new BorderLayout());
        title.setLayout(new BorderLayout());
        title.add(titleLbl, BorderLayout.CENTER);
        //was trying either way to add borderlayout panels, neither seemed to work
        JPanel top = new JPanel();
        top.setLayout(new BorderLayout());
        top.add(srchLbl, BorderLayout.WEST);
        top.add(srchTxt, BorderLayout.CENTER);
        top.add(srchBtn, BorderLayout.EAST);

        JPanel radBtn = new JPanel(new BorderLayout());
        //radBtn.setLayout(new BorderLayout());
        radBtn.add(andRB, BorderLayout.WEST);
        radBtn.add(orRB, BorderLayout.CENTER);
        radBtn.add(phraseRB, BorderLayout.EAST);

        JPanel bot = new JPanel(new BorderLayout());
        //bot.setLayout(new BorderLayout());
        bot.add(fileBtn, BorderLayout.WEST);
        bot.add(infoLbl, BorderLayout.CENTER);
        bot.add(aboutBtn, BorderLayout.EAST);
        //was trying either way to add panels to frame, neither seemed to work
        f.getContentPane().add(title);
        //f.add(title);
        f.add(radBtn);
        f.add(top);
        f.add(bot);
        
        f.setVisible(true);

    }

    public void actionPerformed(ActionEvent e) {
        //will use case for action strings
    }

    public static void main(String[] args) {
        // TODO code application logic here

       
    }
}

netbeans also wants to add @override for my actionPerformed method. This is a 3 windowed program, this main frame, a frame with adding files to the search index and an about page. Not sure why the frame is empty. I also dont think my EXITONCLOSE operation is working at all. All help is appreciated.

You need to add some code to the main() method. The java program starts the program's execution by calling that method. Your program would immediately return without doing anything.

you have not yet created an instance of your SearchEngine class. your main method is not doing anything; do this in your main method

public static void main(String[] args){
     new SearchEngine().start(); 
}
Member Avatar for loserspearl

K Thanks, I'll try that. I'm not used to swing frames, only used applets so far

Member Avatar for loserspearl

Got the main window to open and show, working on actions to open the secondary windows. When working with applets I was able to set the buttons action to a string use a method (this one named actionPerfmormed) that checks which action to handle. I know my buttons are working (because they don't bring up my JOptionpane error) Heres the code from my main and one of my secondary windows

/* GUI program that allows users to manually add files to an index
 * and search the content of them for specific words 
 * 
 */

package project3;

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

public class SearchEngine extends JFrame implements ActionListener {
    
    /**
     * @param args the command line arguments
     */
    
    //create GUI objects
    static JFrame f = new JFrame("File Search Engine");
    LayoutManager lo;
    JButton srchBtn, fileBtn, aboutBtn;
    JLabel titleLbl, srchLbl, infoLbl;
    
    public void srchGUI() {

        lo = new FlowLayout();
        f.setLayout(lo);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        titleLbl = new JLabel("File Search Engine");
        titleLbl.setFont(new Font("SansSeriff", Font.BOLD, 20));

        Font stdFont = new Font("SanSeriff", Font.PLAIN, 14);
        srchLbl = new JLabel("Search: ");
        srchLbl.setFont(stdFont);

        infoLbl = new JLabel("   FileSearch v1.0   ");
        infoLbl.setFont(new Font("SanSeriff", Font.PLAIN, 12));

        srchBtn = new JButton("Search");
        srchBtn.setActionCommand("srchAct");
        srchBtn.setToolTipText("Search files");
        srchBtn.addActionListener(this);

        fileBtn = new JButton("Files");
        fileBtn.setActionCommand("fileAct");
        fileBtn.setToolTipText("Search files");
        fileBtn.addActionListener(this);

        aboutBtn = new JButton("About");
        aboutBtn.setActionCommand("aboutAct");
        aboutBtn.setToolTipText("Search files");
        aboutBtn.addActionListener(this);

        JTextArea srchTxt = new JTextArea(1, 20);
        srchTxt.setFont(stdFont);
        srchTxt.setEditable(true);

        String andStr = "And";
        String orStr = "Or";
        String phraseStr = "Phrase";
        JRadioButton andRB = new JRadioButton(andStr);
        JRadioButton orRB = new JRadioButton(orStr);
        JRadioButton phraseRB = new JRadioButton(phraseStr);

        //grouping radio buttons
        ButtonGroup btnGroup = new ButtonGroup();
        btnGroup.add(andRB);
        btnGroup.add(orRB);
        btnGroup.add(phraseRB);

        //adding action listeners
        andRB.addActionListener(this);
        orRB.addActionListener(this);
        phraseRB.addActionListener(this);
        
        //add contents to panels
        //JPanel title = new JPanel(new BorderLayout());
        JPanel title = new  JPanel();
            title.setLayout(new BorderLayout());
            title.add(titleLbl, BorderLayout.CENTER);
            
        JPanel top = new JPanel();
            top.setLayout(new BorderLayout());
            top.add(srchLbl, BorderLayout.WEST);
            top.add(srchTxt, BorderLayout.CENTER);
            top.add(srchBtn, BorderLayout.EAST);

        JPanel radBtn = new JPanel();
            radBtn.setLayout(new BorderLayout());
            radBtn.add(andRB, BorderLayout.WEST);
            radBtn.add(orRB, BorderLayout.CENTER);
            radBtn.add(phraseRB, BorderLayout.EAST);

        JPanel bot = new JPanel();
            bot.setLayout(new BorderLayout());
            bot.add(fileBtn, BorderLayout.WEST);
            bot.add(infoLbl, BorderLayout.CENTER);
            bot.add(aboutBtn, BorderLayout.EAST);

        //add panels to frame
        f.add(title);
        f.add(top);
        f.add(radBtn);
        f.add(bot);
        
    }                        

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        new SearchEngine().srchGUI();
        f.setSize(400,200);
        f.addWindowListener( new WindowAdapter()
            {@Override
            public void windowClosing ( WindowEvent we )
                { System.exit( 0 ); }
            }
        );
        f.setLocation(150, 150);
        f.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if("srchAct".equals(e.getActionCommand())) 
        {
            //add search action
        }
        else if("fileAct".equals(e.getActionCommand()))
        {
            new FileViewer().fileGUI();            
        }
        else if("aboutAct".equals(e.getActionCommand()))
        {
            new AboutViewer().aboutGUI(); 
        }
        else
        {
            //debugging
            JOptionPane.showMessageDialog(null, "Something has gone wrong with actions", "Error", JOptionPane.ERROR_MESSAGE);            
        }
    }
}

Here is the second

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package project3;

/**
 *
 * @author Joe Owens, BJ Fink
 */

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;


public class FileViewer extends JFrame implements ActionListener {
    
    static JFrame f = new JFrame("Serach Index");
    LayoutManager lo;
    JLabel titleLbl, fileLbl, statusLbl, spaceLbl;
    JButton addBtn, rmvBtn, updtBtn;
    JTextArea fileTxt, statusTxt;
    
    public void fileGUI() {
        
        lo = new FlowLayout();
        f.setLayout(lo);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
        
        titleLbl = new JLabel("File Search Index");
        titleLbl.setFont(new Font("SansSeriff", Font.BOLD, 20));
        
        Font stdFont = new Font("SanSeriff", Font.PLAIN, 14);
        fileLbl = new JLabel("File Name");
        fileLbl.setFont(stdFont);
        
        statusLbl = new JLabel("Status");
        statusLbl.setFont(stdFont);
        
        spaceLbl = new JLabel("        ");
        spaceLbl.setFont(stdFont);
        
        fileTxt = new JTextArea();
        fileTxt.setColumns(14);
        fileTxt.setRows(6);
        //the user cannot manually change text in the field
        //just for displaying the files included in the index
        fileTxt.setEditable(false);
        
        statusTxt = new JTextArea();
        statusTxt.setColumns(14);
        statusTxt.setRows(6);
        statusTxt.setEditable(false);
        
        addBtn = new JButton("Add");
        addBtn.setActionCommand("srchAct");
        addBtn.setToolTipText("Add files to the index");
        addBtn.addActionListener(this);
        
        rmvBtn = new JButton("Remove");
        rmvBtn.setToolTipText("Remove files from the index");
        
        updtBtn = new JButton("Update");
        updtBtn.setToolTipText("Update the indexed files");
        
        JPanel title = new  JPanel();
            title.setLayout(new BorderLayout());
            title.add(titleLbl, BorderLayout.CENTER);
            
        JPanel top = new JPanel();
            top.setLayout(new BorderLayout());
            top.add(fileLbl, BorderLayout.WEST);
            top.add(spaceLbl, BorderLayout.CENTER);
            top.add(statusLbl, BorderLayout.EAST);
            
        JPanel mid = new JPanel();
            mid.setLayout(new BorderLayout());
            mid.add(fileTxt, BorderLayout.EAST);
            mid.add(statusTxt, BorderLayout.WEST);
            
        JPanel bot = new JPanel();
            bot.setLayout(new BorderLayout());
            bot.add(addBtn, BorderLayout.WEST);
            bot.add(rmvBtn, BorderLayout.CENTER);
            bot.add(updtBtn, BorderLayout.EAST);
            
        f.add(title);
        f.add(top);
        f.add(mid);
        f.add(bot);                  
             
    }
            
    public static void main(String args[]) {
        new FileViewer().fileGUI();
        f.setSize(320,250);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //actions by index buttons
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
}

The fileBtn should be creating a new instance of the FileViewer classes fileGUI method.

Do you have any more questions or problems?

Member Avatar for loserspearl

I'm not sure why my button events aren't working is all.

Add a println to the listener method to be sure it is being called. Print out the value of the event object that is passed to the method.
Also Print out the values used in the listener method in the if statement tests to see which one is true. See where the execution flow is going in the listener method by adding printlns inside of each if statement.

Member Avatar for loserspearl

I know the action listener is working(because the JOptionpane debugger i made) but i went ahead and did further testing (with your string idea)to see that the listener is "hearing" my action correctly with

public class BtnListener implements ActionListener {
@Override
    public void actionPerformed(ActionEvent e) {
    
        //if("srchAct".equals(e.getActionCommand())) 
        if(e.getSource() == srchBtn)
        {
            infoLbl.setText("Search Button Clicked");
            //add search action
        }
        else if(e.getSource() == fileBtn)
        {
            infoLbl.setText("File Button Clicked");
            new FileViewer().fileGUI();            
        }
        else if("aboutAct".equals(e.getActionCommand()))
        {
            infoLbl.setText("About Button Clicked");
            new AboutViewer().aboutGUI(); 
        }
        else
        {
            //debugging
            JOptionPane.showMessageDialog(null, "Something has gone wrong with actions", "Error", JOptionPane.ERROR_MESSAGE);            
        }
    }
}

BtnListener BL = new BtnListener();

I added a button listener and a nested inner class and startng using 'this' instead of frame f. The labels text changes accordingly, but I cant seem to call an instance of the external class's method that opens the new GUI.

Have you added BtnListener to the button as a listner? from your orignal code I can see you have added FileViewer as a listener to addBtn and did not implement the actionPerformed method

cant seem to call an instance of the external class's method

Did you add println statements to the constructor and the method to see if there were being called?

Member Avatar for loserspearl

Moving the setVisible to the bottom got the external class to open properly with the buttons, Thanks for the help, any idea how I can keep the textfield from extending itself when more text than the legnth enters the field? Essentially i want it to act like ur browers URL textfeild and scroll horizontally without a scroll bar

sorry, I don't do much GUI fiddling.

Member Avatar for loserspearl

Thats fine, I'm still figuring out how to do and, or, and phrase searching through files.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.