Did Western empires ever attempt to colonize Japan? Was there any military confrontation? Did Japan's modernization and adoption of European styles of bureaucracy, industry, and military organization have anything to do with perceived threat of vulnerability to colonial powers?
They actually were subjected to New Imperialism in this era, if only for a short while.
First, it is important to understand that Japan resisted early colonial efforts during the Edo period from 1633 to 1854, under the policy of Sakoku (closed country). Tokugawa responded to foreign religious influence and potential foreign colonialism by forbading anyone to enter or leave Japan, executing anyoneTthat stepped foot on Japan. That policy combined with outlawing Christian worship to essentially eliminate Christianity form Japan. Limited trade was only allowed through the Dutch in certain ports, because they were the only ones not attempting to spread Christianity. These policies closed Japan off from the world, but were only a temporary fix.
Second, America did impose the New Imperialism practice of treaty ports onto Japan by force. Commodore Matthew Perry threatened Japan with ships that awed the samurai. The Shogun accepted a highly unpopular unequal treaty system called the Harris Treaty in 1860, which was not unlike the one imposed on China. Basically, there were certain ports in which Americans, and later other western nationals, could operate with immunity from Japanese law and Japan could not set its own tariffs and trade policy, among other stipulations. Because of this, one specific clan led multiple rebellions, with the goal of modernizing Japan in order to resist foreign Imperialism.
The Choshu clan had two punitive expeditions launched against it by the Shogun. The second expedition failed spectacularly, mostly due to the Satsuma clan withdrawing its support and joining Choshu. Both of these launched a civil war in order to install the Emperor snd themselves as rulers, saying the Shogun had lost favor. And they won, immediately embarking on a massive campaign to modernize Japan.
Their rapid modernization program was able to repeal the unequal treaty system. Some of this was due to Japanese elite adopting western customs, laws, and governmental structure, impressing diplomats. However, much of it was due to Japan's success in the Fist Sino-Japanese war. Japan had created a modern army within only forty years. America had the Harris Treaty effectively repealed in 1899 as Japan grew in strength.
Third, Japan consequently engaged in many actions specifically due to the threat of imperialism, particularly from Russia. Japan engaged in an expansionary continental security strategy. Basically this meant they saw territory on the mainlad as vital to preventing agression of western powers. However, many international relations scholars, like the one I cite for this comment, say that this policy was often counter productive due to Japan's natural barriers and reliance on trade. Ultimately, threats of Imperialism beget Japanese Imperialism.
Japan was almost immediately put at odds with Russia for influence in the region. Russia would not back down from Korea and Japan viewed Korea as a foreign launch pad for an invasion into Japan. Czar Nicholas II never gave up the sphere of influence claim over Korea, partly due to racist sentiment and some nobel's logging interests, but not real strategic concerns. After negotiations failed to go anywhere, Japan surprised attacked Russia, and engaged in battles that would be the precursor to WWI's slaughter. Japan won narrowly, and was able to secure spheres of influence over China and Korea, imposing its own version of unequal treaties.
Japan's security strategy was counterproductive. It only agitated the people in the markets in which it was trying to open. That is not to even mention the two contradictory grand strategic plans from the navy and army that were developed and never reconciled. Japan still expanded its territory into N China, in the Mukden incident. Almost every justification for war was done using resistance of western Imperialism. They said that western countries were attempting to A. Hurt Japan economically by imposing tarrifs and trade restrictions and/or B. Gain a nearby foothold to invade or otherwise hurt Japan. However, Japan also engaged in diplomacy with and fought wars with western powers.
Japan was well known and treated seriously by western powers because of its diplomatic and military actions. The gift of cherry trees still dot DC to this day, showing how Japan wanted to bee seen as a modern power on oar with the west. Moreover, Japan fought alongside western powers in different engagements like the Boxer rebelluon and also managed to defeat one of the largest countries, Russia. All of this meant that Japan was treated at least somewhat seriously by western countries, and not a simple target to be easily subjugated. Japan signed major naval treaties with US and Britain, which were still unfair towards Japan, but allowed a substantial navy and showed that they feared Japanese power projection. Ultimately, all of this would spiral out of control as Japan's incoherent security policy unraveled in the Second Sino-Japanese and Pacific wars.
S C Paine The Japanese Empire: Grand Strategy from the Meiji Restoration to the Pacific War