Results 1 to 5 of 5
Thread: Netbeans GUI questions
- 11-12-2011, 09:22 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 7
- Rep Power
- 0
Netbeans GUI questions
Hello im new to netbeans so i was following this tutorial on creating a simple calculator Java #N3 - Making a GUI in NetBeans - YouTube
So at the moment i have my main class and my form class.
Currently i am trying to implement a method when one of the buttons are pressed in my GUI.
I cannot figure out how i can call methods from the main class, when the method
is used in the form.Java Code:private void loadButActionPerformed(java.awt.event.ActionEvent evt) {}
What do i need to do so it can see/call the methods from the main?
-
Re: Netbeans GUI questions
Let's see your whole program.
- 11-12-2011, 09:53 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 7
- Rep Power
- 0
Re: Netbeans GUI questions
Main Class
AVLFormJava Code:import java.io.*; import java.util.Scanner; import java.lang.String.*; import java.util.StringTokenizer; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author Eric */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { AVLForm form = new AVLForm (); form.setVisible(true); // TODO code application logic here } public void LoadData() { StringTokenizer st; String str = ""; try{ // Open the file that is the first FileInputStream fstream = new FileInputStream("data.txt"); // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; //Read File Line By Line while((strLine = br.readLine()) != null) str = (Tokenize(strLine)); //Close the input stream in.close(); }catch (Exception e){//Catch exception if any System.err.println("Error: " + e.getMessage()); } st = new StringTokenizer(str," "); String s; // BST insertion while(st.hasMoreTokens()){ s = st.nextToken(); // Insert it into an AVL tree //first = Insert(s,first); } } public String Tokenize(String abc) { int i,j; String s=""; i=0; while (i<abc.length()){ if (abc.charAt(i)==',') s = s + ""; else if (abc.charAt(i)=='.') s = s + ""; else s = s + abc.charAt(i); i++; } return s; } }
Java Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * AVLForm.java * * Created on Nov 12, 2011, 2:22:52 PM */ /** * * @author Eric */ import java.io.*; import java.lang.String.*; import java.util.StringTokenizer; public class AVLForm extends javax.swing.JFrame { /** Creates new form AVLForm */ public AVLForm() { initComponents(); } /** 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. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jPanel1 = new javax.swing.JPanel(); loadBut = new javax.swing.JButton(); isAVLBut = new javax.swing.JButton(); isBSTBut = new javax.swing.JButton(); deleteBut = new javax.swing.JButton(); insertBut = new javax.swing.JButton(); printBut = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("AVL Trees"); loadBut.setText("Load Tree"); loadBut.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { loadButActionPerformed(evt); } }); isAVLBut.setText("isAVL"); isBSTBut.setText("isBST"); deleteBut.setText("Delete"); insertBut.setText("Insert"); printBut.setText("Print"); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(deleteBut) .addGap(18, 18, 18) .addComponent(isAVLBut)) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(insertBut) .addGap(18, 18, 18) .addComponent(isBSTBut)) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(loadBut) .addGap(18, 18, 18) .addComponent(printBut))) .addContainerGap()) ); jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {deleteBut, insertBut, isAVLBut, isBSTBut, loadBut, printBut}); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(loadBut) .addComponent(printBut)) .addGap(18, 18, 18) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(insertBut) .addComponent(isBSTBut)) .addGap(18, 18, 18) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(deleteBut) .addComponent(isAVLBut)) .addContainerGap()) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(98, 98, 98) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(102, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(92, 92, 92) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGap(103, 103, 103)) ); pack(); }// </editor-fold> // Insert Method private void loadButActionPerformed(java.awt.event.ActionEvent evt) { StringTokenizer st; String str = ""; try { // Open the file that is the first FileInputStream fstream = new FileInputStream("data.txt"); // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; //Read File Line By Line while ((strLine = br.readLine()) != null) { str = (Tokenize(strLine)); } //Close the input stream in.close(); } catch (Exception e) {//Catch exception if any System.err.println("Error: " + e.getMessage()); } st = new StringTokenizer(str, " "); String s; // BST insertion while (st.hasMoreTokens()) { s = st.nextToken(); // Insert it into an AVL tree //first = Insert(s,first); } // TODO add your handling code here: } public String Tokenize(String abc) { int i, j; String s = ""; i = 0; while (i < abc.length()) { if (abc.charAt(i) == ',') { s = s + ""; } else if (abc.charAt(i) == '.') { s = s + ""; } else { s = s + abc.charAt(i); } i++; } return s; } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(AVLForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(AVLForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(AVLForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(AVLForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new AVLForm().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton deleteBut; private javax.swing.JButton insertBut; private javax.swing.JButton isAVLBut; private javax.swing.JButton isBSTBut; private javax.swing.JPanel jPanel1; private javax.swing.JButton loadBut; private javax.swing.JButton printBut; // End of variables declaration }
Line 125 of the second code is what i want to call into my main method to take a data file input, tokenize it up so i can insert each string into an AVL tree
-
Re: Netbeans GUI questions
You could give the GUI class a Main field, say called "main", and then alter the GUI's constructor to take a Main parameter and then have the main field refer to the parameter. Then inside of your button's actionPerformed method, you can call methods on the main field.
- 11-13-2011, 07:22 PM #5
Member
- Join Date
- Sep 2011
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
Questions about jar files / netbeans
By trishtren in forum NetBeansReplies: 9Last Post: 04-28-2011, 12:21 PM -
questions
By amaliutz in forum New To JavaReplies: 13Last Post: 02-02-2011, 06:13 PM -
I have 2 questions =P
By santa in forum New To JavaReplies: 4Last Post: 01-19-2011, 05:35 PM -
Need help with some questions
By El_Davidos in forum New To JavaReplies: 5Last Post: 11-23-2010, 10:13 AM -
questions
By Gilgamesh in forum New To JavaReplies: 3Last Post: 11-27-2007, 11:18 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks