We study an opportunistic subchannel scheduling and transmission mode selection problem for the OFDMA system with device-to-device (D2D) communication. We allow D2D users to opportunistically select its transmission mode between two transmission modes: direct transmission between D2D users (direct one- hop transmission) and indirect transmission through the BS (indirect two-hop transmission). We develop a framework with which opportunistic transmission mode selection can be modeled as opportunistic subchannel scheduling, which enables our problem to be reduced to an opportunistic subchannel scheduling problem. We formulate a stochastic optimization problem that aims to maximize the average sum-rate of the system, while satisfying the quality-of-service (QoS) requirement of each user. By solving the problem, we develop an optimal opportunistic subchannel scheduling algorithm, which enables us to perform both subchannel scheduling and transmission mode selection opportunistically.