Ok so im building a level editor for this game. What the code i cant working does is, when the button is clicked it should get the selected cell from the jtable and set a value for it. However, for some reason I cannont get it working. Here is the code:

for (int y = 0; y < 30; y++)
					{
					        for (int x = 0; x < 30; x++)
						{
							if(wallChooser.isCellSelected(y, x));
							{
								wallChooser.getModel().setValueAt(value, y, x);
								System.out.println("works again" + x + " " + y);
								value = "";
							}
					        }
                                           }

when i run the application for some reason it just print works again for every single cell. But the selected cell does not get the value.

Please help,

thanks
jakx12

Ok so im building a level editor for this game. What the code i cant working does is, when the button is clicked it should get the selected cell from the jtable and set a value for it. However, for some reason I cannont get it working. Here is the code:

for (int y = 0; y < 30; y++)
					{
					        for (int x = 0; x < 30; x++)
						{
							if(wallChooser.isCellSelected(y, x));
							{
								wallChooser.getModel().setValueAt(value, y, x);
								System.out.println("works again" + x + " " + y);
								value = "";
							}
					        }
                                           }

when i run the application for some reason it just print works again for every single cell. But the selected cell does not get the value.

Please help,

thanks
jakx12

how about when you removed the semi-colon from if(wallChooser.isCellSelected(y, x));

ahh thanks. I didnt see that!
But now it never prints works again so i dont think it registers if a cell has been selected.

Any ideas as to why?

wallChooser.setCellSelectionEnabled(true);

test

for (int y = 0; y < 4; y++) {
            for (int x = 0; x < 4; x++) {
               // if (wallChooser.isCellSelected(y, x)) {
                    String value = "" + x + "|" + y + "|" + wallChooser.isCellSelected(y, x);
                    wallChooser.getModel().setValueAt(value, y, x);
                    System.out.println("works again" + x + " " + y);
               // }
            }
        }

Ok thanks but I have already set cell selection to true, so i dont know why its not working.

Once more explain what does mean

its not working.

private void setValueAtSelectedCell(String value) {
        for (int y = 0; y < 4; y++) {
            for (int x = 0; x < 4; x++) {
                if (wallChooser.isCellSelected(y, x)) {
                    wallChooser.getModel().setValueAt(value, y, x);
                    System.out.println("works again" + x + " " + y);
                }
            }
        }
    }

button action listener method

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        // for example value is a String type and represents current time
        String value = "" + System.currentTimeMillis();
        setValueAtSelectedCell(value);
    }

thats pretty much exactly what i have done, however, the cell never gets the value assigned to it. This is because for some reason the isCellSelected() method always returns false.
Thats what I dont understand.

Select via GUI the cell to be selected. (...or not to be, this is a FAQ)
or post an appropriate dose of the code.

what?
I can post all the code but i think its unnecessary. The problem is not with the code but with that method, provided by JTable.

methods are methods
selected cell is selected
answer the question, why "the cell" was not selected?

what are you talking about? You are not being particularly helpful. Sorry to say this, but that post had nothing to do with what your asking, nor are you trying to answer it. My question is why is it always returning false.

I can't be helpfull. I do not know your code. It is not possible to discuss on something to which they have no access.
Try my example.

package jakx12;

/*
 * TestTable.java
 *
 * Created on 2009-10-25, 14:05:24
 */
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.*;

/**
 *
 * @author j3c
 */
public class TestTable extends JPanel {

    private JScrollPane jScrollPane;
    private JTable wallChooser;
    private JButton jButton1;
    private JButton jButton2;

    public TestTable() {
        initComponents();
        jScrollPane = new JScrollPane();
        wallChooser = new JTable();
        wallChooser.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]{
                    {null, null, null, null, null, null, null, null, null, null},
                    {null, null, null, null, null, null, null, null, null, null},
                    {null, null, null, null, null, null, null, null, null, null},
                    {null, null, null, null, null, null, null, null, null, null},
                    {null, null, null, null, null, null, null, null, null, null},
                    {null, null, null, null, null, null, null, null, null, null},
                    {null, null, null, null, null, null, null, null, null, null}
                },
                new String[]{
                    "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",}));
        wallChooser.setCellSelectionEnabled(true);
        jScrollPane.setViewportView(wallChooser);
        add(jScrollPane, BorderLayout.CENTER);
    }

    private void initComponents() {

        jButton1 = new JButton();
        jButton2 = new JButton();
        setLayout(new java.awt.BorderLayout());
        jButton1.setText("setValueAtSelectedCell");
        jButton1.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jButton2.setText("clear");
        jButton2.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(1, 0));
        panel.add(jButton1);
        panel.add(jButton2);
        add(panel, java.awt.BorderLayout.PAGE_END);
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        String value = "" + System.currentTimeMillis();
        setValueAtSelectedCell(value);
    }

    private void setValueAtSelectedCell(String value) {
        for (int y = 0; y < wallChooser.getModel().getRowCount(); y++) {
            for (int x = 0; x < wallChooser.getModel().getColumnCount(); x++) {
                if (wallChooser.isCellSelected(y, x)) {
                    wallChooser.getModel().setValueAt(value, y, x);
                    System.out.println("works again" + x + " " + y);
                }
            }
        }
    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
        for (int y = 0; y < wallChooser.getModel().getRowCount(); y++) {
            for (int x = 0; x < wallChooser.getModel().getColumnCount(); x++) {
                wallChooser.getModel().setValueAt(null, y, x);
            }
        }
    }

    public static void main(String[] args) {
        JFrame f = new JFrame("jakx12");
        f.setDefaultCloseOperation(3);
        f.add(new TestTable());
        f.setSize(1000, 200);
        f.setVisible(true);
    }
}

check

wallChooser.isEditing()

if result is true, blocking the setting

Thank you for the help, I will try your code, but as i said, the only problem is that the icellselcted always returns false.

Feel free traying this code.
here is modified method with cancelCellEditing

private void setValueAtSelectedCell(String value) {
                    //if (wallChooser.isEditing()) {
                    //    wallChooser.getCellEditor().cancelCellEditing();
                    //}
                    //
        for (int row = 0; row < wallChooser.getModel().getRowCount(); row++) {
            for (int column = 0; column < wallChooser.getModel().getColumnCount(); column++) {
                if (wallChooser.isCellSelected(row, column)) {       
                    //force canceling editing of selected cell
                    wallChooser.getCellEditor(row, column).cancelCellEditing();
                    wallChooser.getModel().setValueAt(value, row, column);
                    System.out.println("works again " + row + " " + column);
                }
            }
        }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.