How to show context menu in FlowPane body - javafx-2

I have a FlowPane which use used as stage for many BorderPane components. I created Context menu for the BorderPane components. I want to add second context menu also for the FlowPane but it turns out that I now have two context menus when I click on BorderPane. I need a way to exclude the second context menu from the BorderPane.
FlowPane flow;
public ScrollPane infrastructurePane()
{
flow = new FlowPane();
flow.setPadding(new Insets(5, 5, 5, 5));
flow.setVgap(15);
flow.setHgap(15);
flow.setAlignment(Pos.CENTER);
ScrollPane scroll = new ScrollPane();
scroll.setStyle("-fx-background-color:transparent;");
Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
scroll.setPrefSize(primaryScreenBounds.getWidth(), primaryScreenBounds.getHeight());
scroll.setHbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED); // Horizontal scroll bar
scroll.setVbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED); // Vertical scroll bar
scroll.setContent(flow);
scroll.viewportBoundsProperty().addListener(new ChangeListener<Bounds>()
{
#Override
public void changed(ObservableValue<? extends Bounds> ov, Bounds oldBounds, Bounds bounds)
{
flow.setPrefWidth(bounds.getWidth());
flow.setPrefHeight(bounds.getHeight());
}
});
//flow.setPrefWrapLength(170); // preferred width allows for two columns
flow.setStyle("-fx-background-color: white;");
for (int i = 0; i < 28; i++)
{
flow.getChildren().add(generatePanel(flow));
}
scroll = makeTabContextMenu(scroll);
String cssURL = "/com/dx57dc/css/ButtonsDemo.css";
String css = this.getClass().getResource(cssURL).toExternalForm();
flow.getStylesheets().add(css);
return scroll;
}
public BorderPane generatePanel(FlowPane flow)
{
HBox thb = new HBox();
thb.setPadding(new Insets(10, 10, 10, 10));
thb.setStyle("-fx-background-color: #006699;");
HBox bhb = new HBox();
bhb.setPadding(new Insets(10, 10, 10, 10));
bhb.setStyle("-fx-background-color: #B0B0B0;");
DropShadow ds = new DropShadow();
ds.setOffsetY(3.0);
ds.setOffsetX(3.0);
ds.setColor(Color.GRAY);
BorderPane bp = new BorderPane();
bp.setEffect(ds);
bp.setCache(true);
bp.setPrefSize(320, 180);
bp.setMaxSize(320, 180);
bp.setId("app");
bp.setStyle("-fx-background-color: linear-gradient(to bottom, #f2f2f2, #d4d4d4);"
+ " -fx-border: 2px solid; -fx-border-color: white;");
bp.setTop(thb);
bp.setBottom(bhb);
ScrollPane sp = new ScrollPane();
bp = panelContextMenu(bp);
bp = mouseOver(bp);
return bp;
}
public BorderPane mouseOver(final BorderPane bp)
{
bp.setOnMouseEntered(new EventHandler<MouseEvent>()
{
#Override
public void handle(MouseEvent t)
{
bp.setStyle("-fx-border: 2px solid; -fx-border-color: black;");
}
});
bp.setOnMouseExited(new EventHandler<MouseEvent>()
{
#Override
public void handle(MouseEvent t)
{
bp.setStyle("-fx-border: 2px solid; -fx-border-color: white;");
}
});
bp.setOnMouseClicked(new EventHandler<MouseEvent>()
{
#Override
public void handle(MouseEvent me)
{
if (me.getButton().equals(MouseButton.PRIMARY) && me.getClickCount() % 1 == 0)
{
bp.setPrefSize(480, 280);
bp.setMaxSize(480, 280);
}
if (me.getButton().equals(MouseButton.PRIMARY) && me.getClickCount() % 2 == 0)
{
bp.setPrefSize(320, 180);
bp.setMaxSize(320, 180);
}
}
});
return bp;
}
private ScrollPane makeTabContextMenu(ScrollPane scroll)
{
ContextMenu contextMenu = new ContextMenu();
MenuItem item1 = new MenuItem("New");
item1.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent e)
{
System.out.println("New");
flow.getChildren().add(generatePanel(flow));
}
});
// Rename
MenuItem rename = new MenuItem("Rename");
final TextField textField = new TextField();
rename.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent e)
{
System.out.println("Rename");
//textField.setText(label.getText());
//tab.setGraphic(textField);
textField.selectAll();
textField.requestFocus();
}
});
textField.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent event)
{
//label.setText(textField.getText());
//tab.setGraphic(label);
}
});
textField.focusedProperty().addListener(new ChangeListener<Boolean>()
{
#Override
public void changed(ObservableValue<? extends Boolean> observable,
Boolean oldValue, Boolean newValue)
{
if (!newValue)
{
//label.setText(textField.getText());
//tab.setGraphic(label);
}
}
});
// Orientation
Menu tabOrientation = new Menu("Orientation");
// Orientation sub menu - Top, Left, Right, Bottom
MenuItem tabOrientationSubTop = new MenuItem("Top");
tabOrientationSubTop.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent e)
{
//tabPane.setSide(Side.TOP);
}
});
MenuItem tabOrientationSubLeft = new MenuItem("Left");
tabOrientationSubLeft.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent e)
{
//tabPane.setSide(Side.LEFT);
}
});
MenuItem tabOrientationSubRight = new MenuItem("Right");
tabOrientationSubRight.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent e)
{
//tabPane.setSide(Side.RIGHT);
}
});
MenuItem tabOrientationSubBottom = new MenuItem("Bottom");
tabOrientationSubBottom.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent e)
{
//tabPane.setSide(Side.BOTTOM);
}
});
tabOrientation.getItems().addAll(tabOrientationSubTop, tabOrientationSubLeft, tabOrientationSubRight, tabOrientationSubBottom);
MenuItem item3 = new MenuItem("Close Tab");
item3.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent e)
{
System.out.println("Close Tab");
// Close the Tab and remove it from the TabPane
//tabPane.getTabs().remove(tab);
// If there are no tabs into the TabPane fill all empty space by resizing the near components
// if (tabPane.getTabs().size() == 0)
// {
// ActionTabs.getMainPane().setManaged(false); // Exclude the panel to fill the empty space
// }
}
});
MenuItem item4 = new MenuItem("Close All Panels");
item4.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent e)
{
System.out.println("Close All Panels");
flow.getChildren().clear();
}
});
contextMenu.getItems().addAll(item1, rename, tabOrientation, item3, item4);
scroll.setContextMenu(contextMenu);
return scroll;
}
public BorderPane panelContextMenu(final BorderPane bp)
{
final ContextMenu contextMenu = new ContextMenu();
MenuItem item1 = new MenuItem("About");
item1.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent e)
{
System.out.println("About");
}
});
MenuItem item2 = new MenuItem("Preferences");
item2.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent e)
{
System.out.println("Preferences");
}
});
MenuItem item3 = new MenuItem("Close");
item3.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent e)
{
flow.getChildren().remove(bp);
}
});
contextMenu.getItems().addAll(item1, item2, item3);
bp.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>()
{
#Override
public void handle(ContextMenuEvent event)
{
contextMenu.show(bp, event.getScreenX(), event.getScreenY());
}
});
bp.addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>()
{
#Override
public void handle(MouseEvent event)
{
contextMenu.hide();
}
});
return bp;
}
Is there a way to show makeTabContextMenu() only visible for the FlowPane body?

