after setText the JButton does not update its new text

[nobr]i have a jbutton set up with an actionevent

it can change the text when an it is pressed but not when the a function is called

publicvoid actionPerformed(ActionEvent e){

System.err.println(e.getActionCommand());

final JButton source = (JButton) (e.getSource());

source.setText(source.getText());

hint[4].setText("<html><center><font size=2>1234<br>6789</font></center></html>");

}

publicvoid setCell(finalint row,finalint column,finalint value){

hint[row * 3 + column].setText("" + value);

System.err.println("row " + row +"col " + column +"value "

+ value);

}

syserr shows the correct values.

When the actionperformed is called, the update does occur but when setCell is called, the updates does not happen.[/nobr]

[1457 byte] By [jdelatora] at [2007-10-3 10:51:20]
# 1

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.

camickra at 2007-7-15 6:16:34 > top of Java-index,Desktop,Core GUI APIs...
# 2

[nobr]short.java

import java.awt.Dimension;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;

import javax.swing.Box;

import javax.swing.BoxLayout;

import javax.swing.JFileChooser;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JPanel;

import javax.swing.KeyStroke;

import javax.swing.UIManager;

public class Short implements ActionListener {

static JFileChooser fileChooser;

SudokuPanel sudokuPane;

JPanel mainPane;

public Short() {

sudokuPane = new SudokuPanel("Sudoku Puzzle");

mainPane = new JPanel();

mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.PAGE_AXIS));

mainPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

mainPane.add(Box.createRigidArea(new Dimension(0, 5)));

mainPane.add(sudokuPane);

mainPane.add(Box.createGlue());

}

/**

* main - main function which calls itself from a thread

*

* @param arg

*command line arguments which have no effect

* @return nothing

*/

public static void main(final String[] args) {

// Schedule a job for the event-dispatching thread:

// creating and showing this application's GUI.

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

Short J = new Short();

J.createAndShowGUI();

}

});

}

/**

* createAndShowGUI - Creates the GUI and displays it. For thread safety,

* this function should be called from the event-dispatching thread.

*

* @param nothing

* @return nothing

*/

public void createAndShowGUI() {

try {

UIManager.setLookAndFeel(UIManager

.getCrossPlatformLookAndFeelClassName());

} catch (final Exception e) {

System.out.println("Failed to set the Cross Platform GUI");

e.printStackTrace();

}

// Create and set up the window.

final JFrame frame = new JFrame("My Sudoku");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Short mySudoku = new Short();

mySudoku.mainPane.setOpaque(true);

frame.setContentPane(mySudoku.mainPane);

// create the Menu and the "debug window"

frame.setJMenuBar(this.createMenu());

// Display the window.

frame.pack();

frame.setVisible(true);

}

public JMenuBar createMenu() {

// Build the menu items for Help Menu

final JMenuItem helpTopics = new JMenuItem("Help Topics");

helpTopics.setAccelerator(KeyStroke.getKeyStroke("ctrl H"));

helpTopics.addActionListener(this);

// Build the Help menu

final JMenu helpMenu = new JMenu("Help");

helpMenu.setMnemonic(KeyEvent.VK_H);

helpMenu.add(helpTopics);

helpMenu.addSeparator();

// Build the Menu Bar

final JMenuBar menuBar = new JMenuBar();

menuBar.add(helpMenu);

return menuBar;

}

public void actionPerformed(final ActionEvent e) {

e.getActionCommand();

System.err.println(e.getActionCommand());

final JMenuItem source = (JMenuItem) (e.getSource());

if (source.getText().equals("Help Topics")) {

this.openFile();

}

}

private void openFile() {

sudokuPane.openFile();

}

}

SudokuBox.java

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JComboBox;

import javax.swing.JFormattedTextField;

import javax.swing.JPanel;

public class SudokuBox extends JPanel implements ActionListener {

private static final long serialVersionUID = 1;

JFormattedTextField textField;

JComboBox unitChooser;

String title;

JButton[] hint;

SudokuBox() {

setLayout(new GridLayout(3, 3));

hint = new JButton[9];

for (int i = 0; i < 9; i++) {

hint[i] = new JButton("<html><font size=6><b>" + "."

+ "</b></font></html>");

hint[i].addActionListener(this);

add(hint[i]);

}

}

public void actionPerformed(ActionEvent e) {

System.err.println(e.getActionCommand());

hint[4].setText("<html><center><font size=2>1234<br>6789</font></center></html>");

}

public void setCell(final int row, final int column, final int value) {

hint[4].setText("<html><center><font size=2>setCell works</font></center></html>");

hint[row * 3 + column].setText("" + value);

System.err.println("row " + row + " col " + column + " value "

+ value);

}

}

