JFrame and JPanel problems with calling between - jframe

package pozivanjeProzora;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
public class MainPanel extends JPanel {
SidePanel panel ;
MainWindow instanca;
public MainPanel()
{
super();
JButton button = new JButton("Pozdrav iz main panela");
add(button);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
instanca.add(panel);
}
});
}
}
So, I extend JFrame in my MainWindow and I created instance of it. I am in MainPanel class which is called in MainWindow right now. But if I come in MainPanel and if I click on this button I want to my MainWindow(which extends JFrame) to add panel from SidePanel class(which has one button and extends JPanel also).
But if I click on button it shows me only NullPointerException. Where is my foult? MainWindow only has couple of lines about location of window and visible and I added MainPanel there as a first panel when I run my program. But, when I call another panel from MainPanel and when I try to add that panel with instance of MainWindow it shows error.

Your variable instanca is never initialised. Perhaps pass it in the constructor.
i.e.
public MainPanel(MainWindow mainWindow)
{
instanca = mainWindow;

Related

JPanel subclass won't appear in JFrame

I have this class PokerPanel which extends JPanel, whatever I put in the JPanel subclass will not appear in the JFrame when it is set as the Content Pane. Below is the code
public class PokerPanel extends JPanel {
private int x = 1000;
private int y = 1000;
public PokerPanel(){
this.setPreferredSize(new Dimension(x, y));
this.setBackground(Color.BLACK);
}
Then in my main class
public static void main(String[] args) {
PokerPanel pp = new PokerPanel();
pp.setOpaque(true);
JFrame frame = new JFrame();
frame.setSize(pp.getX(), pp.getY());
frame.setContentPane(pp);
frame.setVisible(true);
}
This does not show the black background as intended (or anything else i put in there for that matter JLabels etc), however if I do it as shown below then it does work.
public static void main(String[] args) {
PokerPanel pp = new PokerPanel(rg);
pp.setOpaque(true);
JPanel panel = new JPanel();
panel.setBackground(Color.BLACK);
JFrame frame = new JFrame();
frame.setSize(pp.getX(), pp.getY());
frame.setContentPane(panel);
frame.setVisible(true);
}
I can't seem to figure out why it would not show the JPanel when it is a subclass of JPanel any help would be appreciated

JavaFX: How to update the center view of a borderpane with new values

I am creating a program which has a row of buttons on the top, and a row of buttons on the side.
The top buttons control the view, and the side buttons control what object to reference in the view.
My main/root view is a borderpane.
The point is to, as I click on any of these buttons, to change a value in my MainController, and then reload the center view with these new values. I thought it would be so simple as to write a method that would change the value and then set a new center according to these values.
However, as I test it, it can display the two values I have already asked it to display, but gives me a huge load of red error code whenever I run.
My MainViewController looks as follows:
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class MainViewController extends Application {
#FXML
private Button cabin1;
#FXML
private Button cabin2;
#FXML
private Button tab1;
#FXML
private Button tab2;
#FXML
public void setCabinOne() throws IOException {
cabinIndex=1;
setCenterView();
}
#FXML
public void setCabinTwo() throws IOException {
cabinIndex=2;
setCenterView();
}
#FXML
public void setTabOne() throws IOException {
tabIndex=1;
setCenterView();
}
#FXML
public void setTabTwo() throws IOException {
tabIndex=2;
setCenterView();
}
public int getCabinIndex() {
return cabinIndex;
}
public int getTabIndex() {
return tabIndex;
}
private int tabIndex=0;
private int cabinIndex=1;
public Stage primaryStage;
private BorderPane mainPane;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
this.primaryStage=primaryStage;
primaryStage.setTitle("Test");
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainViewController.class.getResource("MainView.fxml"));
mainPane = loader.load();
setCenterView();
Scene scene = new Scene(mainPane);
primaryStage.setScene(scene);
primaryStage.show();
}
public void setCenterView() throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainViewController.class.getResource("TestView.fxml"));
AnchorPane testPane = loader.load();
TestViewController tvc = loader.<TestViewController>getController();
tvc.changeLabel(getCabinIndex());
tvc.changeIndex(getTabIndex());
mainPane.setCenter(testPane);
}
}
and my TestViewController looks as follows:
import javafx.fxml.FXML;
import javafx.scene.control.Label;
public class TestViewController {
#FXML
private Label cabinIndex;
#FXML
private Label tabIndex;
public void initialise() {
this.changeLabel(0);
this.changeIndex(0);
}
public void changeLabel(int n) {
cabinIndex.setText("Cabin "+Integer.toString(n));
}
public void changeIndex(int n) {
tabIndex.setText("Tab "+Integer.toString(n));
}
}
What am I doing wrong?
Your application is basically structurally wrong: you are using the application subclass as the controller, and this simply won't work (at least, not easily). You need to refactor this with a startup class (subclass of Application) that is distinct from the controller. Virtually any complete example will work as a template for you, but the Oracle tutorial is a good place to start.
What is happening is as follows:
When you call Application.launch() in MainViewController.main(...), the FX toolkit is started, an instance of your application class MainViewController is created, the FX Application Thread is started, and the start() method belonging to the MainViewController instance is invoked on the FX Application Thread.
When you call the FXMLLoader's load() method, it parses the FXML file at the specified location. When it sees the fx:controller attribute, which I'm assuming is fx:controller="MainViewController", it creates an instance of the specified controller class. Once the FXML is parsed, any matching #FXML-annotated fields (belonging to that instance) are initialized with the corresponding objects from the FXML file, and then the initialize() method is called on that instance.
So notice now you actually have two instances of MainViewController: the one from which FXMLLoader.load() was called, and the one created by the FXMLLoader. The #FXML-annotated fields in the instance created by the FXMLLoader are initialized, but the ones in the original instance remain set to the default value of null. Conversely, the mainPane field is initialized in the start method in the original MainViewController instance, but is never initialized in the instance created by the FXMLLoader. So when you press the button and invoke setCenterView() (I assume this is how your FXML is set up), you end up calling mainPane.setCenter() when mainPane is still null.
You could probably just about force it to work like this. Something like: remove the fx:controller attribute from MainView.fxml, and call loader.setController(this). But when you stray that far from the usual patterns used by a framework, your code becomes hard to maintain and debug. I would recommend following the design intended by the API.

