blob: f47e8b2b42080800d64eb4ce8c1b2bf53c068fca [file] [log] [blame]
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
= DevFaqInput
:jbake-type: wiki
:jbake-tags: wiki, devfaq, needsreview
:jbake-status: published
:keywords: Apache NetBeans wiki DevFaqInput
:description: Apache NetBeans wiki DevFaqInput
:toc: left
:toc-title:
:syntax: true
This is an example on how to get inout from the Output Window. Just change what's done on the read method (listener implementation) to customize the behavior. Things that come to mind: chat client, text based games, etc...
[source,java]
----
/*
* Enable/create the tabs we need for the component
*/
package sample.component;
import com.dreamer.outputhandler.InputMonitor;
import com.dreamer.outputhandler.OutputHandler;
import org.jivesoftware.smack.util.ReaderListener;
import org.openide.modules.ModuleInstall;
import org.openide.windows.WindowManager;
/**
* Manages a module's lifecycle. Remember that an installer is optional and
* often not needed at all.
*/
public class Installer extends ModuleInstall implements ReaderListener {
private final String normal = "Output";
@Override
public void restored() {
WindowManager.getDefault().invokeWhenUIReady(new Runnable() {
@Override
public void run() {
OutputHandler.output(normal, "Welcome! Type something below.");
//Create a monitor for the tab. This enables input in the tab as well.
InputMonitor monitor = OutputHandler.createMonitor(normal);
//Add a listener to be notified.
monitor.addListener(Installer.this);
}
});
}
@Override
public void read(String read) {
OutputHandler.output(normal, "You typed: " + read);
}
}
----
*Note: *This is feature is available on the link:http://plugins.netbeans.org/plugin/39695/?show=true[Output Handler plugin] version 1.2.
== Example of using input reader in output window
[source,java]
----
package sample.component;
import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.IOException;
import org.openide.util.Exceptions;
import org.openide.util.RequestProcessor;
import org.openide.windows.IOProvider;
import org.openide.windows.InputOutput;
import org.openide.windows.OnShowing;
@OnShowing
public class OutputWindowReaderExample implements Runnable {
/**
* Open Output Window and ask for some input.
*/
public static void requestAnswerExample() throws IOException {
assert !EventQueue.isDispatchThread();
InputOutput io = IOProvider.getDefault().getIO("ioName", true);
io.select();
io.getOut().println("How are you?");
BufferedReader br = new BufferedReader(io.getIn());
String answer = br.readLine();
io.getOut().println("You are " + answer + " today");
br.close();
io.getOut().close();
io.getErr().close();
}
/**
* Call requestAnswerExample from a background thread, after initialization
* of the user interface.
*/
@Override
public void run() {
RequestProcessor.getDefault().post(new Runnable() {
@Override
public void run() {
try {
requestAnswerExample();
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
}
});
}
}
----
*Note:* The module that contains this class depends on Window System API (org.openide.windows).
=== Apache Migration Information
The content in this page was kindly donated by Oracle Corp. to the
Apache Software Foundation.
This page was exported from link:http://wiki.netbeans.org/DevFaqInput[http://wiki.netbeans.org/DevFaqInput] ,
that was last modified by NetBeans user Jhavlin
on 2012-10-17T11:51:08Z.
*NOTE:* This document was automatically converted to the AsciiDoc format on 2018-02-07, and needs to be reviewed.