Have you tried to set the event consumed?
#Override
public void handle(MouseEvent t)
{
...
t.consume()
...
}

Related

Android Studio getFilter does not sort

This is my RecyclerViewAdapter code
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolder> implements Filterable{
Context context;
ArrayList<RecyclerViewItems> itemsArrayList;
ArrayList<RecyclerViewItems> itemsArrayListFull;
private SelectItemListener listener;
RelativeLayout plannerLayout;
public RecyclerViewAdapter(Context context, ArrayList<RecyclerViewItems> itemsArrayList, SelectItemListener listener) {
this.context = context;
this.itemsArrayListFull = itemsArrayList;
this.itemsArrayList = new ArrayList<>(itemsArrayListFull);
this.listener = listener;
}
#NonNull
#Override
public RecyclerViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new RecyclerViewHolder(LayoutInflater.from(context).inflate(R.layout.recyclerview_items_layout, parent, false));
}
#Override
public void onBindViewHolder(#NonNull RecyclerViewHolder holder, int position) {
holder.nameView.setText(itemsArrayListFull.get(position).getName());
holder.sizeView.setText(itemsArrayListFull.get(position).getSizeString());
holder.imageView.setImageResource(itemsArrayListFull.get(position).getImage());
holder.imageView.setTag(itemsArrayListFull.get(position).getImage());
holder.imageView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
listener.onItemSelected(holder.imageView);
return false;
}
});
}
#Override
public int getItemCount() {
return itemsArrayListFull.size();
}
#Override
public Filter getFilter() {
return itemsFilter;
}
private final Filter itemsFilter = new Filter(){
#Override
protected FilterResults performFiltering(CharSequence charSequence) {
ArrayList<RecyclerViewItems> filteredItemsList = new ArrayList<>();
if (charSequence == null || charSequence.length() == 0){
filteredItemsList.addAll(itemsArrayListFull);
} else {
String filterPattern = charSequence.toString().toLowerCase().trim();
for(RecyclerViewItems recyclerViewItems : itemsArrayListFull){
if(recyclerViewItems.name.toLowerCase().contains(filterPattern)){
filteredItemsList.add(recyclerViewItems);
}
}
}
FilterResults results = new FilterResults();
results.values = filteredItemsList;
results.count = filteredItemsList.size();
return results;
}
#Override
protected void publishResults(CharSequence charSequence, FilterResults results) {
itemsArrayList.clear();
itemsArrayList.addAll((ArrayList)results.values);
notifyDataSetChanged();
}
};
}
This is my main activity page.
public class Planner_Area_Page extends AppCompatActivity implements AdapterView.OnItemSelectedListener, SelectItemListener {
Spinner spinner;
//Planner area layout variables
RelativeLayout plannerArea;
BathroomPlannerLayout bathroomPlannerLayout;
RecyclerViewAdapter recyclerViewAdapter;
ArrayList<RecyclerViewItems> itemsArrayList;
RecyclerView recyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_planner_area);
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//Planner area layout init
plannerArea = findViewById(R.id.plannerArea);
bathroomPlannerLayout = new BathroomPlannerLayout(this, plannerArea);
itemsArrayList = new ArrayList<RecyclerViewItems>();
setSpinner();
initSearchWidget();
makeResponsive();
getData();
}
private void getData() {
itemsArrayList.add(new RecyclerViewItems("toilet", 9, 9, R.drawable.toilet));
itemsArrayList.add(new RecyclerViewItems("shower", 9, 9, R.drawable.toilet));
itemsArrayList.add(new RecyclerViewItems("toilet", 9, 9, R.drawable.toilet));
itemsArrayList.add(new RecyclerViewItems("toilet", 9, 9, R.drawable.toilet));
itemsArrayList.add(new RecyclerViewItems("toilet", 9, 9, R.drawable.toilet));
recyclerViewAdapter = new RecyclerViewAdapter(getApplicationContext(), itemsArrayList, this);
recyclerView.setAdapter(recyclerViewAdapter);
recyclerViewAdapter.notifyDataSetChanged();
}
//set searchview on the menubar
#Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.menu_item,menu);
MenuItem menuItem = menu.findItem(R.id.app_bar_search );
SearchView searchView = (SearchView) menuItem.getActionView();
searchView.setMaxWidth(Integer.MAX_VALUE);
searchView.setQueryHint("Search Here!");
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String userText) {
return false;
}
#Override
public boolean onQueryTextChange(String userText) {
recyclerViewAdapter.getFilter().filter(userText);
return true;
}
});
return super.onCreateOptionsMenu(menu);
}
I thought when I type any word on the searchview, and that text goest to getfilter method with parameter"userText".
and then that "userText" goes into itemsFilter and sort the items and put new item list in the filtereditemslist.
But it is not sorting when I run debug. How can I sort this out?

