I am currently (Dec 13, 2016) taking a class on Groovy, and I found that the Groovy console have a huge bug which doesn’t allow it to obtain input from the user. System.console() gives ‘null’ and all the solutions involving the use of System.in or the input reader buffer seems to fail.

These input-from-user bugs seems to be known based on what the instructor told me when I showed this solution. After some research, I found that they were reported but nothing have being done so far to solve the issue.

Below is the only solution I found so far that works, creating a dialog in order to obtain the input from the user and execute a code.
Please notice that if you close the dialog (pressing the X button), you will kill the groovy console too.

import groovy.swing.SwingBuilder;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
 
def sb = new SwingBuilder()
sb.frame(title: 'Action',
         location: [300, 55],
         pack: true,
         show: true,
         defaultCloseOperation: JFrame.EXIT_ON_CLOSE) {

    gridLayout(columns: 2, rows: 4)
    label('Continue [ Y/N ]:')
    textField(id: 'input')       
    
    button(text: 'Execute', actionPerformed: {
       execute(input.text, sb)
    })            
}

def execute(input, sb){    
    // Your code
    output("User andwer: ${input}", sb) 
    // More of your code
}

def output(output, sb){
    def out = (output) ? output : ""
    sb.optionPane().showMessageDialog(null, 
                                      "$output", 
                                      "Executing",
                                      JOptionPane.INFORMATION_MESSAGE)
}

© 2016, Alejandro G. Carlstein Ramos Mejia. All rights reserved.

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

*

Click to Insert Smiley

SmileBig SmileGrinLaughFrownBig FrownCryNeutralWinkKissRazzChicCoolAngryReally AngryConfusedQuestionThinkingPainShockYesNoLOLSillyBeautyLashesCuteShyBlushKissedIn LoveDroolGiggleSnickerHeh!SmirkWiltWeepIDKStruggleSide FrownDazedHypnotizedSweatEek!Roll EyesSarcasmDisdainSmugMoney MouthFoot in MouthShut MouthQuietShameBeat UpMeanEvil GrinGrit TeethShoutPissed OffReally PissedMad RazzDrunken RazzSickYawnSleepyDanceClapJumpHandshakeHigh FiveHug LeftHug RightKiss BlowKissingByeGo AwayCall MeOn the PhoneSecretMeetingWavingStopTime OutTalk to the HandLoserLyingDOH!Fingers CrossedWaitingSuspenseTremblePrayWorshipStarvingEatVictoryCurseAlienAngelClownCowboyCyclopsDevilDoctorFemale FighterMale FighterMohawkMusicNerdPartyPirateSkywalkerSnowmanSoldierVampireZombie KillerGhostSkeletonBunnyCatCat 2ChickChickenChicken 2CowCow 2DogDog 2DuckGoatHippoKoalaLionMonkeyMonkey 2MousePandaPigPig 2SheepSheep 2ReindeerSnailTigerTurtleBeerDrinkLiquorCoffeeCakePizzaWatermelonBowlPlateCanFemaleMaleHeartBroken HeartRoseDead RosePeaceYin YangUS FlagMoonStarSunCloudyRainThunderUmbrellaRainbowMusic NoteAirplaneCarIslandAnnouncebrbMailCellPhoneCameraFilmTVClockLampSearchCoinsComputerConsolePresentSoccerCloverPumpkinBombHammerKnifeHandcuffsPillPoopCigarette