AS PRESIDENT Obama indulged in some pre-electoral China-bashing — announcing a trade case on auto parts during a campaign appearance in Ohio — his secretary of defense was learning how such nationalist games are played across the Pacific. Leon Panetta’s visit to Tokyo and Beijing this week coincided with mass demonstrations in scores of Chinese cities, where crowds burned Japanese flags, trashed Japanese cars and sang patriotic songs — all under the approving eyes of state security forces.

China, of course, does not have an election coming up — but it is planning a transfer of power among top leaders sometime in the coming weeks. The shift from outgoing Hu Jintao to incoming Xi Jinping has been anything but smooth; among other things, Mr. Xi abruptly disappeared from view for two weeks this month without explanation. Though he appeared physically and politically healthy when he met Mr. Panetta on Wednesday, China’s leaders seem to think this week was a convenient moment to orchestrate an outpouring of nationalist feeling. A rally against perceived foreign enemies could bolster fledgling new leaders — and deflect questions about the opaque power struggles and faltering economy at home.

Though they responded temperately to the Chinese rabble-rousing, Japan’s leaders were not innocent of political calculation. The pretext for the demonstrations was provided last week when the government of Yoshihiko Noda announced that it would purchase three uninhabited islands near Taiwan, known to Japan as the Senkaku and to China as the Diaoyu and claimed by both countries. Mr. Noda was trying to undercut nationalist Tokyo Governor Shintaro Ishihara, who had talked of having his city buy the desolate rocks from their Japanese private owners. Both politicians are maneuvering ahead of Japan’s next elections, due by early next year.

Mr. Panetta, who was appealing for reason on all sides, didn’t seem to get very far: China’s defense minister publicly threatened “further actions” against Tokyo after their meeting. In Tokyo, meanwhile, Mr. Panetta likely heard grumbling about another island dispute, between Japan and South Korea: Outgoing South Korean President Lee Myung-bak is using his last months in office to stoke tensions over what Seoul calls the Dokdo islands, as well as its demands for further Japanese apologies and compensation for World War II.

The fact that all these tempests are driven by domestic political calculations suggests they are less serious than they appear. Chinese, Japanese and Korean politicians are aiming mainly to win votes, not to vanquish their neighbors. But posturing of this kind does have economic costs: Many Japanese companies and stores have temporarily shut down in China, thousands of travelers have canceled their tickets, and there is talk that big Japanese manufacturers, which have seen their stocks swoon this week, will look for factory sites outside China.

There is also a danger that China will lose control of the crowds it conjured. Western journalists noticed that some demonstrators in Beijing carried Mao posters, and they wondered if they were followers of Bo Xilai, the populist provincial party chief who was purged six months ago. Whether in Ohio or Beijing, an opportunistic pander to populist sentiment often boomerangs in the long run.