Swing/AWT/SWT :: Calling Method Does Not Work From JMenuBar
Nov 6, 2014
I have a paint program in Java where I can draw objects. The objects are stored in an arrayList. In the menubar the user can chooce "Back", which means the last item in the arrayList is removed. After that I want the program to loop through the arrayList and draw the remaining items.
My problem is that when I try to do that it will not work from the menu (menuItem2). If I instead add the code to one of the colorpanels (yellowPanel), from where the user can pick colors, it works fine.
menuItem2 uses ActionListener and yellowPanel uses MouseListener.
public class PaintProgram extends JFrame implements ActionListener {
I am trying to add a JMenuBar to this program with just one dropdown to select one option but I am getting an error with the setJMenuBar(menuBar); line as it does not extend JFram. How I would add a menu to this program another way.
public class Calculator extends JPanel implements ActionListener{ private static final long serialVersionUID = 1L; JMenuBar menuBar = new JMenuBar(); JMenu noteMenu = new JMenu("Note"); JMenuItem newNote = new JMenuItem("New Note"); public static final int WIDTH = 350; public static final int HEIGHT = 560;
I' currently wrking on BeatBox application from Head First Java. I made a jpanel the pane of the frame & when i try to add a JMenuBar object to that it is not getting displayed when i run the code. When i add a menuBar directly to the JFrame object it is visible & working fine but not incase when added to JPanel.
I have two classes involved in this portion. CheckIn_Search
(class #1) is my main UI and I am collecting search information from the user in a jPanel form. When they click "Search" it passes the search criteria to a method that contacts a web service and gets the results. These results are passed to coSearchResults
(class #2) that opens a jFrame and jTable (in modal format, over the main window created by CheckIn_Search.
The user selects a row in the table and that selection data is passed back to coSearchResults to populate a new jPanel form. When the data is passed from coSearchResults, I want to close its window, hide the first jPanel in CheckIn_Search and make the second jPanel visible. To accomplish this, I created a method in CheckIn_Search that simply hides/shows the panels (it will do more later).
I instantiate and call the method in CoSearchResults as part of the button click event. The problem is, it doesn't work. The panels are unaffected and there are no errors. I've put in break-points and it goes into the method.. Here is the coSearch_Results code.
Here is am retrieving the jtable row id and using the first value in the row to get the data from the table model as there is more data than what is displayed in the table. I'm sending that whole row of data to the method that changes visibility.
private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOKActionPerformed // TODO add your handling code here: int rowNum = jTable1.getSelectedRow(); String coNum = jTable1.getValueAt(rowNum,0).toString(); String[] text;
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class C1 extends JFrame { public C1() { JMenuBar b = new JMenuBar(); b.setBackground(Color.green); setJMenuBar(b); setVisible(true); } public static void main(String[] args) { C1 c = new C1(); } }
So I'm working on a project and noticed that my toString() method won't work. This is just an example of the type of code that I have in my real project. THIS IS MY MAIN CLASS
XML Code:
package trialanderror; import java.util.Scanner; public class TrialAndError { public static void main(String[] args) { Scanner keys = new Scanner(System.in); String name; String phonenumber;
The idea is to create a program to add plants and retrieve plants from a Nursery. I created a class with the properties of the plants and also there is the class an Array list to keep track of the plants entered ( they will have to be printed later) (I am not too sure if adding a public class there is the best option.
The program will allow the user to pick and action and from there the code will do something. I don't want to have all the code inside 'main' The problem is in line 114.This is what I have so far.
Java Code:
package plant.nursery; import java.util.ArrayList; import java.util.Scanner; /**Class to create a plant for a nursery. public class PlantNursery
I need to get the string encodedString from the method encode able to be used in the decode method.
Java Code:
public String encode(String plainText) { int prepareString; int shiftChar; String preparedString2 = prepareString(plainText); String encodedString = ""; for(int c = 0 ; c < preparedString2.length();c++)
I need to create an applet that displays a grid of command buttons which I have done. I then need to create a new class that draws a silly picture of an alien, which I have also done. Where I am completely stuck and confused, is that I do not know how to get the drawing into the applet. My code is below. I really do not fully understand why this is not working or how to get it working.
import java.applet.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MartianGame extends JApplet implements ActionListener { DrawMartian aMartian = new DrawMartian(); DrawJupiterian aJupiterian = new DrawJupiterian();
I am having problem to call a method from another class using arrays. I already know how to call a method without using arrays but I am not sure how to pass parameters to the main method using arrays. I'd really appreciate any feedback or comments!This is what I have so far.
public class Customer123 { public static void main(String [] args){ Scanner input = new Scanner(System.in); int x; System.out.print("Total number of customers: "); x = input.nextInt();
I have been reading about methods and I do have a beginner level understanding on how they work. I was trying to mess around and make a dog calculator using methods. I ran into a small snag; I cannot get the method to call to the main or the program to compile correctly. The first code below is the original. To me it looks like (based off of some examples I looked at) there should be no problems, but NetBeans gives me a few errors. 1) line 8- "cannot find symbol, variable dogYearCalc; 2) line 18 illegal start of expression; and 3) line 22 - unreachable statement.
import javax.swing.JOptionPane; public class KrisExample { public static void main (String[] args) { double dogYears = 0; JOptionPane.showInputDialog (null,"Please enter you dog's age in human years:");
[Code] ....
Someone told me that I was calling dogYearCalc without any arguments in your main method. I take that to mean that I needed to add it to the main, so I did here:
public static void main (String[] args, double dogYearCalc) {
That got rid of my first error, but then when I tried to run the program NetBeans said that I have no main class, so switched back to the original program above.
I thought that when I calling the dogYearCalc method on line 10 was the whole purpose of using a method. It seems to me that putting it somewhere in the main is counter productive.
calling a method from one class to another.I have two classes. One called Vacation and another called VacationDriver.I am trying to input an int for addVac and have the value update to the numSold within the updateSales() method in Vacation. From there it should update and display in the v1.toString in the VacationDriver.
Vacation import java.text.NumberFormat; public class Vacation { private String vacationName; private int numSold; private double priceEach;
I have a class for employees. This class has basic information for the employee but no real pay information. And 2 subclasses, one for employee's paid for hourly rates and one for those paid a yearly salary. Each subclass has their own pay() method, that calculates and returns their pay and extra fields relative to calculate that.
I'm just curious, if I do this and create an object for an hourly paid employee like so:
Can we recall the main method? I'm trying a code to recall main method (after the calling of JVM). I know this doesn't make any sense but I'm trying this just like that.
Code:
class derived { public static void main(String args[]) { System.out.println("Main Method class"); show(); } static void show()
I have a JSP page that calls a Java method .. using GlassFish 4.0 it worked just fine, now I'm trying to run it on a new server with Tomcat 6.0 but it keeps giving me this error: "the function result must be used with a prefix when a default namespace is not specified"
I have a file greenGrow.txt, and every three lines of the file has a last name, first name, and yard size. Every time a Customer object is created, I need to read the file 3 lines and assign the object a last name, first name, and yard size.
Snippet of my code:
import java.io.*; import java.util.*; public class Customer { private String lastName; private String firstName; private int yardSize;
[Code] .....
My issue is that I cannot call readFile() from the constructor, and I'm assuming that's because I throw Exception on readFile(). Is this a simple fix, or am I looking at it the wrong way?
My program crashes every time I call this method because of the while loop. I guess its going into a infinite loop. The game should keep running until the user ends it. Is the use of my .isKeyPressed correct and if so what else could it be?
public static void run() { while (true) { if (StdDraw.isKeyPressed(KeyEvent.VK_Q)) { break;
I need to access a method from a dll in java as below and is giving an error
"Exception in thread "main" java.lang.UnsatisfiedLinkError: Testdll.Decrypt(Ljava/lang/StringLjava/lang/String; at Testdll.Decrypt(Native Method) at Testdll.main(Testdll.java:31) " I have included the jna-4.0.0.jar and the HashMatchCryptography.dll to the project in eclipe and the java-library-path has the path for the lib
// Display a message, preceded by // the name of the current thread static void threadMessage(String message) { String threadName = Thread.currentThread().getName();
[Code] .....
I want to know How join() and interrupt() method is working on this program?
When I try to call an object it can't find the symbol in the argument list. NetBeans says that it cannot find the movieCategory symbol when I try to call it. When I compile it to test a popup comes up that states "One or more projects were compiled with errors. Application you are running may end unexpectedly. I ran it anyways and everything runs up to the point of where it should call the object.
At this point it should get the Movie object and run the code within that, but if I put one of the categories it throws. "Exception in thread "main" java. lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>at MovieApp.main(MovieApp.java:33)Java Result: 1"
From my understanding to call an object it requires objectName.methodName(argumentList). Here is my Main method
/** *This application will store a list of 100 movies and display them by category */ import java.util.Scanner; public class MovieApp { public static void main(String args[]) { //Displays <code>String</code> welcome message System.out.println("Welcome to the Movie Application."); System.out.println("There are 100 movies in the list."); System.out.println("What category are you interested in?"); System.out.println();
I've created a getMin method to return the smallest value in a queue. However, I'm having trouble calling the method in my main.
/** * Main method. * * @param args * the command line arguments; unused here */ public static void main(String[] args) { SimpleReader in = new SimpleReader1L(); SimpleWriter out = new SimpleWriter1L(); Queue<Integer> testQueue = new Queue1L<Integer>();
This current one is to calculate a planes holding pattern. I have to write a method to prompt user to enter speed in knots, then it converts it to km/hr. Another method to calc. pattern width using the speed, another method to calc. pattern length, than a main method which would call and print out the speed in knots, speed in km, pattern width and length.My current problem is on the second method. It works in that I can enter the values and it gives me the correct answers, however it's asking me to enter the speed twice, instead of just once. Anything I try just results in errors and won't compile.
import java.util.Scanner ; //main method public class TitleRemoved { public static void main(String[] args) { double airSpeedKm = airSpeedOts () ; System.out.println("That speed is " + airSpeedKm + " km/hr.") ;
[code].....
I want my code to not only work, but be organized and easily readable as well, so I want to avoid bad habits.