I'm trying to populate a comboBox in my server GUI with some data. Specifically, i would like to add the id of the clients registered to the Server.
I must have implemented it wrong, because changes after the initialize() method of the gui don't apply.
I copy here the involved code:
In server:
public class Server implements ServerInterface {
private static List<ClientInterface> clients;
static ServerGui gui;
public Server() throws RemoteException
{
super();
clients = new ArrayList<ClientInterface>( );
gui = new ServerGui();
}
*.... other methods not involved ....*
public static void main(String[] args)
{
try {
Server statsServer = new Server();
ServerInterface stub = (ServerInterface) UnicastRemoteObject.exportObject(statsServer, 1099);
Registry r=LocateRegistry.createRegistry(1099);
r.rebind("aaa", stub);
System.out.println("Server ready");
//gui.fullCombobox(clients);
gui.main(null);
}
/* If any communication failures occur... */
catch (RemoteException e) {
System.out.println("Communication error " + e.toString());
}
}
}
In GUI
import java.awt.EventQueue;
public class ServerGui {
private JFrame frame;
public JComboBox<String> comboBox;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ServerGui window = new ServerGui();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public ServerGui() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 725, 487);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
comboBox = new JComboBox();
comboBox.addItem("ciao");
comboBox.addItem("miao");
comboBox.setBounds(162, 113, 182, 38);
frame.getContentPane().add(comboBox);
}
public void fullCombobox(List <ClientInterface> lista) throws RemoteException
{
Iterator<ClientInterface> it= lista.iterator();
while(it.hasNext())
{
ClientInterface c = ( ClientInterface ) it.next();
Integer i = c.getId();
comboBox.addItem(i.toString());
}
}
}
When i invoke a Server method through Client, i invoke at the end of it the fullComboBox method too. I supposed the combobox would reload automatically, but it doesn't change at all.
It seems that all i do after the initialize() method doesn't work.
How can i resolve that?
And am i following the right way to do a gui for a Server? I had some doubts if to put a gui object inside the server (as i did) or to put a server inside the gui. I'm really confused.
Thanks in advance to everyone.
From reviewing your code shortly, it seems that you are not even calling the method
public void fullCombobox(List<ClientInterface> lista) throws RemoteException {}
You either forgot to show the call in your code or it was the mistake that made you think that it is a problem with the JComboBox.
Related
I'm trying to create a text chat with Java. I have a Server and a Client that connect to each other using Streams, and send data using the objectInputStream and objectOutputStream.
I have GUI's for both the client and the server.
I made these GUI's using intellij's GUI Form.
server GUI form image
The problem I'm having is when I try to display text to the GUI of the server. I can append to the GUi if I call my relayToAll method from the JTextField actionlistener, which then send the message to all the clients and prints it out in the servers GUI.
If i try to call the same method from where I receive the input, then the append to the text area does not work.
Can anyone tell me why its not appending?
Thanks
public class ServerTest {
private JTextField textField1;
private JTextArea textArea1;
private JPanel Panel;
static private ObjectOutputStream objectOutputStream;
static private ObjectInputStream objectInputStream;
static private Socket client;
static private ArrayList<Socket> clients = new ArrayList<Socket>();
static private ArrayList<ObjectOutputStream> objectOutputStreams = new ArrayList<>();
public void relayToAll(String message){
try {
for(int i = 0; i < clients.size(); i++) {
ObjectOutputStream output = objectOutputStreams.get(i);
output.writeObject(message);
output.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
appendTextArea(message);
}
public void appendTextArea(String text){
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
System.out.println("This should go to the Server GUI: " + text);
textArea1.append(text + "\n");
}
});
}
public ServerTest() {
textField1.addActionListener(e -> {
System.out.println(e.getActionCommand());
relayToAll(e.getActionCommand());
textField1.setText("");
});
}
public void ReadInput(ObjectInputStream input, int port){
try {
String oldMessage = "";
while (true) {
String message = (String) input.readObject();
if (message != oldMessage){
System.out.println(port + ": " + message);
oldMessage = message;
relayToAll(port + ": " + message);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public void IOSetup(){
try {
ServerSocket serverSocket = new ServerSocket( 6969 );
ExecutorService executor = Executors.newFixedThreadPool(5);
System.out.println("server on\n");
for (int i = 0; i < 5; i++){
client = serverSocket.accept();
clients.add(client);
System.out.println("Connection from: "+ client.getPort());
objectOutputStream = new ObjectOutputStream(client.getOutputStream());
objectOutputStreams.add(objectOutputStream);
objectInputStream = new ObjectInputStream(clients.get(i).getInputStream());
executor.submit(() -> {
ReadInput(objectInputStream, client.getPort());
});
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Server");
frame.setContentPane(new ServerTest().Panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
ServerTest application = new ServerTest();
application.IOSetup();
}
Actually you've got kind of a silly mistake. Please check lines (A) and (B) below:
public static void main(String[] args) {
JFrame frame = new JFrame("Server");
frame.setContentPane(new ServerTest().Panel); // *************** (A)
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
ServerTest application = new ServerTest(); // *************** (B)
application.IOSetup();
}
Do you see a problem? You're creating TWO ServerTest objects, one that has its Panel variable added to the JFrame and that gets displayed, and the other that is set up for IO communication. The ActionListener changes the state of the displayed JTextArea while the IO communications changes the state of a JTextArea that is in the second ServerTest instance, the one not displayed.
One improvement is to create only one instance:
public static void main(String[] args) {
ServerTest application = new ServerTest(); // create one instance
JFrame frame = new JFrame("Server");
// frame.setContentPane(new ServerTest().Panel);
frame.setContentPane(application.Panel); // and use in both places
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
//ServerTest application = new ServerTest();
application.IOSetup(); // and use in both places
}
Other problems:
You've got long-running code long running and blocking in a background thread, and that is potentially dangerous, and the only reason that your GUI is not getting frozen is because you're starting the GUI (incorrectly) on the main thread and off of the Swing event thread. For more on this, you will want to read up on Swing concurrency: Lesson: Concurrency in Swing
You will want to learn and use Java naming conventions. Variable names should all begin with a lower letter while class names with an upper case letter. Learning this and following this will allow us to better understand your code, and would allow you to better understand the code of others.
I am an absolute beginner in coding. I would like to know why is my Jframe blank when run, how do I fix it. From what I have research on the internet it seems that I should put the component inside the JFrame as it is empty but how do I do it
My Code
public class Video extends JFrame
{
public static void main(String[] args) throws URISyntaxException {
final URI uri = new URI("https://www.youtube.com/watch?v=rl0YiZjTqpw");
class OpenUrlAction implements ActionListener
{
#Override public void actionPerformed(ActionEvent e) {
open(uri);
}
}
JFrame frame = new JFrame("Links");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(410, 400);
Container container = frame.getContentPane();
container.setLayout(new GridBagLayout());
JButton btnclickHereTo = new JButton();
btnclickHereTo.setText("<HTML> <FONT color=\"#000099\"><U>Click Here To Watch Video</U></FONT>");
btnclickHereTo.setHorizontalAlignment(SwingConstants.LEFT);
btnclickHereTo.setBorderPainted(false);
btnclickHereTo.setOpaque(false);
btnclickHereTo.setBackground(Color.WHITE);
btnclickHereTo.setToolTipText(uri.toString());
btnclickHereTo.addActionListener(new OpenUrlAction());
container.add(btnclickHereTo);
frame.setVisible(true);
}
private static void open(URI uri)
{
if (Desktop.isDesktopSupported())
{
try
{
Desktop.getDesktop().browse(uri);
}
catch (IOException e)
{ /* TODO: error handling */ }
}
else
{ /* TODO: error handling */ }
}
}
public void setVisible(boolean b) {
Why would you override the setVisible(...) method of your frame? There is no reason to do that.
I am an absolute beginner in coding
Start with something basic, like the example from the Swing tutorial on How to Make Frames.
Keep a reference to the tutorial link handy since it contains information and examples for all Swing basics.
I'm trying to implement a Barcode Reader for a CK71 ATEX Intermec Scanner. The operating system is Windows Embedded Handheld 6.5 and as JVM I'm using phoneME Personal Profile. I did install the DC_Java_WM6_Armv4i.cab (see the picture)
When I run the code below I get the following error: ITCScan failed to load. java.lang.UnsatisfiedLinkError: no ITCScan.dll in java.library.path
How can I fix this error? I've tried everything.
Note that before, I was using CreME JVM and everything was working fine. I gave up CreME when my 30 days evaluation version expired.
The content of the .lnk file (instead of myProject.MainClass are the real names of course):
255#"\Program Files\pMEA PP\bin\cvm.exe" "-Xopt:stdioPrefix=\My Documents,useConsole=false" -cp "\My Documents\Trasabilitate.jar;\My Documents\DataCollection.jar" myProject.MainClass
Here is the complete code:
/*
* BarcodeSample.java
*
* COPYRIGHT (c) 2004 INTERMEC TECHNOLOGIES CORPORATION, ALL RIGHTS RESERVED
*/
import java.awt.*;
import com.intermec.datacollection.*;
/**
* This sample demonstrates using the BarcodeReader class to
* read barcode data into a text field.
*/
public class BarcodeSample extends Frame implements BarcodeReadListener
{
BarcodeReader bcRdr;
TextField txtFieldData;
Button btnClose;
Label labelStatus;
public BarcodeSample(String aTitle)
{
super(aTitle);
initComponents();
try
{
bcRdr = new BarcodeReader();
bcRdr.addBarcodeReadListener(this);
// Starts asynchronous barcode read
bcRdr.threadedRead(true);
}
catch (BarcodeReaderException e)
{
System.out.println(e);
labelStatus.setText(e.getMessage());
//*****
//* Since m_labelStatus was not initialized with text,
//* doLayout() is required on some platforms in order
//* to show the new label text for the first setText()
//* call.
//*****
doLayout();
}
}
private void initComponents()
{
setLayout(new FlowLayout());
txtFieldData = new TextField(20);
add(txtFieldData);
btnClose = new Button("Close");
add(btnClose);
labelStatus = new Label();
add(labelStatus);
btnClose.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent e)
{
exitApp();
}
});
btnClose.addKeyListener(new java.awt.event.KeyListener() {
public void keyPressed(java.awt.event.KeyEvent e) {
if (e.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER)
{
exitApp();
}
}
public void keyReleased(java.awt.event.KeyEvent e) {}
public void keyTyped(java.awt.event.KeyEvent e) {}
});
}
/**
* This method is invoked when the BarcodeReadEvent occurs.
*/
public void barcodeRead(BarcodeReadEvent aBarcodeReadEvent)
{
/**
* Uses EventQueue.invokeLater to ensure the UI update
* executes on the AWT event dispatching thread.
*/
final String sNewData = aBarcodeReadEvent.strDataBuffer;
EventQueue.invokeLater(new Runnable() {
public void run() {
// Displays the scanned data in the text field
txtFieldData.setText(sNewData);
}
});
}
public void exitApp()
{
if (bcRdr != null)
bcRdr.dispose(); // Release system resources used by BarcodeReader
setVisible(false);
dispose(); // Dispose the frame
System.exit(0);
}
public static void main(String[] args)
{
final BarcodeSample asyncReader =
new BarcodeSample("Barcode Sample");
asyncReader.addWindowListener(new java.awt.event.WindowAdapter()
{
public void windowClosing(java.awt.event.WindowEvent e)
{
asyncReader.exitApp();
};
});
asyncReader.setVisible(true);
}
}
I finally got it to work. I found out what my java path was using the snippet here (I'll post it below in case something happens to the link):
Properties p = System.getProperties();
Enumeration keys = p.keys();
while (keys.hasMoreElements()) {
String key = (String)keys.nextElement();
String value = (String)p.get(key);
System.out.println(key + ": " + value);
}
And then I added my ITCScan.dll to the folder where java.library.path was set (in my case, \ProgramFiles\pMEA PP\bin.
I don't know if this is the most elegant solution, but it works. Hope it'll help somebody someday.
I'm trying to separate my Swing GUI from my actual code. In short, I want the user to kick off a process (based on the user's selections); in this case, the JFrame will no longer be needed.
What I couldn't figure out is how to share the user's selection from the GUI.class with the Main.class.
Do you have any advice for me?
Here's my code:
public class Main {
public static void main(String[] args) {
// Show GUI
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
GUI gui = new GUI(templates);
gui.setVisible(true);
}
});
// Kick off a process based on the user's selection
}
}
public class GUI extends JFrame {
private static final long serialVersionUID = 1L;
public GUI(Object[] objects) {
setTitle("GUI");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 350, 100);
setLocationRelativeTo(null);
JPanel cp = new JPanel();
cp.setBorder(new EmptyBorder(10, 10, 10, 10));
setContentPane(cp);
JLabel lbl = new JLabel("Selection:");
cp.add(lbl);
final JComboBox<String> comboBox = new JComboBox<String>(new String[] { "One", "Two", "Three" });
comboBox.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
dispose();
// Share the selected item with Main.class
}
});
cp.add(comboBox);
}
}
You could create an object to store the selection result and pass it in to the constructor of the GUI class. Set the selection result in that object before closing the UI and then your Main class could access the value:
public class SelectionResult {
private String selectionResult;
public void setSelectionResult(final String selectionResult) {
this.selectionResult = selectionResult;
}
public String getSelectionResult() {
return this.selectionResult;
}
}
Then, you could modify the GUI constructor like this:
private final SelectionResult selectionResult;
public GUI(Object[] objects, SelectionResult selectionResult) {
this.selectionResult = selectionResult;
...
Create a SelectionResult object in your Main class, and pass it to the constructor of the GUI class. In you GUI class ActionListener, you can then call the setSelectionResult() method with the selected value and that value will be available from the Main class.
You would need to add code to make your main method wait while you are waiting for the value to be set in the UI and then proceed with your logic based on the selection.
A Good way of doing this is use Callback mechanism.
Steps to follow:
create a callback interface
interface Callback {
void execute(Object result);
}
GUI class will implement Callback interface but without providing any implementation
Make GUI class abstract
abstract class GUI extends JFrame implements Callback
Now create an object of GUI class providing actual implementation of Callback interface
Here you can use Anonymous class
GUI gui = new GUI() {
#Override
public void execute(Object result) {
System.out.println("You have selected " + result);
}
};
You can pass any thing in execute() method of Callback.
comboBox.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
dispose();
// Share the selected item with Main.class
// Callback
execute(comboBox.getSelectedItem());
}
});
Here Main class is responsible for capturing the response of Callback that is directed by GUI class.
Here is the code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class Main {
public static void main(String[] args) {
// Show GUI
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
GUI gui = new GUI() {
#Override
public void execute(Object result) {
System.out.println("You have selected " + result);
}
};
gui.setVisible(true);
}
});
// Kick off a process based on the user's selection
}
}
interface Callback {
void execute(Object result);
}
abstract class GUI extends JFrame implements Callback {
private static final long serialVersionUID = 1L;
public GUI() {
setTitle("GUI");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 350, 100);
setLocationRelativeTo(null);
JPanel cp = new JPanel();
cp.setBorder(new EmptyBorder(10, 10, 10, 10));
setContentPane(cp);
JLabel lbl = new JLabel("Selection:");
cp.add(lbl);
final JComboBox comboBox = new JComboBox(new String[] { "One", "Two", "Three" });
comboBox.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
dispose();
// Share the selected item with Main.class
execute(comboBox.getSelectedItem());
}
});
cp.add(comboBox);
}
}
I borrowed a design that I found on stackoverflow to redirect console output to a GUI. It worked fine until I started reading from text files in my program. Now when I run the program using the GUI no output is displayed and the GUI freezes and then closes on its own eventually. Here is a slimmed down version of my GUI code:
public class GreenhouseControlsGUI {
public static class MyGui extends JFrame implements ActionListener {
// Start button
JButton Start = new JButton("Start");
/..................................../
/**
* The constructor.
*/
public MyGui(){
super("Greenhouse Controls");
/............................../
bottomPanel.setLayout(new FlowLayout());
bottomPanel.add(Start);
/............................../
getContentPane().add(holdAll, BorderLayout.CENTER);
Start.addActionListener(this);
/............................../
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if (e.getSource() == Start)
GreenhouseControls.startMeUp(); // Start program...
/............................../
}
public static void main(String[] args){
MyGui myApplication = new MyGui();
// redirect output to GUI
myApplication.redirectSystemStreams();
// Specify where will it appear on the screen:
myApplication.setLocation(10, 10);
myApplication.setSize(500, 300);
// Show it!
myApplication.setVisible(true);
}
private void updateTextArea(final String text) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
myText.append(text);
}
});
}
private void redirectSystemStreams() {
OutputStream out = new OutputStream() {
#Override
public void write(int b) throws IOException {
updateTextArea(String.valueOf((char) b));
}
#Override
public void write(byte[] b, int off, int len) throws IOException {
updateTextArea(new String(b, off, len));
}
#Override
public void write(byte[] b) throws IOException {
write(b, 0, b.length);
}
};
System.setOut(new PrintStream(out, true));
System.setErr(new PrintStream(out, true));
}
}
}
I'm pretty sure my problem starts with reading from the 'filename' path in the code below because I don't have this problem when I comment the 'filename' variable declaration out. I thought the methods to redirect console output to my GUI were only redirecting output.... Why is it screwing everything up when I read from a file? I am new to programming and I have probably overlooked something obvious, but I can't figure it out.
Here is the static startMeUp() method invoked inside the GUI class:
public static void startMeUp() {
try {
String option = "-f"; // (-f) to start program or (-d) to restore program
filename = "src/greenhouse/examples3.txt"; // read file from here
dumpFile = "dump.out"; // restore program from here
// if option is invalid invoke corresponding print statement
if ( !(option.equals("-f")) && !(option.equals("-d")) ) {
System.out.println("Invalid option");
printUsage();
}
GreenhouseControls gc = new GreenhouseControls(); // Create GreenhouseControls object
Restart restart = new Restart(0,filename, gc); // Create Restart object
Restore restore = new Restore(0,dumpFile); // Create Restore object
// if the option value is -f ...
if (option.equals("-f")) {
gc.addEvent(restart); // add new Restart object to addEvent()
}
gc.run();
// if the option value is -d ...
if (option.equals("-d")) {
gc.addEvent(restore); // add new Restore object to addEvent()
}
gc.run();
}catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Invalid number of parameters");
printUsage();
}
}
You need to invoke startMeUp in a new thread, because your console program is blocking the event dispatch thread. Like this:
new Thread () {
#Override public void run () {
GreenhouseControls.startMeUp();
}
}.start();
instead of just
GreenhouseControls.startMeUp();