SudokuPanel.java

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JComboBox;

import javax.swing.JFormattedTextField;

import javax.swing.JPanel;

public class SudokuBox extends JPanel implements ActionListener {

private static final long serialVersionUID = 1;

JFormattedTextField textField;

JComboBox unitChooser;

String title;

JButton[] hint;

SudokuBox() {

setLayout(new GridLayout(3, 3));

hint = new JButton[9];

for (int i = 0; i < 9; i++) {

hint[i] = new JButton("<html><font size=6><b>" + "."

+ "</b></font></html>");

hint[i].addActionListener(this);

add(hint[i]);

}

}

public void actionPerformed(ActionEvent e) {

System.err.println(e.getActionCommand());

hint[4].setText("<html><center><font size=2>1234<br>6789</font></center></html>");

}

public void setCell(final int row, final int column, final int value) {

hint[4].setText("<html><center><font size=2>setCell works</font></center></html>");

hint[row * 3 + column].setText("" + value);

System.err.println("row " + row + " col " + column + " value "

+ value);

}

}

pressing the jbuttons will update some of the values within the jbutton but clicking on help->help topics will not update any values, even though setText does get called on the jbuttons[/nobr]

jdelatora at 2007-7-15 6:16:34 > top of Java-index,Desktop,Core GUI APIs...
# 3

How is this code executable? You included the code for SudoduBox, but you never use the class in the posted code.

What is the code for the menu included for? Your problem is with a button, not on how to build a menu.

Reread the definition of SSCCE. The idea is for you to isolate you problem in a simple demo program and then we can provide some help.

camickra at 2007-7-15 6:16:34 > top of Java-index,Desktop,Core GUI APIs...
# 4

thats is sscce

execute short.java

sudokubox is used by sudokupanel

this is is as simple as i can get it to be. I want to have the full effect of jpanels that i use, so that you guys can help me

When you use the menu items to trigger a setText, nothing gets updated but when you click on an item setText works properly. This is why I have the menu.

Message was edited by:

jdelator

jdelatora at 2007-7-15 6:16:34 > top of Java-index,Desktop,Core GUI APIs...
# 5
> thats is ssccenot quite - looks like you've posted 2 x SudokoBox
Michael_Dunna at 2007-7-15 6:16:34 > top of Java-index,Desktop,Core GUI APIs...
# 6
no one is sudokubox and the other was sudokupanelthis was a complete waste of time, you guys just worry about sscce
jdelatora at 2007-7-15 6:16:34 > top of Java-index,Desktop,Core GUI APIs...
# 7

> no one is sudokubox and the other was sudokupanel

Just because you say it is doesn't make it so.

This is precisely the reason we ask for an SSCCE. 90% of the questions posted on the forum are because of silly mistakes made by the programmer. Forcing your to create a SSCCE will usually cause you to find the mistake. We are not here to debug programs for you.

This posting is another example of a silly mistake. You can't even copy, paste code correctly.

camickra at 2007-7-15 6:16:34 > top of Java-index,Desktop,Core GUI APIs...
# 8

> one is sudokubox and the other was sudokupanel

no, both are

public class SudokuBox extends JPanel implements ActionListener {

> this was a complete waste of time,

exactly

> you guys just worry about sscce

you're the one with the problem (and not just the code).

you're even too lazy to check your own post.

Michael_Dunna at 2007-7-15 6:16:34 > top of Java-index,Desktop,Core GUI APIs...
# 9

> you guys just worry about sscce

this post (today) is an excellent example fof why sscce's are requested

http://forum.java.sun.com/thread.jspa?threadID=789808

however, I'm willing to learn - show us what helpful response you would post,

in order to solve the problem.

Do not ask for more info - that would be "a waste of time".

Just work with what is in the original post.

Michael_Dunna at 2007-7-15 6:16:34 > top of Java-index,Desktop,Core GUI APIs...
# 10

[nobr]yes i agree. Michael said just perfect. how ever check this out

import java.awt.Dimension;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;

import javax.swing.Box;

import javax.swing.BoxLayout;

import javax.swing.JButton;

import javax.swing.JComboBox;

import javax.swing.JFileChooser;

import javax.swing.JFormattedTextField;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JPanel;

import javax.swing.KeyStroke;

import javax.swing.UIManager;

/**

* @author: aniruddha<br>

* @date: Nov 28, 2006, 9:32:01 AM<br>

* @source: JButtonProb.java<br>

* @project: HelpForum<br>

*/

/**

* @author aniruddha

*

*/

public class JButtonProb implements ActionListener

{

static JFileChooserfileChooser;

SudokuPanelsudokuPane;

JPanelmainPane;

public JButtonProb()

{

sudokuPane = new SudokuPanel();

mainPane = new JPanel();

mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.PAGE_AXIS));

mainPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

mainPane.add(Box.createRigidArea(new Dimension(0, 5)));

mainPane.add(sudokuPane);

mainPane.add(Box.createGlue());

}