Trying to create simple GEF

i am trying to create simple automation tool for testing.I have followed a simple tutorials
on net and created a RCP with view on eclipse. now i have tried to include simple GEF
component on the view it throws me error saying " Could not create the view: Plug-in "GEFTutorial" was unable to instantiate class "geftutorial.View"."
here is my source code
particularly when i uncomment creation of
private ScrollingGraphicalViewer viewer = new ScrollingGraphicalViewer();
private RootEditPart rootEditPart = new ScalableFreeformRootEditPart();
private EditPartFactory editPartFactory = new SimpleGEFEditPartFactory();
all the above statements on the view.my view appears back
here is my source code for view.java
package geftutorial;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.gef.*;
import org.eclipse.gef.editparts.ScalableFreeformRootEditPart;
import org.eclipse.gef.ui.parts.ScrollingGraphicalViewer;
public class View extends ViewPart {
public static final String ID = "GEFTutorial.view";
//Use a standard Viewer for the Draw2d canvas
private ScrollingGraphicalViewer viewer = new ScrollingGraphicalViewer();
//Use standard RootEditPart as holder for all other edit parts
private RootEditPart rootEditPart = new ScalableFreeformRootEditPart();
//Custom made EditPartFactory, will automatically be called to create
//edit
// parts for model elements
private EditPartFactory editPartFactory = new SimpleGEFEditPartFactory();
//The model
private SuperWidget model;
//private TableViewer viewer;
/**
* The content provider class is responsible for providing objects to the
* view. It can wrap existing objects in adapters or simply return objects
* as-is. These objects may be sensitive to the current input of the view,
* or ignore it and always show the same content (like Task List, for
* example).
*/
class ViewContentProvider implements IStructuredContentProvider {
public void inputChanged(Viewer v, Object oldInput, Object newInput) {
}
public void dispose() {
}
public Object[] getElements(Object parent) {
if (parent instanceof Object[]) {
return (Object[]) parent;
}
return new Object[0];
}
}
class ViewLabelProvider extends LabelProvider implements
ITableLabelProvider {
public String getColumnText(Object obj, int index) {
return getText(obj);
}
public Image getColumnImage(Object obj, int index) {
return getImage(obj);
}
public Image getImage(Object obj) {
return PlatformUI.getWorkbench().getSharedImages().getImage(
ISharedImages.IMG_OBJ_ELEMENT);
}
}
/**
* This is a callback that will allow us to create the viewer and initialize
* it.
*/
public void createPartControl(Composite parent) {
/*viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
| SWT.V_SCROLL);
viewer.setContentProvider(new ViewContentProvider());
viewer.setLabelProvider(new ViewLabelProvider());
// Provide the input to the ContentProvider
viewer.setInput(new String[] {"One", "Two", "Three"});
*/
//Create a dummy model
model = new SuperWidget("Model");
model.createDummyModel();
//Initialize the viewer, 'parent' is the
// enclosing RCP windowframe
viewer.createControl(parent);
viewer.setRootEditPart(rootEditPart);
viewer.setEditPartFactory(editPartFactory);
//Inject the model into the viewer, the viewer will
// traverse the model automatically
viewer.setContents(model);
//Set the view's background to white
viewer.getControl().setBackground(new Color(null, 255,255,255));
}
/**
* Passing the focus request to the viewer's control.
*/
public void setFocus() {
viewer.getControl().setFocus();
}
}
Can someone give me a clue about this? i am new to RCP and GEF :(
I'm also just learning GEF, but from what I have seen gef editors are not ViewPart (views) but editors, extending EditPart.
Check my ongoing GEF tutorial here. Hope it helps.
You can also access other GEF tutorial from the eclipse website.

accessing array with object

hi i have two classes in android and in one class i have write an array and i want to access it in the main class but the error is give me that "force closed" here is my code
package com.semanticnotion.DAO;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class DAO extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WordsDAO DAO = new WordsDAO(new String[] "Arte","Arquitectura","Familia","Moda","Cotilleos","Cine","Libros","Historia","Pintura","Musica","Tendencies","Modernimso","Pop art","Masteialismo","realities","filosofia","moda","fotografia","religion"});
Button next = (Button) findViewById(R.id.Button01);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), WordsDAO.class);
startActivity(myIntent);
}
});
}
}
and the second class code is
package com.semanticnotion.DAO;
public class WordsDAO
{
String[] words = new String[] "Arte","Arquitectura","Familia","Moda","Cotilleos","Cine","Libros","Historia","Pintura","Musica","Tendencies","Modernimso","Pop art","Masteialismo","realities","filosofia","moda","fotografia","religion"};
public WordsDAO(String[] words )
{
this.words=words;
}
}
please any one tell what well be the error in this code thaks
First of all: the constructor in your second class would not be used. The way to pass parameters to another activity is to use Intent.putExtra in the code calling the other activity and in your other activity use
Bundle extras = getIntent().getExtras();
if(extras !=null)
{
String value = extras.getString("keyName");
}
to get the data in onCreate.
That said, I guess the problem arises from your second class not providing an explicit parameterless constructor.