How do i update my firebase adapter with new items without repopulating the entire list

I'm trying to implement endless scrolling in my firebase recycler view.I limit my query to 5 items then the users scrolls to the end of the list the query is updated and the limit is increased my another 5. But the adapter only updates when i leave and return to the activity. I tried notifydatasetchange(), but that doesn't work. I tried recyclerview.setadapter(). It works but it also resets the scroll position of my recycler view.Nothing else seems to work.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view=inflater.inflate(R.layout.card_view,container,false);
mDatabase= FirebaseDatabase.getInstance().getReference().child("Posts");
mDatabaseUpvote= FirebaseDatabase.getInstance().getReference().child("Upvote");
mAuth=FirebaseAuth.getInstance();
recyclerView = (RecyclerView) view.findViewById(R.id.recycleview_new);
recyclerView.setHasFixedSize(true);
mLayoutManager=(new LinearLayoutManager(getActivity()));
recyclerView.setLayoutManager(mLayoutManager);
mDatabaseUpvote.keepSynced(true);
mDatabase.keepSynced(true);
query=mDatabase.limitToFirst(count);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
firstItem=mLayoutManager.findLastVisibleItemPosition();
ItemCount=recyclerView.getChildCount();
TotalItemCount=mLayoutManager.getItemCount();
if (loading){
if (TotalItemCount>prevTotal){
loading=false;
prevTotal=TotalItemCount;
}
}
if (!loading && (TotalItemCount-firstItem) <= (firstItem+visthresh)){
count=count+2;
query=mDatabase.limitToFirst(count);
Toast.makeText(getActivity(), "ThresholdReached", Toast.LENGTH_SHORT).show();
loading=true;
}
}
});
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Cards, Postviewholder>(
Cards.class,
R.layout.cards,
Postviewholder.class,
query
) {
#Override
protected void populateViewHolder(final Postviewholder viewHolder, Cards model, final int position) {
final String postKey=getRef(position).getKey();
viewHolder.setUsername(model.getUsername());
viewHolder.setImage(getActivity(),model.getImage());
viewHolder.setimage_icon(getActivity(),model.getImageicon());
viewHolder.setTitle(model.getTitle());
viewHolder.setTextColor(model.getTextColor());
viewHolder.setUpvote(postKey);
viewHolder.post_Image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent2= new Intent(getActivity(),View_post.class);
intent2.putExtra("Userprofile",postKey);
startActivity(intent2);
}
});
viewHolder.imageicon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent2= new Intent(getActivity(),UserActivity_2.class);
intent2.putExtra("Userprofile",postKey);
startActivity(intent2);
}
});
viewHolder.upvote.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ProcessUpvote = true;
mDatabaseUpvote.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (ProcessUpvote) {
if (dataSnapshot.child(postKey).hasChild(mAuth.getCurrentUser().getUid())) {
mDatabaseUpvote.child(postKey).child(mAuth.getCurrentUser().getUid()).removeValue();
ProcessUpvote = false;
} else {
mDatabaseUpvote.child(postKey).child(mAuth.getCurrentUser().getUid()).setValue("RandomValue");
ProcessUpvote = false;
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
if (sharedPreferences != null) {
SharedPreferences preferences=PreferenceManager.getDefaultSharedPreferences(getActivity());
Posit=(preferences.getInt(RV_POS_INDEX,0));
mRvTopView=(preferences.getInt(RV_TOP_VIEW,0));
Toast.makeText(getActivity(), "RestoreState", Toast.LENGTH_SHORT).show();
firebaseRecyclerAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
public void onItemRangeInserted(int positionStart, int itemCount) {
mLayoutManager.scrollToPositionWithOffset(Posit, mRvTopView);
}
});
}
return view;
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
Posit = mLayoutManager.findFirstVisibleItemPosition();
View startView = recyclerView.getChildAt(0);
mRvTopView = (startView == null) ? 0 : (startView.getTop() - recyclerView.getPaddingTop());
Toast.makeText(getActivity(), "SaveState", Toast.LENGTH_SHORT).show();
sharedPreferences= PreferenceManager.getDefaultSharedPreferences(getActivity());
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putInt(RV_POS_INDEX,Posit);
editor.putInt(RV_TOP_VIEW,mRvTopView);
editor.apply();
}
public static class Postviewholder extends RecyclerView.ViewHolder {
View mView;
ImageView upvote;
ImageView imageicon;
ImageView post_Image;
private DatabaseReference mDatabaseUpvote;
private FirebaseAuth mAuth;
public Postviewholder(View itemView) {
super(itemView);
mView = itemView;
imageicon = (ImageView) mView.findViewById(R.id.Post_imiageicon);
post_Image = (ImageView) mView.findViewById(R.id.CardImage);
upvote = (ImageView) mView.findViewById(R.id.upvote);
mDatabaseUpvote = FirebaseDatabase.getInstance().getReference().child("Upvote");
mAuth = FirebaseAuth.getInstance();
}
public void setUpvote(final String postKey) {
mDatabaseUpvote.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.child(postKey).hasChild(mAuth.getCurrentUser().getUid())) {
upvote.setImageResource(R.drawable.ic_thumb_up_black_24dp2);
} else {
upvote.setImageResource(R.drawable.ic_thumb_up_black_24dp);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
public void setImage(final Context ctx, final String image) {
final ImageView imageView = (ImageView) mView.findViewById(R.id.CardImage);
Glide.with(ctx)
.load(image)
.into(imageView);
}
public void setTitle(String title) {
TextView cardtitle = (TextView) mView.findViewById(R.id.cardTitle);
cardtitle.setText(title);
}
public void setUsername(String username) {
TextView post_username = (TextView) mView.findViewById(R.id.post_username);
post_username.setText(username);
}
public void setimage_icon(final Context ctx, final String imageicon) {
final ImageView image_icon = (ImageView) mView.findViewById(R.id.Post_imiageicon);
Glide.with(ctx)
.load(imageicon)
.into(image_icon);
}
public void setTextColor(int TextColor) {
CardView cardView= (CardView)mView.findViewById(R.id.Cardview_card);
TextView post_username = (TextView) mView.findViewById(R.id.post_username);
TextView cardtitle = (TextView) mView.findViewById(R.id.cardTitle);
int textcolr=getContrastColor(TextColor);
int white=Color.WHITE;
int resultColor = blendARGB(TextColor,white,0.1f);
cardView.setCardBackgroundColor(resultColor);
cardtitle.setTextColor(textcolr);
post_username.setTextColor(textcolr);
}
}
}