/**

* main - main function which calls itself from a thread

*

* @param arg

*command line arguments which have no effect

* @return nothing

*/

public static void main(final String[] args)

{

// Schedule a job for the event-dispatching thread:

// creating and showing this application's GUI.

javax.swing.SwingUtilities.invokeLater(new Runnable()

{

public void run()

{

JButtonProb J = new JButtonProb();

J.createAndShowGUI();

}

});

}

/**

* createAndShowGUI - Creates the GUI and displays it. For thread safety,

* this function should be called from the event-dispatching thread.

*

* @param nothing

* @return nothing

*/

public void createAndShowGUI()

{

try

{

UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());

}

catch(final Exception e)

{

System.out.println("Failed to set the Cross Platform GUI");

e.printStackTrace();

}

// Create and set up the window.

final JFrame frame = new JFrame("My Sudoku");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JButtonProb mySudoku = new JButtonProb();

mySudoku.mainPane.setOpaque(true);

frame.setContentPane(mySudoku.mainPane);

// create the Menu and the "debug window"

frame.setJMenuBar(this.createMenu());

// Display the window.

frame.pack();

frame.setVisible(true);

}

public JMenuBar createMenu()

{

// Build the menu items for Help Menu

final JMenuItem helpTopics = new JMenuItem("Help Topics");

helpTopics.setAccelerator(KeyStroke.getKeyStroke("ctrl H"));

helpTopics.addActionListener(this);

// Build the Help menu

final JMenu helpMenu = new JMenu("Help");

helpMenu.setMnemonic(KeyEvent.VK_H);

helpMenu.add(helpTopics);

helpMenu.addSeparator();

// Build the Menu Bar

final JMenuBar menuBar = new JMenuBar();

menuBar.add(helpMenu);

return menuBar;

}

public void actionPerformed(final ActionEvent e)

{

e.getActionCommand();

System.err.println(e.getActionCommand());

final JMenuItem source = (JMenuItem) (e.getSource());

if(source.getText().equals("Help Topics"))

{

this.openFile();

}

}

private void openFile()

{

//sudokuPane.openFile();

}

}

class SudokuBox extends JPanel implements ActionListener

{

private static final longserialVersionUID= 1;

JFormattedTextFieldtextField;

JComboBoxunitChooser;

Stringtitle;

JButton[]hint;

SudokuBox()

{

setLayout(new GridLayout(3, 3));

hint = new JButton[9];

for(int i = 0; i < 9; i++)

{

hint[i] = new JButton("<html><font size=6><b>" + "." + "</b></font></html>");

hint[i].addActionListener(this);

add(hint[i]);

}

}

public void actionPerformed(ActionEvent e)

{

System.err.println(e.getActionCommand());

hint[4].setText("<html><center><font size=2>1234<br>6789</font></center></html>");

}

public void setCell(final int row, final int column, final int value)

{

hint[4].setText("<html><center><font size=2>setCell works</font></center></html>");

hint[row * 3 + column].setText("" + value);

System.err.println("row " + row + " col " + column + " value " + value);

}

}

class SudokuPanel extends JPanel implements ActionListener

{

private static final longserialVersionUID= 1;

JFormattedTextFieldtextField;

JComboBoxunitChooser;

Stringtitle;

JButton[]hint;

SudokuPanel()

{

setLayout(new GridLayout(3, 3));

hint = new JButton[9];

for(int i = 0; i < 9; i++)

{

hint[i] = new JButton("<html><font size=6><b>" + "." + "</b></font></html>");

hint[i].addActionListener(this);

add(hint[i]);

}

}

public void actionPerformed(ActionEvent e)

{

System.err.println(e.getActionCommand());

((JButton)e.getSource()).setText("<html><center><font size=2>ADC<br>JAVA</font></center></html>");

hint[4].setText("<html><center><font size=2>1234<br>6789</font></center></html>");

}

public void setCell(final int row, final int column, final int value)

{

hint[4].setText("<html><center><font size=2>setCell works</font></center></html>");

hint[row * 3 + column].setText("" + value);

System.err.println("row " + row + " col " + column + " value " + value);

}

}

:)[/nobr]

Aniruddha-Herea at 2007-7-15 6:16:34 > top of Java-index,Desktop,Core GUI APIs...