I'm trying to use the jDatePicker tool from here: sourceforge.net/projects/jdatepicker/files/latest/download and I've placed the .jar file in C:\Program Files (x86)\BlueJ\lib\userlib and it's been recognised in the BlueJ preferences but I have no idea how to actually use it in my project. I've tried all sorts of import commands but it's not picking it up. Any ideas?
Update: OK, i've now got it to compile, but the applet doesn't run, BlueJ just says "Applet not initialised":
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import java.util.*;
import java.util.Calendar;
import java.util.Date;
import net.sourceforge.jdatepicker.*;
import net.sourceforge.jdatepicker.impl.*;
import net.sourceforge.jdatepicker.util.*;
public class Task1 extends java.applet.Applet implements ActionListener
{
public void init()
{
setLayout(null);
setSize(200,240);
JButton btnConfirm=new JButton("Confirm"); //initalises the button
btnConfirm.setBounds(15,2,100,20);
add(btnConfirm); //paints it on the screen
btnConfirm.addActionListener(this);
TextField text = new TextField(20);
text.setBounds(5,24,185,20);
add(text);
UtilDateModel model = new UtilDateModel();
JDatePanelImpl datePanel = new JDatePanelImpl(model);
JDatePickerImpl datePicker = new JDatePickerImpl(datePanel);
datePicker.setBounds(50,80,185,20);
add(datePicker);
}
/* Use the method actionPerformed to trap the event button clicked */
public void actionPerformed(ActionEvent evt)
{
JOptionPane.showMessageDialog(null,"ALERT MESSAGE","TITLE",JOptionPane.WARNING_MESSAGE);
}
}
To import other classes in your Java files, you need to add an import statement with the full qualified name of the class (The file name with the package).
To import the JDatePicker class, you need to add:
import org.jdatepicker.JDatePicker;
If you want to import all the classes in a package, you can use *:
import org.jdatepicker.*;
The classes must be present in the Classpath. As you are adding the JAR file in the BlueJ\lib\userlib folder, this should be enough to import the classes correctly.
You may need to import other classes in other packages like:
import org.jdatepicker.impl.*;
import org.jdatepicker.util.*;
Import your required classes or packages as needed.
You need to tell java where to find the library. You do this by adding the location, where you put the library to your classpath.
Usually your IDE supports you in this process by providing a GUI to do this.
For BlueJ, take a look here: http://www.bluej.org/faq.html#faq_How_do_I_use_custom_class_libraries__JARs__
Related
I have this class that I want to import into another class that is outside the previous class folder.
So, I have a GoogleDriveAPI class, which I want to import to DocumentServices class.
on top of my GoogleDriveAPI class there is this line
package org.ofbiz.ClientManagementServices;
but when I try to import it to DocumentServices class with this line below
import org.ofbiz.clientmanagementservices.GoogleDriveAPI;
I get this error below,
error: package org.ofbiz.clientmanagementservices does not exist
[javac17] import org.ofbiz.clientmanagementservices.GoogleDriveAPI;
What might be the problem with my import because I am 100% sure I am doing the right thing?
Java packages are cas sensitive.
You should change the import to :
import org.ofbiz.ClientManagementServices.GoogleDriveAPI;
I seem to have a problem with IntelliJ concerning the setOnAction lambda function. After lots of research I can not find an answer to my problem.
I'm learning to program in Java (latest JDK 8 version) and recently moved from NetBeans to IntelliJ IDE. I wrote a very simple program and the code works perfectly in NetBeans, but I have problems using IntelliJ.
The setOnAction function is not recognized by IntelliJ. I have configured the IDE (Project Structure/Modules/ and chose 8 - Lambda's, type annotations,...) but with no success. I manually added: import javafx.event.ActionEvent;
I have also configured (Settings/General/Auto Import/Add unambiguous imports on the fly).
The program contains two classes, a Main class and a GUI class.
Main class:
package fxvb0203;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
public class fxvb0203 extends Application
{
public static void main(String[] args)
{
launch(args);
}
#Override
public void start(Stage primaryStage)
{
FlowPane root = new FlowPane();
Scene scene = new Scene(root);
new GUI(root);
primaryStage.setTitle("Test");
primaryStage.setScene(scene);
primaryStage.show();
}
}
GUI class:
package fxvb0203;
import javafx.scene.layout.FlowPane;
import javafx.event.ActionEvent;
import java.awt.*;
public class GUI
{
private final Button btnText = new Button("Text");
private final TextField txtField = new TextField();
public GUI(FlowPane pane)
{
btnText.setOnAction(event ->
{
txtField.setText("text");
});
pane.getChildren().add(btnText);
pane.getChildren().add(txtField);
}
}
setOnAction gives the following warning:
can not resolve method setOnAction, (lambda expression)
import javafx.event.ActionEvent; is greyed out as it is not used, which is strange. So it must be something with IntelliJ that is not correct.
Another small problem I have with IntelliJ only, but which does not relate to the main problem, is the following:
pane.getChildren().add(btnText);
pane.getChildren().add(txtField);
These two lines of code give the following error:
add (javafx.scene.Node) in list cannot be applied to (java.awt.TextField)
In NetBeans both work fine, but IntelliJ gives problems.
I hope to rely on the professional help of this community, because I am kinda stuck here and besides these small problems I really like the IntelliJ IDE.
Many thanks in advance.
Greets.
In your GUI class you have the following import statement: import java.awt.*;. The * means you're importing everything inside the java.awt package which includes (among others): java.awt.TextField and java.awt.Button.
What this means is that instead of btnText being a javafx.scene.control.Button it is actually a java.awt.Button. The AWT Button does not have a setOnAction method therefore you are getting a compilation error. This also affects your txtField which is now a java.awt.TextField instead of a javafx.scene.control.TextField.
Replace import java.awt.* with import javafx.scene.control.Button and import javafx.scene.control.TextField; or replace it with import javafx.scene.control.*.
In case importing java.awt.* was on purpose it is generally a bad idea to mix different GUI toolkits together.
I am working on a somewhat large project with many subdirectories. I am, however, coming across an issue of importing classes from within another directory. The directory structure is as so:
main.dir
repository.dir
Bill.java
transaction.dir
AutomaticBillPay.java
How can I import Bill into AutomaticBillPay?
I have tried may iterations of:
package main;
package main.repositorysys;
import main.repositorysys.Bill;
import repositorysys.Bill;
import Bill;
Sadly, the only line that compiles is the first: package main;. Any tips / direction will help!
You can achieve it through this
/*Declare your class package */
package main.transactionsubsys;
/*import the classes you want */
import main.repositorysys.Bill;
/*Write your class*/
public class AutomaticBillPay {
/*AutomaticBillPay code */
}
Your AutomaticBillPay should look like this:
package main.transaction;
import main.repository.Bill;
public class AutomaticBillPay {
// your class implementation here
}
Not sure where repositorysys came from?
package should be the full path to your encompassing directory
import should be the full path to the class you want to import
I have several java files in a package and all of them have the same import blocks e.g :
package org.ezim.core;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import org.ezim.core.Ezim;
import org.ezim.core.EzimDtxSemantics;
import org.ezim.core.EzimLogger;
import org.ezim.ui.EzimFileOut;
import org.ezim.ui.EzimMain;
It looks awful having the same batches of code in each file and i want to refactor it.
I was wondering if its possible to put all these imports in a single java file then use a single line in all the other java files to call them.
Its like the extend function for classes (for variables) , but i want one for the imports.
Thanks
No. That isn't possible. What is possible is not using imports at all, instead you can use fully qualified class names like
org.ezim.core.Ezim ezim = new org.ezim.core.Ezim(); // <-- not import needed.
You can always use * sign to import multiple classes from one package, but thus watch for name clashes.
I am working on creating a computer controlled bots for a game using Java. I got a example bot program and I am understanding this currently.
I am not able to understand what does #JProp means in the code below. Can any one help me on this. Also, how do I view all the contents of the import files at the start of the program.
package com.mycompany.mavenproject1;
import cz.cuni.amis.introspection.java.JProp;
import cz.cuni.amis.pogamut.base.agent.impl.AgentId;
import cz.cuni.amis.pogamut.base.agent.module.comm.PogamutJVMComm;
import cz.cuni.amis.pogamut.base.agent.navigation.IPathExecutorState;
import cz.cuni.amis.pogamut.base.communication.worldview.listener.annotation.EventListener;
import cz.cuni.amis.pogamut.base.utils.guice.AgentScoped;
import cz.cuni.amis.pogamut.base3d.worldview.object.ILocated;
import cz.cuni.amis.pogamut.base3d.worldview.object.Location;
import cz.cuni.amis.pogamut.unreal.communication.messages.UnrealId;
import cz.cuni.amis.pogamut.ut2004.agent.module.utils.TabooSet;
import cz.cuni.amis.pogamut.ut2004.agent.navigation.UT2004PathAutoFixer;
import cz.cuni.amis.pogamut.ut2004.agent.navigation.stuckdetector.UT2004DistanceStuckDetector;
import cz.cuni.amis.pogamut.ut2004.agent.navigation.stuckdetector.UT2004PositionStuckDetector;
import cz.cuni.amis.pogamut.ut2004.agent.navigation.stuckdetector.UT2004TimeStuckDetector;
import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004Bot;
import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004BotModuleController;
import cz.cuni.amis.pogamut.ut2004.bot.params.UT2004BotParameters;
import cz.cuni.amis.pogamut.ut2004.communication.messages.UT2004ItemType;
import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.Initialize;
import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.BotKilled;
import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.ConfigChange;
import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.FlagInfo;
import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.GameInfo;
import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.InitedMessage;
import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Item;
import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.NavPoint;
import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Player;
import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.PlayerKilled;
import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Self;
import cz.cuni.amis.pogamut.ut2004.utils.UT2004BotRunner;
import cz.cuni.amis.utils.Heatup;
import cz.cuni.amis.utils.exception.PogamutException;
import cz.cuni.amis.utils.flag.FlagListener;
/**
* Example of Simple Pogamut bot, that randomly walks around the map searching
* for preys shooting at everything that is in its way.
*
* #author Rudolf Kadlec aka ik
* #author Jimmy
*/
#AgentScoped
public class CTFBot extends UT2004BotModuleController<UT2004Bot> {
/** boolean switch to activate engage behavior */
#JProp
public boolean shouldEngage = true;
/** boolean switch to activate pursue behavior */
It seems this JProp annotation is used for introspection purposes (allowing the contents of the variable which is decorated to be easily inspected from within your IDE).
Quoting this manual:
Introspection is designed to ease the bot's parameterization. It is
often needed to adjust multiple behavior parameters at runtime and you
will probably end up creating your own GUI (graphical user interface)
for this purpose. In introspection, you just annotate desired
variables with #JProp annotation and they will be accessible via the
Netbeans GUI.
Let's look how logging and introspection works in EmptyBot example.
First start the bot (F6), then have a look on it's source code. In the
initial section several variables annotated with the #JProp are
defined.
#JProp
public String stringProp = "Hello bot example";
#JProp
public boolean boolProp = true;
#JProp
public int intProp = 2;
#JProp
public double doubleProp = 1.0;
Now expand bot's node under the UT server node (in Services tab), you
will see two new nodes - Logs and Introspection. After selecting the
Introspection node the annotated variables will be shown in the
Properties (Ctrl + Shift + 7) window. Note that the intProp variable
is being continuously updated. New values of variables can be also set
in this window.