am having a problem trying to access this keyword in a different class using Java programming. I have tried Context, class.this but no help yet...
I have created a project using NetBeans gui builder, I want when i click button the form to get disposed...
Main class contains the click event for disposing the JFrame Form
BestQSystems.java:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
CloseWindow.closeWindow();
}
Class to close the JFrame: CloseWindow.java
import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import javax.naming.Context;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* #author Benson
*/
public class CloseWindow {
public static void closeWindow(){
WindowEvent widnowEvent = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(widnowEvent);
}
}
Am having an error in this line WindowEvent widnowEvent = new WindowEvent(this, WindowEvent.WINDOW_CLOSING); Please advise me on how to access this keyword in a different class.
You can pass a reference to this to the other method. For example:
BestQSystems.java
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
CloseWindow.closeWindow(this);
}
and in the CloseWindow.java
public class CloseWindow {
public static void closeWindow(BestQSystems ref){
WindowEvent widnowEvent = new WindowEvent(ref, WindowEvent.WINDOW_CLOSING);
}
}
Related
I cannot figure out why the following command does not work when the program calls the command. I dont have a very good background in Java, but as I understand it, the command should execute the print line statement when i press the button on the joystick that calls the command. Im not sure whether the problem may be that the command needs a Action listener or button listener somewhere, or whether I need to somehow relate the command to the same console. There should only be one recognized console it can print to, which i know works from other print line statements in the original program... right?
Here is the library which may help
THE PROGRAM:
/*--------------------------------------------------------------------------
--*/
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved.
*/
/* Open Source Software - may be modified and shared by FRC teams. The code
*/
/* must be accompanied by the FIRST BSD license file in the root directory
of */
/* the project.
*/
/*--------------------------------------------------------------------------
--*/
package org.usfirst.frc.team5621.robot;
import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Spark;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.buttons.Button;
import edu.wpi.first.wpilibj.buttons.JoystickButton;
import edu.wpi.first.wpilibj.command.Command;
import edu.wpi.first.wpilibj.command.Subsystem;
import edu.wpi.first.wpilibj.drive.DifferentialDrive;
/**
* The VM is configured to automatically run this class, and to call the
* functions corresponding to each mode, as described in the IterativeRobot
* documentation. If you change the name of this class or the package after
* creating this project, you must also update the manifest file in the resource
* directory.
*/
public class Robot extends IterativeRobot {
private DifferentialDrive m_robotDrive
= new DifferentialDrive(new Spark(0), new Spark(1));
static Joystick m_stick = new Joystick(1);
private Timer m_timer = new Timer();
static Subsystem ExampleSubsystem;
Command ExampleCommand;
Command CompressCommand;
Command DecompressCommand;
Command OpenClawCommand;
Command CloseClawCommand;
Command CompressorToggleCommand;
public static class OI {
//Create Joystick and Buttons
static Joystick m_stick = new Joystick(1);
static Button button1 = new JoystickButton(m_stick, 1);
static Button button2 = new JoystickButton(m_stick, 2);
static Button button3 = new JoystickButton(m_stick, 3);
static Button button4 = new JoystickButton(m_stick, 4);
static Button button5 = new JoystickButton(m_stick, 5);
static Button button6 = new JoystickButton(m_stick, 6);
static Button button7 = new JoystickButton(m_stick, 7);
static Button button8 = new JoystickButton(m_stick, 8);
public OI() {
// Define Commands for Joystick Buttons
OI.button1.whileHeld(new CompressorToggleCommand());
OI.button2.whileHeld(new CompressCommand());
OI.button3.whileHeld(new DecompressCommand());
OI.button4.whileHeld(new OpenClawCommand());
OI.button5.whileHeld(new CloseClawCommand());
OI.button6.whileHeld(new ExampleCommand());
OI.button7.whileHeld(new ExampleCommand());
OI.button8.whileHeld(new ExampleCommand());
}
}
public class Compressor {
Compressor c = new Compressor();
}
public class Solenoid {
Solenoid exampleSolenoid = new Solenoid();
}
/**
* This function is run when the robot is first started up and should be
* used for any initialization code.
*/
#Override
public void robotInit() {
}
/**
* This function is run once each time the robot enters autonomous mode.
*/
#Override
public void autonomousInit() {
m_timer.reset();
m_timer.start();
}
/**
* This function is called periodically during autonomous.
*/
#Override
public void autonomousPeriodic() {
// Drive for 2 seconds
if (m_timer.get() < 2.0) {
m_robotDrive.arcadeDrive(0.5, 0.0); // drive forwards half speed
} else {
m_robotDrive.stopMotor(); // stop robot
}
}
/**
* This function is called once each time the robot enters teleoperated mode.
*/
#Override
public void teleopInit() {
System.out.println("TeleOperated Mode Enabled");
}
/**
* This function is called periodically during teleoperated mode.
*/
#Override
public void teleopPeriodic() {
m_robotDrive.arcadeDrive(m_stick.getY(), m_stick.getX());
}
/**
* This function is called periodically during test mode.
*/
#Override
public void testPeriodic() {
}
}
THE COMMAND
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package org.usfirst.frc.team5621.robot;
import edu.wpi.first.wpilibj.command.Command;
/**
* An example command. You can replace me with your own command.
*/
public class CompressCommand extends Command {
public CompressCommand() {
}
// Called just before this Command runs the first time
#Override
protected void initialize() {
}
// Called repeatedly when this Command is scheduled to run
#Override
protected void execute() {
System.out.println("Compressing...");
exampleSolenoid.set(true);
}
// Make this return true when this Command no longer needs to run execute()
#Override
protected boolean isFinished() {
return false;
}
}
First off, I'd like to say that it's nice to see a fellow FRC programmer on Stack Overflow. Pertaining to your question, your problem may lie in the fact that you are declaring multiple public classes in the same file. This is very bad practice in Java programming. The problem you are looking to solve is here:
public OI() {
// Define Commands for Joystick Buttons
OI.button1.whileHeld(new CompressorToggleCommand());
OI.button2.whileHeld(new CompressCommand());
OI.button3.whileHeld(new DecompressCommand());
OI.button4.whileHeld(new OpenClawCommand());
OI.button5.whileHeld(new CloseClawCommand());
OI.button6.whileHeld(new ExampleCommand());
OI.button7.whileHeld(new ExampleCommand());
OI.button8.whileHeld(new ExampleCommand());
}
Once again, it is bad practice to have nested public classes, but what is really causing your problem is that you set the Button-On-Press actions for each button in the constructor of your OI class, but you never actually make an OI object, therefore the constructor is never called, and the code is never run. In your robotInit function, make an new OI object:
void robotInit() {
OI myOI = new OI();
}
Also remove the OI tag from your statements inside of the OI class. It is not needed. So that this:
OI.button1.whileHeld(new CompressorToggleCommand());
Becomes this:
button1.whileHeld(new CompressorToggleCommand());
I would also recommend moving all of your object initializations to robotInit. I would suggest making all of your many nested classes each in their own file, and be sure to make objects of all of these classes in your main Robot class. I would also suggest reading up on some basic Object Oriented Programming concepts such as how constructors work, as well as how to use classes and objects in Java. Also maybe read up on the FRC functions you are using in the FRC API Reference
I have a Java Application, I am wanting to format a button as either Active or Inactive (Also possibly a hover method).
The code as I would like to implement it:
//Home Tab - Active by default
home = new TabButton();
home.setSize(new Dimension(tabWidth, tabHeight));
home.setFont(getLauncherFont(34));
home.setForeground(Color.white);
home.setText("HOME");
home.setBounds(160, 0, tabWidth, tabHeight);
home.setActive(); --> This Method is what I would like to create
I already have a class to create a JButton for the tab:
package com.anarcist.minemodloaderv1.skin.components;
import java.awt.Color;
import javax.swing.JButton;
/**
*
* #author anarcist
*/
public class TabButton extends JButton {
public TabButton() {
this.setBorderPainted(false);
this.setFocusPainted(false);
this.setContentAreaFilled(true);
this.setBackground(Color.blue);
}
}
I have researched abstract classes. But my TabButton class already extends JButton.
I would like a method like this:
public void setActive(){
this.setBackground(Color.red);
//Any other changes a want to make regularly
}
That can simply be implemented like this home.setActive();
My Question I suppose is: Is it easy enough to implement what I am looking for, or will I have to got the long way and set all attributes manually every time?
What you've described in the post can be done like this:
package com.anarcist.minemodloaderv1.skin.components;
import java.awt.Color;
import javax.swing.JButton;
/**
*
* #author anarcist
*/
public class TabButton extends JButton {
public TabButton() {// initialize
this.setBorderPainted(false);
this.setFocusPainted(false);
this.setContentAreaFilled(true);
this.setBackground(Color.blue);
}
// add your own methods or override JButton methods
public void setActive(){
//Add code
//example: setEnabled(true);
}
}
I'm writing an eclipse plugin for a domain specific language. I've subclassed editor and things are working fine.
I'd like to add a popup menu so that I can do my own refactorings. I've used org.eclipse.ui.popupmenus to add the popupmenu, which when you right click on the editor, does provide the menu, it can be clicked, and the click executes the following method:
public void run(IAction action) {
MessageDialog.openInformation(shell, "SyntaxColoringCSharp", "refactorA was executed.");
}
My problem is that I don't know where in the editor the click happened - so it's difficult for me to get the right bit of the file to perform the refactoring on. The cursor doesn't move to where the click happened.
How do I find out where in the editor the popupmenu click happened? (i.e. how do the Java refactoring get this information?)
Here is the basic class I'm working with:
package arteditor.popup.actions;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.editors.text.TextEditor;
import editoractions.EditorActions;
public class refactorA implements IObjectActionDelegate {
private Shell shell;
/**
* Constructor for Action1.
*/
public refactorA() {
super();
}
/**
* #see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
*/
public void setActivePart(IAction action, IWorkbenchPart targetPart) {
shell = targetPart.getSite().getShell();
}
/**
* #see IActionDelegate#run(IAction)
*/
public void run(IAction action) {
MessageDialog.openInformation(shell, "SyntaxColoringCSharp", "refactorA was executed.");
}
/**
* #see IActionDelegate#selectionChanged(IAction, ISelection)
*/
public void selectionChanged(IAction action, ISelection selection) {
}
}
It is normal to use the cursor (caret) position for the right click action - that is what the Java refactoring does in the Java editor context menu.
The StyledText widget has various methods to return the caret position.
What I don't like about my code below is:
getters are needed for every JButton on each page
the actionPerformed method can quickly become bloated with if-else statements
So, is there a better way to control all GUI actions from a single class?
If I define an actionPerformed method within each respective page (JPanel), each page will need access to instances of the page(s) switched to, and I am trying to avoid using the Singleton pattern for each page...
Here is the code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
*
* #author Ian A. Campbell
*
*/
public class Controller implements ActionListener {
/**
* instance variables:
*/
private Frame frame;
private OptionPage firstPage;
private FirstOptionPage firstOption;
private SecondOptionPage secondOption;
/**
*
*/
public Controller() {
// instantiating the frame here:
this.frame = new Frame();
/*
* instantiating all pages here:
*
* NOTE: passing "this" because this class
* handles the events from these pages
*/
this.firstPage = new OptionPage(this);
this.firstOption = new FirstOptionPage(this);
this.secondOption = new SecondOptionPage(this);
}
/**
*
*/
public void start() {
this.frame.add(this.firstPage); // adding the first page
// NOTE: these lines prevent blank loading and flickering pages!
this.frame.validate();
this.frame.repaint();
this.frame.setVisible(true);
}
/**
*
* #return the JFrame instantiated from the class Frame
*/
public Frame getFrame() {
return this.frame;
}
#Override
public void actionPerformed(ActionEvent e) {
// the "first option" button from the OptionPage:
if (e.getSource() == this.firstPage.getFirstButton()) {
this.frame.getContentPane().removeAll();
this.frame.getContentPane().add(this.firstOption);
// the "second option" button from the OptionPage:
} else if (e.getSource() == this.firstPage.getSecondButton()) {
this.frame.getContentPane().removeAll();
this.frame.getContentPane().add(this.secondOption);
}
// NOTE: these lines prevent blank loading and flickering pages!
this.frame.validate();
this.frame.repaint();
this.frame.setVisible(true);
}
} // end of Controller
Use a Card Layout. Card Layout Actions adds some extra features that you might find helpful.
You could use card layout, or you could get creative and remove elements. For instance:
panel.remove((JButton)myButton1)); // Remove all of the elements...
panel.add((JButton)myButton2)); // Add the new elements
Of course I wouldn't deal with the java built in GUI at all, IMO the layout designs are horrific. I would much rather use something like "A New Look and Feel" -- http://www.javootoo.com/.
Can someone convert this into Clojure, I don't know to do the line setMainWindow(argument) like things....
import com.vaadin.Application;
class something {
public void init() {
Window main = new Window("The Main Window");
setMainWindow(main);
addComponent(new WindowOpener("Window Opener", main));
}
}
Update:
package app;
import com.vaadin.Application;
import com.vaadin.ui.Button;
import com.vaadin.ui.Window;
/**
* The Application's "main" class
*/
#SuppressWarnings("serial")
public class MyVaadinApplication extends Application{
private Window window;
#Override
public void init(){
window = new Window("My Vaadin Application");
setMainWindow(window);
window.addComponent(new Button("Click Me"));
}
}
There is a "/lib/vaadin.jar" which contains all "com.vaadin.*" things.
I think setMainWindow(window); is from the extended class. I am not going to write that method.
Literal translation:
(defn init []
(let [main (Window. "The Main Window")]
(setMainWindow main)
(addComponent (WindowOpener. "Window Opener" main))))
Though it doesn't make much sense without the context.
See http://dev.vaadin.com/wiki/Articles/ClojureScripting. Also I would suggest http://www.odesk.com.