How to remove a simple body in JBox2D? - libgdx

Whenever I try to destroy a body in JBox2D it always give me a NullPointerException, and I have checked online and they show various ways of doing it 'safely' which I don't understand. I was wondering if anyone could type up some simple pseudo code to show how you would destroy a body in Box2D. I am also using the LibGDX API as well.
Here is my code that I use:
public void render(float delta) {
if(bodiesContainer.size() > 0)
{
for(Body body:bodiesContainer)
{
world.destroyBody(body);
}
}
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(delta);
stage.draw();
world.step(TIMESTEP, VELOCITYITERATIONS, POSITIONITERATIONS);
camera.update();
debugRenderer.render(world, camera.combined);
batch.begin();
batch.end();
}
public void show() {
// TODO Auto-generated method stub
batch = new SpriteBatch();
camera = new OrthographicCamera(Gdx.graphics.getWidth()/5, Gdx.graphics.getHeight()/5);
world = new World(new Vector2(0,0f), true);
debugRenderer = new Box2DDebugRenderer();
stage = new Stage();
particleCreator = new ParticleCreator(0, 0, 1);
particleCreator.destroyParticle(world, particleBody);
if (particleCreator == null)
{
System.out.println("true");
}
if (particleBody == null)
{
System.out.println("true particleBody");
}
if (particleBodyDef == null)
{
System.out.println("true particleBodyDef");
}
Gdx.input.setInputProcessor(new InputProcessor() {
#Override
public boolean touchUp(int arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean touchDragged(int arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean touchDown(int arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean scrolled(int arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean mouseMoved(int arg0, int arg1) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean keyUp(int arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean keyTyped(char arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean keyDown(int keycode) {
switch(keycode) {
case Keys.SPACE:
break;
}
return false;
}
});
}
public class ParticleCreator
{
//Default constructor
public ParticleCreator()
{
}
public ParticleCreator(World world, BodyDef bodyDef, Body body, float position_x, float position_y, float particle_radius)
{
bodyDef = new BodyDef();
bodyDef.type = BodyType.DynamicBody;
bodyDef.position.set(position_x, position_y);
CircleShape circleShape = new CircleShape();
circleShape.setRadius(particle_radius);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = circleShape;
fixtureDef.restitution = 0;
body = world.createBody(bodyDef);
body.createFixture(fixtureDef);
}
public void destroyParticle(World world, Body body)
{
world.destroyBody(body);
System.out.println("This Particle has been destroyed !!!");
}
}
The bodiesContainer is an ArrayList, and the particleBody is a JBox2D body i wish to destroy, but every time I press the space button it crashes and gives me a NullPointerException.

Related

Detect user input in render() or using InputProcessor in libgdx

I am new to libgdx and I wonder if you should use the render() method to get user input or if you should use the InputProcessor.
That depends on the use-case. Why do you need it and does that need to be done event driven or continuous?
For example, if you want to move a sprite on the screen while a certain key is pressed, then that's a continuous action:
#Override public void render() {
if (Gdx.input.isKeyPressed(Input.Keys.SPACE))
sprite.translateX(speed * Gdx.graphics.getDeltaTime());
...
}
However, if you for example want to change the color when the user presses a certain key, then that's an event:
#Override public void create() {
Gdx.input.setInputProcessor(new InputAdapter() {
#Override public boolean keyUp (int keycode) {
if (keycode == Input.Keys.SPACE)
sprite.setColor(Color.RED);
}
});
...
}
Note that polling is a convenience method built on top of events, it is very easy to that yourself. Like so:
private boolean moving;
#Override public void create() {
Gdx.input.setInputProcessor(new InputAdapter() {
#Override public boolean keyDown (int keycode) {
if (keycode == Input.Keys.SPACE)
moving = true;
}
#Override public boolean keyUp (int keycode) {
if (keycode == Input.Keys.SPACE)
moving = false;
}
});
...
}
#Override public void render() {
if (moving)
sprite.translateX(speed * Gdx.graphics.getDeltaTime());
...
}
This often allows you to write more clean and use-case specific code, like so:
private float speed;
#Override public void create() {
Gdx.input.setInputProcessor(new InputAdapter() {
#Override public boolean keyDown (int keycode) {
switch (keycode) {
case Input.Keys.LEFT: speed -= 10f; break;
case Input.Keys.RIGHT: speed += 10f; break;
}
}
#Override public boolean keyUp (int keycode) {
switch (keycode) {
case Input.Keys.LEFT: speed += 10f; break;
case Input.Keys.RIGHT: speed -= 10f; break;
}
}
});
...
}
#Override public void render() {
sprite.translateX(speed * Gdx.graphics.getDeltaTime());
...
}
With this in mind, it can in many cases be better to use event driven input handling. However, if you find yourself using a lot of boolean flags, then you might as well use the builtin input polling.
You should use InputProcessor for user input.
if you want to write anonymously then in show method you should write:-
Gdx.input.setInputProcessor(new InputProcessor() {
#Override
public boolean touchUp(int arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean touchDragged(int arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean touchDown(int arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean scrolled(int arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean mouseMoved(int arg0, int arg1) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean keyUp(int arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean keyTyped(char arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean keyDown(int arg0) {
// TODO Auto-generated method stub
return false;
}
});
or you can implement input processor and register instance in setInputProcessor.
Example:-
public class InputTest implements InputProcessor {
#Override
public boolean keyDown(int arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean keyTyped(char arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean keyUp(int arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean mouseMoved(int arg0, int arg1) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean scrolled(int arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean touchDown(int arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean touchDragged(int arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean touchUp(int arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
return false;
}
}
in show method of your screen class you should write:-
public void show() {
Gdx.input.setInputProcessor(new InputTest());
}
in my suggestion these two are the best way to take user input if you are not using stage.
Hope this will work for you.

Creating On Screen Controls Libgdx

I am working on libgdx.I am using this example https://github.com/oakes/libgdx-examples/tree/master/SuperKoalio/core/src/net/sekao/superkoalio to create a game.
I want to add controls on the screen, so that user can move left,right,up using the buttons.I used this "Controller" class https://github.com/BrentAureli/ControllerDemo/tree/master/core/src/com/brentaureli/overlaydemo
But I am not able to see any controls on the screen.Can any one please provide me solution to fix this.
public class MainScreen implements Screen {
Stage stage;
TiledMap map;
OrthogonalTiledMapRenderer renderer;
OrthographicCamera camera;
Koala koala;
Controller controller;
public void show() {
map = new TmxMapLoader().load("level1.tmx");
final float pixelsPerTile = 16;
renderer = new OrthogonalTiledMapRenderer(map, 1 / pixelsPerTile);
camera = new OrthographicCamera();
controller = new Controller();
stage = new Stage();
stage.getViewport().setCamera(camera);
koala = new Koala();
koala.layer = (TiledMapTileLayer) map.getLayers().get("walls");
koala.setPosition(20, 10);
stage.addActor(koala);
}
public void render(float delta) {
Gdx.gl.glClearColor(0.5f, 0.5f, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.position.x = koala.getX();
camera.update();
if(Gdx.app.getType() == Application.ApplicationType.Android)
controller.draw();
renderer.setView(camera);
renderer.render();
stage.act(delta);
stage.draw();
}
public void dispose() {
}
public void hide() {
}
public void pause() {
}
public void resize(int width, int height) {
camera.setToOrtho(false, 20 * width / height, 20);
controller.resize(width, height);
}
public void resume() {
}
}
Controller Class:
public class Controller {
Viewport viewport;
Stage stage;
boolean upPressed, downPressed, leftPressed, rightPressed;
OrthographicCamera cam;
public Controller(){
cam = new OrthographicCamera();
viewport = new FitViewport(800, 480, cam);
stage = new Stage(viewport);
stage.addListener(new InputListener(){
#Override
public boolean keyDown(InputEvent event, int keycode) {
switch(keycode){
case Input.Keys.UP:
upPressed = true;
break;
case Input.Keys.DOWN:
downPressed = true;
break;
case Input.Keys.LEFT:
leftPressed = true;
break;
case Input.Keys.RIGHT:
rightPressed = true;
break;
}
return true;
}
#Override
public boolean keyUp(InputEvent event, int keycode) {
switch(keycode){
case Input.Keys.UP:
upPressed = false;
break;
case Input.Keys.DOWN:
downPressed = false;
break;
case Input.Keys.LEFT:
leftPressed = false;
break;
case Input.Keys.RIGHT:
rightPressed = false;
break;
}
return true;
}
});
Gdx.input.setInputProcessor(stage);
Table table = new Table();
table.left().bottom();
Image upImg = new Image(new Texture("flatDark25.png"));
upImg.setSize(50, 50);
upImg.addListener(new InputListener() {
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
upPressed = true;
return true;
}
#Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
upPressed = false;
}
});
Image downImg = new Image(new Texture("flatDark26.png"));
downImg.setSize(50, 50);
downImg.addListener(new InputListener() {
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
downPressed = true;
return true;
}
#Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
downPressed = false;
}
});
Image rightImg = new Image(new Texture("flatDark24.png"));
rightImg.setSize(50, 50);
rightImg.addListener(new InputListener() {
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
rightPressed = true;
return true;
}
#Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
rightPressed = false;
}
});
Image leftImg = new Image(new Texture("flatDark23.png"));
leftImg.setSize(50, 50);
leftImg.addListener(new InputListener() {
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
leftPressed = true;
return true;
}
#Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
leftPressed = false;
}
});
table.add();
table.add(upImg).size(upImg.getWidth(), upImg.getHeight());
table.add();
table.row().pad(5, 5, 5, 5);
table.add(leftImg).size(leftImg.getWidth(), leftImg.getHeight());
table.add();
table.add(rightImg).size(rightImg.getWidth(), rightImg.getHeight());
table.row().padBottom(5);
table.add();
table.add(downImg).size(downImg.getWidth(), downImg.getHeight());
table.add();
stage.addActor(table);
}
public void draw(){
stage.draw();
}
public boolean isUpPressed() {
return upPressed;
}
public boolean isDownPressed() {
return downPressed;
}
public boolean isLeftPressed() {
return leftPressed;
}
public boolean isRightPressed() {
return rightPressed;
}
public void resize(int width, int height){
viewport.update(width, height);
}
}

hide admob banner ads

I would like to show banner admob exactly at the moment the user enter into the main game screen. I hide the banner in onCreate with setAlpha(0f) method, and show it in the first render loop with setAlpha(1f). The timing is a bit off. How do I make it more precise? I also wonder if setting alpha is acceptable method to hide admob adds?
//RENDER LOOP
if(showAd==false){
actionResolver.showBanner();
showAd=true;
}
//android launcher
protected AdView adView;
protected View gameView;
private InterstitialAd interstitialAd;
private Activity context;
#Override
public void showBanner() {
try {
runOnUiThread(new Runnable() {
public void run() {
adView.setAlpha(1f);
}
});
} catch (Exception e) {
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context=this;
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
// Do the stuff that initialize() would do for you
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
layout.setLayoutParams(params);
AdView admobView = createAdView();
layout.addView(admobView);
View gameView = createGameView(cfg);
layout.addView(gameView);
setContentView(layout);
startAdvertising(admobView);
interstitialAd = new InterstitialAd(this);
interstitialAd.setAdUnitId(AD_UNIT_ID_INTERSTITIAL);
interstitialAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
}
#Override
public void onAdClosed() {
super.onAdClosed();
loadInterstitial();
}
});
}
private AdView createAdView() { ////// HERE I SET ALPHA
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId(AD_UNIT_ID_BANNER);
adView.setId(123456); // this is an arbitrary id, allows for relative positioning in createGameView()
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
adView.setLayoutParams(params);
adView.setBackgroundColor(Color.BLACK);
adView.setAlpha(0f);
return adView;
}
private View createGameView(AndroidApplicationConfiguration cfg) {
gameView = initializeForView(new Cube(this,this), cfg);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
params.addRule(RelativeLayout.BELOW, adView.getId());
gameView.setLayoutParams(params);
return gameView;
}
private void startAdvertising(AdView adView) {
// AdRequest adRequest = new AdRequest.Builder().build();
// adView.loadAd(adRequest);
AdRequest.Builder builder = new AdRequest.Builder();
// builder.addTestDevice("3F39DA9CF3F587314F9C4C0D88639C28");
adView.loadAd(builder.build());
}
#Override
public void showInterstitial() {
try {
runOnUiThread(new Runnable() {
public void run() {
if (interstitialAd.isLoaded()) {
interstitialAd.show();
}
}
});
} catch (Exception e) {
}
}
#Override
public void loadInterstitial() {
try {
runOnUiThread(new Runnable() {
public void run() {
if(interstitialAd.isLoaded()){
}else{
AdRequest interstitialRequest = new AdRequest.Builder().build();
interstitialAd.loadAd(interstitialRequest);
}
}
});
} catch (Exception e) {
}
}
#Override
public void onResume() {
super.onResume();
if (adView != null) adView.resume();
}
#Override
public void onPause() {
if (adView != null) adView.pause();
super.onPause();
}
#Override
public void onDestroy() {
if (adView != null) adView.destroy();
super.onDestroy();
}
#TargetApi(19)
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}
}

Refresh Json in Fragment1 each 60s and update the data in Fragment2

Hi everybody!!
I have two fragments:Fragment1 and Fragment2; Fragment1 contains Json,and after getting Json, I replace Fragment1 to Fragment2 via .replace(..) and transfer the Json via Bundle.
My Goal is: Refresh Json in Fragment1 each 60s for example and update automatically Fragment2 but i don't know how to do that!! i need your help!!
this is my code:
Class Fragment1
public class Fragment1 extends Fragment implements OnClickListener{
public static final String IMAGE_RESOURCE_ID="iconResourceID";
public static final String ITEM_NAME="itemName";
Button btnvalider;
//test transfer variable entre fragment
public Communicator com;
public void setCom(Communicator com) {
this.com = com;
}
/*
* Test Jsonparser
*
*/
Context c;
private ProgressDialog pDialog;
public String testfinalewa="";
JSonParser jsonParser = new JSonParser();
// url to create new product
private static String url_create_product = "http://10.0.2.2/webservice/create_personne.php";
public String getTestfinalewa() {
return testfinalewa;
}
public void setTestfinalewa(String testfinalewa) {
this.testfinalewa = testfinalewa;
}
// JSON Node names
private static final String TAG_SUCCESS = "success";
//fin test json
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
c=getActivity();
return inflater.inflate(R.layout.fragment_1, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
btnvalider=(Button)getActivity().findViewById(R.id.button1);
btnvalider.setOnClickListener(this);
this.com=(Communicator) getActivity();
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
new Handler().postDelayed(new Runnable() {
public void run() {
// call JSON methods here
new AttemptLogin().execute();
}
}, 1 );
}
//interface pour transferer variable entre fragment
public interface Communicator{
public void respond(String data);
}
class AttemptLogin extends AsyncTask<String, String, String>{//<params,progress,result>
boolean failure = false;
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(c);
pDialog.setMessage("Chargement...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
#Override
protected String doInBackground(String... args) {
String name ="test";// pseudo.getText().toString();
String moddepasse = "test";//mdp.getText().toString();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", name));
Log.i("misy ve", "ok="+moddepasse);
Log.i("misy ve", "ok="+name);
params.add(new BasicNameValuePair("moddepasse", moddepasse));
Log.i("test", "mbola tena mety eto 1");
// getting JSON Object
JSONObject json = jsonParser.makeHttpRequest(url_create_product,
"POST", params);
Log.d("Create Response", json.toString());
Log.i("test", "mbola tena mety eto");
try {
int success = json.getInt(TAG_SUCCESS);
String succ=json.getString("ok");
Log.i("milay", succ);
if ((success == 1)) {
Log.i("accepter", "mdp correct");
} else {
Log.i("pas accepter", "non correct");
}
} catch (JSONException e) {
e.printStackTrace();
}
return json.toString();
}
protected void onPostExecute(String result) {
pDialog.dismiss();
com.respond(result);
Fragment2 fb=new Fragment2();
FragmentTransaction t=getFragmentManager().beginTransaction();
Bundle args=new Bundle();
args.putString("mondata", result);
fb.setArguments(args);
t.replace(R.id.myFramePrincipal, fb).commit();
}
}
}
class Fragment2
public class Fragment2 extends Fragment{
TextView text;
String aa;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
text=(TextView) getActivity().findViewById(R.id.textView1);
text.setText(getArguments().getString("mondata"));
}
public void refreshData(String data) {
aa= new String(data);
//aa.notifyDataSetChanged();
text.setText(aa);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment_3, container, false);
}
public void ChangerText(String data) {
text.setText(data);
}
}
Thanks for your help!!