JavaFX is locking the app when after ActionEvent is done

I am building a menu (file, view, help, etc.). For ex. in File you have upload and exit (both working fine). In help I have some topics: ACF, ARIMA, HoltWinter, the idea is when you chose one of these a text to be displayed in a GridPane. The problem is when you chose one, it displays its text, but it also locks the whole app and you can do nothing, except close the app.
This is a file chooser and it's working just fine:
MenuItem upload = new MenuItem("Upload");
upload.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Resource File");
fileChooser.showOpenDialog(primaryStage);
}
});
And here is an event, which is blocking the app:
MenuItem acfH = new MenuItem("ACF");
acfH.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent event){
fr.md.pack.TextClass.acfText.setFill(Color.FIREBRICK);
fr.md.pack.TextClass.acfText.setText(fr.md.pack.StringClass.aboutACF);
grid.add(fr.md.pack.TextClass.acfText,1,10);
}
});
Any suggestions how to solve the issue?
Thanks in advance.
Here is the whole code:
public class MD extends Application {
#Override
public void start(final Stage primaryStage) throws IOException {
primaryStage.setTitle("Crude oil price prediction");
primaryStage.getIcons().add(new Image("file:resources/images/DukeWithHelmet.png"));
final GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25, 25, 25, 25));
Group root = new Group();
MenuBar menuBar = new MenuBar();
Scene scene = new Scene(root, 500,500, Color.WHITE);
scene.getStylesheets().add(this.getClass().getResource("style.css")
.toExternalForm());
primaryStage.setTitle("Crude oil price predicition");
menuBar.prefWidthProperty().bind(primaryStage.widthProperty());
Menu file = new Menu("File2");
MenuItem upload = new MenuItem("Upload");
upload.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Resource File");
fileChooser.showOpenDialog(primaryStage);
}
});
MenuItem exit = new MenuItem("Exit");
exit.setMnemonicParsing(true);
exit.setAccelerator(new KeyCodeCombination(KeyCode.X,KeyCombination.CONTROL_DOWN));
exit.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
Platform.exit();
}
});
Menu test = new Menu("Test");
MenuItem arima = new MenuItem ("ARIMA");
arima.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
}
});
MenuItem acf = new MenuItem ("ACF");
acf.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
}
});
MenuItem pacf = new MenuItem ("PACF");
pacf.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
}
});
MenuItem hw = new MenuItem ("HoltWinters");
hw.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
}
});
Menu help = new Menu("Help");
MenuItem arimaH = new MenuItem ("ARIMA");
arimaH.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
fr.md.pack.TextClass.arimaText.setFill(Color.FIREBRICK);
fr.md.pack.TextClass.arimaText.setText(fr.md.pack.StringClass.aboutArima);
grid.add(fr.md.pack.TextClass.arimaText, 1, 10);
}
});
MenuItem acfH = new MenuItem("ACF");
acfH.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent event){
fr.md.pack.TextClass.acfText.setFill(Color.FIREBRICK);
fr.md.pack.TextClass.acfText.setText(fr.md.pack.StringClass.aboutACF);
grid.add(fr.md.pack.TextClass.acfText,1,10);
}
});
MenuItem pacfH = new MenuItem ("PACF");
pacfH.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
fr.md.pack.TextClass.pacfText.setFill(Color.FIREBRICK);
fr.md.pack.TextClass.pacfText.setText(fr.md.pack.StringClass.aboutPACF);
grid.add(fr.md.pack.TextClass.pacfText, 1, 10);
}
});
MenuItem hwH = new MenuItem ("HoltWinters");
hwH.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
fr.md.pack.TextClass.hwText.setFill(Color.FIREBRICK);
fr.md.pack.TextClass.hwText.setText(fr.md.pack.StringClass.aboutHW);
grid.add(fr.md.pack.TextClass.hwText, 1, 10);
}
});
Menu about = new Menu("About");
MenuItem thisS = new MenuItem ("About this software");
thisS.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
fr.md.pack.TextClass.textRef.setFill(Color.FIREBRICK);
fr.md.pack.TextClass.aboutSoft.setText(fr.md.pack.StringClass.aboutSofttext);
grid.add(fr.md.pack.TextClass.aboutSoft, 1, 40);
}
});
MenuItem R = new MenuItem ("About R");
R.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
fr.md.pack.TextClass.textRef.setFill(Color.FIREBRICK);
fr.md.pack.TextClass.aboutR.setText(fr.md.pack.StringClass.aboutRtext);
grid.add(fr.md.pack.TextClass.aboutR, 1, 40);
}
});
MenuItem RServe = new MenuItem ("About RServe");
RServe.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
fr.md.pack.TextClass.textRef.setFill(Color.FIREBRICK);
fr.md.pack.TextClass.textRef.setText(fr.md.pack.StringClass.aboutText);
grid.add(fr.md.pack.TextClass.textRef, 1, 40);
}
});
file.getItems().addAll(upload,exit);
test.getItems().addAll(arima,acf,pacf,hw);
help.getItems().addAll(arimaH,acfH,pacfH,hwH);
about.getItems().addAll(thisS, R, RServe);
menuBar.getMenus().addAll(file,test,help,about);
root.getChildren().addAll(menuBar,grid);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
The classes used are just containing the strings to display:
package fr.md.pack;
public class StringClass {
final static String aboutText = "Малко информация за RServe.";
final static String aboutRtext = "Малко инфо за Р";
final static String aboutSofttext = "Малко инфо за софта";
final static String aboutArima = "Малко инфо за ARIMA";
final static String aboutACF = "Малко инфо за ACF";
final static String aboutPACF = "Малко инфо за PACF";
final static String aboutHW = "Малко инфо за Holt Winters";
}
Well, I just installed Java 8 and surprise....everything is working just fine. I used till now Java 1.7 update 45 and it was locking my application as soon as I try to use the Action event. With Java 8 it's working but not without some bugs....

