Excel Prompt User on Focus
In this section, you will learn how to prompt user on focusing the cell using Apache POI.
EXAMPLE
In the below example, you will learn how to prompt user on focusing the cell. Here, the message title and message is user defined.
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.hssf.usermodel.DVConstraint; import org.apache.poi.hssf.usermodel.HSSFDataValidation; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.DataValidation; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.util.CellRangeAddressList; public class XLFocusPrompt { public static void main(String args[]) throws FileNotFoundException { Workbook workbook = new HSSFWorkbook(); Sheet sheet = workbook.createSheet("Message on Focus"); CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0); DVConstraint dvConstraint = DVConstraint .createExplicitListConstraint(new String[] { "10", "20", "30" }); DataValidation dataValidation = new HSSFDataValidation(addressList, dvConstraint); // DISPLAY MSG WHEN CELL A1 IS ON FOCUS dataValidation.createPromptBox("Message on Focus", "Cell A1 is on Focus"); dataValidation.setShowPromptBox(true); sheet.addValidationData(dataValidation); FileOutputStream fileOut = new FileOutputStream("xls/XLFocusPrompt.xls"); try { workbook.write(fileOut); fileOut.close(); } catch (IOException e) { e.printStackTrace(); } } }
OUTPUT