How do I get notified whenever a new editor is opened in Eclipse?

I have a view which would like to be notified about all the currently opened editors. Where can I add a listener to achieve this?
I was expecting WorkbenchPage or EditorManager to have some appropriate listener registry, but I couldn't find it.
Does your view uses a org.eclipse.ui.IPartListener2 ?
That is what is using this EditorListener, whose job is to react, for a given view, to Editor events (including open and close)
public class EditorListener implements ISelectionListener, IFileBufferListener,
IPartListener2 {
protected BytecodeOutlineView view;
EditorListener(BytecodeOutlineView view){
this.view = view;
}
[...]
/**
* #see org.eclipse.ui.IPartListener2#partOpened(org.eclipse.ui.IWorkbenchPartReference)
*/
public void partOpened(IWorkbenchPartReference partRef) {
view.handlePartVisible(partRef.getPart(false));
}
Now if your ViewPart directly implements an IPartListener2, it can register itself to the various Editors, like this BytecodeReferenceView
public class BytecodeReferenceView extends ViewPart implements IPartListener2, ISelectionListener {
[...]
public void createPartControl(Composite parent) {
browser = new Browser(parent, SWT.BORDER);
browser.setText(BytecodeOutlinePlugin.getResourceString(NLS_PREFIX
+ "empty.selection.text"));
final IWorkbenchWindow workbenchWindow = getSite().getWorkbenchWindow();
workbenchWindow.getPartService().addPartListener(this);
[...]
I think you're on the right track. You need to listen to the IWorkbenchPage IPartService events:
page.addPartListener(new IPartListener() {
partOpened(IWorkbenchPart part) {
...
}
...
});