Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

letsdiscussit

macrumors newbie
Original poster
Jun 26, 2007
27
0
I am trying to build a GUI interface with a search function.
unfortunaltey I have a problem with search function I want to apply to a button:
Code:
private void sortButtonActionPerformed(java.awt.event.ActionEvent evt) {
       
       int a = (int)((Double.parseDouble(numberField.getText())));
    sort (b);
       sortBox.print( g, head, b, x, y + "sorted");

Here is the code of the whole swing appl. It would be too much code so I highlighted only the search code to focus on
Code:
/*
* GUI.java
 *
 * Created on April 24, 2008, 6:43 PM
 */

package learn;
import java.awt.Graphics;
/**
 *
 * @author  alexander
 */
public class GUI extends javax.swing.JFrame {
    
    /** Creates new form GUI */
    public GUI() {
        initComponents();
    }

   [B] public void sort(int b[]) {
        int temp;
        for (int i = 0; i < b.length - 1; i++) {
            for (int j = i + 1; j < b.length; j++) {
                if (b[i] > b[j]) {
                    temp = b[i];
                    b[i] = b[j];
                    b[j] = temp;
                }
            }

        }
    }
      public void print(Graphics g, String head, int b[], int x, int y) {
        g.drawString(head, x, y);
        x += 15;
        y += 15;
        for (int i = 0; i < b.length; i++) {
            g.drawString(String.valueOf(b[i]), x, y);
            x += 20;
        }
    }[/B]
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        firstLabel = new javax.swing.JLabel();
        secondLabel = new javax.swing.JLabel();
        numberField = new javax.swing.JTextField();
        numberText = new javax.swing.JLabel();
        sortButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        sortBox = new javax.swing.JTextPane();
        exitButton = new javax.swing.JButton();
        clearButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setAlwaysOnTop(true);
        setBackground(new java.awt.Color(200, 200, 100));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        firstLabel.setFont(new java.awt.Font("Lucida Grande", 0, 18));
        firstLabel.setText("Java Programming");

        secondLabel.setText("Assignment 3");

        numberField.setText("Enter number here");
        numberField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                numberFieldActionPerformed(evt);
            }
        });

        numberText.setText("Enter random number:");

        sortButton.setText("Sort");
        sortButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sortButtonActionPerformed(evt);
            }
        });

        jScrollPane1.setViewportView(sortBox);

        exitButton.setText("Exit");
        exitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitButtonActionPerformed(evt);
            }
        });

        clearButton.setText("Clear");
        clearButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(146, 146, 146)
                        .add(secondLabel))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .add(numberText))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(sortButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)
                            .add(numberField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(111, 111, 111)
                        .add(firstLabel))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                .add(clearButton)
                                .add(66, 66, 66)
                                .add(exitButton))
                            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(20, 20, 20)
                .add(firstLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(secondLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 16, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(46, 46, 46)
                .add(numberText)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(numberField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(sortButton)
                .add(18, 18, 18)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(exitButton)
                    .add(clearButton))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void numberFieldActionPerformed(java.awt.event.ActionEvent evt) {
        
        // TODO add your handling code here:
}

[B]    private void sortButtonActionPerformed(java.awt.event.ActionEvent evt) {
       
       int a = (int)((Double.parseDouble(numberField.getText())));
    sort (b);
       sortBox.print( g, head, b, x, y + "sorted");
       
        //instead of "print", "setText"?[/B]
}

    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {
System.exit (0);       
    }

    private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) {
     numberField.setText("");
     sortBox.setText("");
     // TODO add your handling code here:
}
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUI().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify
    private javax.swing.JButton clearButton;
    private javax.swing.JButton exitButton;
    private javax.swing.JLabel firstLabel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField numberField;
    private javax.swing.JLabel numberText;
    private javax.swing.JLabel secondLabel;
    private javax.swing.JTextPane sortBox;
    private javax.swing.JButton sortButton;
    // End of variables declaration
    
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.