Using FXML to Create ContextMenu within a Pane

I've got a working example for defining a ContextMenu on a Pane in JavaFX FXML, but am not sure it is optimal. Currently, only JavaFX standard controls (e.g. Button, TextField) define a property for specifying a popup ContextMenu. Yet I wanted to have a popup menu appear anywhere in a Pane, in my case a VBox.
I took the approach of extending VBox to support a context menu. It is a 'clunky' solution but works. Is there a better approach? Am I missing some fundamental concept?
Here is my solution...
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import custommenu.view.ContextMenuPane?>
<AnchorPane xmlns:fx="http://javafx.com/fxml" fx:controller="custommenu.controller.CustomMenuController">
<children>
<VBox fx:id="vbox" onContextMenuRequested="#showMenu"
onMousePressed="#hideMenu" prefHeight="200" prefWidth="200">
</VBox>
<ContextMenuPane>
<contextMenu>
<ContextMenu fx:id="menu">
<items>
<MenuItem text="add" onAction="#add" />
</items>
</ContextMenu>
</contextMenu>
</ContextMenuPane>
</children>
</AnchorPane>
CustomMenuPane...
package custommenu.view;
import javafx.scene.control.ContextMenu;
import javafx.scene.layout.Pane;
public class ContextMenuPane extends Pane {
private ContextMenu contextMenu;
public void setContextMenu(ContextMenu contextMenu) {
this.contextMenu = contextMenu;
}
public ContextMenu getContextMenu() {
return contextMenu;
}
}
Controller...
package custommenu.controller;
import javafx.fxml.FXML;
import javafx.scene.control.ContextMenu;
import javafx.scene.input.ContextMenuEvent;
import javafx.scene.layout.VBox;
public class CustomMenuController {
#FXML private VBox vbox;
#FXML private ContextMenu menu;
#FXML public void add() {
System.out.println("add");
}
#FXML
public void showMenu(ContextMenuEvent event) {
System.out.println("showMenu");
menu.show(vbox, event.getScreenX(), event.getScreenY());
event.consume();
}
#FXML public void hideMenu() {
menu.hide();
}
}
Main App...
package custommenu;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class CustomMenuApplication extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
Pane myPane = (Pane)FXMLLoader.load(getClass().getResource("/custommenu/custom_menu_main.fxml"));
Scene scene = new Scene(myPane);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Tried to do it within a Popup? Catch the MouseEvent check for MouseButton.SECONDARY and display your VBox in the Popup.
private void showContextMenu() // showContextMenu(MouseEvent event)
{
Popup popup = new Popup();
VBox content = new VBox();
Button b = new Button("Click Me!");
b.setOnAction(new EventHandler<ActionEvent>() { });
content.getChildren().addAll(b);
popup.getContent().add(content);
popup.setX(0); // or get mouse event x and y
popup.setY(0); // event.getY()
popup.setAutoHide(true);
popup.show(your popup owner);
}
In This example there is button which has context menu when click on left/right it will open the popup when you click on other area except button it will hide the popup context menu
public class Main extends Application{
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) {
Scene scene = new Scene(new Group(), 450, 250);
Button notification = new Button();
MenuItem item1 = new MenuItem("About");
item1.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
System.out.println("About");
}
});
MenuItem item2 = new MenuItem("Preferences");
item2.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
System.out.println("Preferences");
}
});
MenuItem item3 = new MenuItem("About");
item1.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
System.out.println("About");
}
});
MenuItem item4 = new MenuItem("Preferences");
item2.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
System.out.println("Preferences");
}
});
MenuItem item5 = new MenuItem("About");
item1.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
System.out.println("About");
}
});
MenuItem item6 = new MenuItem("Preferences");
item2.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
System.out.println("Preferences");
}
});
MenuItem item7 = new MenuItem("About");
item1.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
System.out.println("About");
}
});
MenuItem item8 = new MenuItem("Preferences");
item2.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
System.out.println("Preferences");
}
});
final ContextMenu contextMenu = new ContextMenu(item1, item2,item3, item4,item5, item6,item7, item8);
contextMenu.setMaxSize(50, 50);
contextMenu.setOnShowing(new EventHandler<WindowEvent>() {
public void handle(WindowEvent e) {
System.out.println("showing");
}
});
contextMenu.setOnShown(new EventHandler<WindowEvent>() {
public void handle(WindowEvent e) {
System.out.println("shown");
}
});
// contextMenu.hide();
notification.setContextMenu(contextMenu);
GridPane grid = new GridPane();
grid.setVgap(4);
grid.setHgap(10);
grid.setPadding(new Insets(5, 5, 5, 5));
grid.add(new Label("To: "), 0, 0);
grid.add(notification, 1, 0);
Group root = (Group) scene.getRoot();
root.getChildren().add(grid);
stage.setScene(scene);
stage.show();
notification.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent me)->{
if(me.getButton()==MouseButton.PRIMARY ){
System.out.println("Mouse Left Pressed");
System.out.println(notification.getScaleX());
System.out.println(notification.getScaleY());
System.out.println(me.getScreenX());
System.out.println(me.getScreenY());
contextMenu.show(notification,me.getScreenX(),me.getScreenY());
}else{
contextMenu.hide();
}
});
}
}