How to disable JButton in the particular row in the JTable

I'm struggling with a at the first sight simple problem (and probably it is so...) - I have to disable certain button in the JTable upon value in one of the cells within the same row.
Below is my inner class for button rendering and editing:
private class CellButton {
class ButtonsPanel extends JPanel {
public JButton jbutton = new JButton("Accept!!");
public ButtonsPanel() {
super();
setOpaque(true);
jbutton.setFocusable(false);
jbutton.setRolloverEnabled(false);
jbutton.setSize(122, 30);
jbutton.setFont(new java.awt.Font("Tahoma", 3, 12));
jbutton.setForeground(new java.awt.Color(0, 102, 102));
add(jbutton);
}
private class ButtonsRenderer extends ButtonsPanel implements TableCellRenderer {
ButtonsPanel bp;
public ButtonsRenderer() {
super();
}
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
this.setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
Object o = table.getModel().getValueAt(row, 8);
bp = (ButtonsPanel) o;
if (jTable5.getValueAt(row, 3).equals("NORMALNA")) {
bp.jbutton.setEnabled(false);
}
return this;
}
}
private class ButtonsEditor extends ButtonsPanel implements TableCellEditor {
ButtonsPanel bp;
public ButtonsEditor(final JTable table, final String tableName) {
super();
//DEBUG: view button click -> control key down + edit button(same cell) press -> remain selection color
MouseListener ml = new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
ButtonModel m = ((JButton) e.getSource()).getModel();
if (m.isPressed() && table.isRowSelected(table.getEditingRow()) && e.isControlDown()) {
setBackground(table.getBackground());
}
}
};
jbutton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
int row = table.convertRowIndexToModel(table.getEditingRow());
Object o = table.getModel().getValueAt(row, 0);
fireEditingStopped();
//update info status
setDmlUpdateStatusPrejeteInfo();
jDialog2.requestFocus();
}
});
addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
fireEditingStopped();
}
});
}
#Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
this.setBackground(table.getSelectionBackground());
Object o = table.getModel().getValueAt(row, 8);
bp = (ButtonsPanel) o;
if (jTable5.getValueAt(row, 3).equals("NORMALNA")) {
bp.jbutton.setEnabled(false);
}
return this;
}
#Override
public Object getCellEditorValue() {
return "";
}
transient protected ChangeEvent changeEvent = null;
#Override
public boolean isCellEditable(EventObject e) {
return true;
}
#Override
public boolean shouldSelectCell(EventObject anEvent) {
return true;
}
#Override
public boolean stopCellEditing() {
fireEditingStopped();
return true;
}
#Override
public void cancelCellEditing() {
fireEditingCanceled();
}
#Override
public void addCellEditorListener(CellEditorListener l) {
listenerList.add(CellEditorListener.class, l);
}
#Override
public void removeCellEditorListener(CellEditorListener l) {
listenerList.remove(CellEditorListener.class, l);
}
public CellEditorListener[] getCellEditorListeners() {
return listenerList.getListeners(CellEditorListener.class);
}
protected void fireEditingStopped() {
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == CellEditorListener.class) {
// Lazily create the event:
if (changeEvent == null) {
changeEvent = new ChangeEvent(this);
}
((CellEditorListener) listeners[i + 1]).editingStopped(changeEvent);
}
}
}
protected void fireEditingCanceled() {
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == CellEditorListener.class) {
// Lazily create the event:
if (changeEvent == null) {
changeEvent = new ChangeEvent(this);
}
((CellEditorListener) listeners[i + 1]).editingCanceled(changeEvent);
}
}
}
}
public ButtonsRenderer br() {
return new ButtonsRenderer();
}
public ButtonsEditor be(JTable jTable, String tableName) {
return new ButtonsEditor(jTable, tableName);
}
}
a) it's better not to call a Jbutton in the name Jbutton, it's just confusing
b) there are many way to disable a jbutton (make it disappear, remove it, resize it...) but the best one is probably this:
Jbutton.setEnabled(false);
c) I think you can use this very useful guide: http://www.macs.hw.ac.uk/guidebook/?name=JButton&page=1