ContextMenu (Popup) always on

I have a problem with a Context menu in JavaFx 2:it never disappers when I left click on the graph of the JFXPanel
Does anybody knows how to solve this problem?
Thanks
Here is my code
final ContextMenu cm = new ContextMenu();
MenuItem chartItem1 = new MenuItem("Chart Settings");
cm.getItems().add(chartItem1);
getScene().setOnMouseReleased(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
if(cm.isShowing()){
cm.hide();
}
if(mouseEvent.getButton() == MouseButton.SECONDARY)
{
cm.show(getScene().getRoot(), mouseEvent.getScreenX(), mouseEvent.getScreenY());
}
}
});
chartItem1.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent t) {
dialogs.ChartFormat cs = new dialogs.ChartFormat(null, true);
cs.setLocationRelativeTo(null);
cs.setVisible(true);
}
});
Reproduced the described behavior. Don't know the reason but you can use ContextMenu#hide():
final ContextMenu cm = new ContextMenu();
MenuItem menuItem = new MenuItem("Item 1");
menuItem.addEventHandler(EventType.ROOT, new EventHandler<Event>() {
#Override
public void handle(Event t) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JPanel messagePane = new JPanel();
messagePane.add(new JLabel("label"));
JDialog jDialog = new JDialog();
jDialog.getContentPane().add(messagePane);
jDialog.pack();
jDialog.setVisible(true);
}
});
}
});
cm.getItems().add(menuItem);
scene.setOnMouseReleased(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
// if(cm.isShowing())
cm.hide();
if (mouseEvent.getButton() == MouseButton.SECONDARY) {
cm.show(lineChart, mouseEvent.getScreenX(), mouseEvent.getScreenY());
}
}
});
Also you can check out these links:
http://pixelduke.wordpress.com/2011/12/11/popupmenu-in-javafx/
http://javafx-jira.kenai.com/browse/RT-17853
http://javafx-jira.kenai.com/browse/RT-14899
Adding sample code to your question would be more descriptive.

Resources