Reputation: 14189
I would create an application server/client with a gui in java but I have not clear how I can organize the class. I created the application gui:
here it's the code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Collection;
public class AziendaGUI implements ActionListener {
private JButton view_list;
private JButton save_list;
private JTextArea text_area;
private JScrollPane scrollpane;
private JPanel pane;
private JFrame frame;
private GridBagLayout grid;
private Azienda company;
public AziendaGUI() {
company = new Azienda();
frame = new JFrame("Immobiliari s.p.a");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
view_list = new JButton("View Property");
view_list.setActionCommand("view_list");
view_list.addActionListener(this);
save_list = new JButton("Save List");
save_list.setActionCommand("save_list");
save_list.addActionListener(this);
text_area = new JTextArea();
scrollpane = new JScrollPane(text_area);
scrollpane.setPreferredSize(new Dimension(250,350));
grid = new GridBagLayout();
pane = new JPanel(grid);
/* Set Constraints view_list button */
grid.setConstraints(view_list, new GridBagConstraints(0,0,1,1,0.0,0.0,GridBagConstraints.WEST,GridBagConstraints.NONE,new Insets(5,5,5,5),0,0));
pane.add(view_list);
/* Set Constraints save_list button */
grid.setConstraints(save_list,new GridBagConstraints(1,0,1,1,0.0,0.0,GridBagConstraints.EAST,GridBagConstraints.NONE,new Insets(5,5,5,5),0,0));
pane.add(save_list);
/* Set Constraint text area */
grid.setConstraints(scrollpane, new GridBagConstraints(0,1,2,1,0.0,0.0,GridBagConstraints.CENTER,GridBagConstraints.NONE,new Insets(5,5,5,5),0,0));
pane.add(scrollpane);
frame.setLayout(new FlowLayout());
frame.add(pane);
frame.pack();
frame.setVisible(true);
}
private void viewList(Collection<Immobile> list){
text_area.setText(""); //Evita che venga ripetuto tutto il contenuto
for(Immobile imb : list){
text_area.append(imb.toString()+"\n");
}
}
private void store(){
String file_name = JOptionPane.showInputDialog("Inserisci il nome del file");
company.store(file_name);
}
@Override
public void actionPerformed(ActionEvent e){
String s = e.getActionCommand();
if(s.equals("view_list")){
viewList(company.getImmobili());
}
if(s.equals("save_list")){
store();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){@Override
public void run(){new AziendaGUI();}});
}
}
and now this application should works as a server so I have to implement the ServerSocket
constructor with all the methods as explained here Reading from and Writing to a Socket
My question is: where I should implement the server.in the same class AziendaGUI ora I have to create another class and call it in the main of AziendaGUI?
Upvotes: 1
Views: 4602
Reputation: 9582
Have a Main class, a Server class, a Client class, and a GUI class. Then you give your Server, and client classes a reference to your GUI class so they can update the GUI when neccessary.
Here is some example code of how a server could look.
public class AziendaGUI {
// GUI objects
private JFrame someWindow;
public AziendaGUI() {
// create and display the GUI
}
// export public methods for various GUI updates you want your
// server class to perform
public someGUIupdate(String s) {
// update the GUI
// for example, add some text to a textbox
// keep in-mind that this code is being run on the
// server thread and NOT the event dispatch thread
// so you need to consider concurrency issues
// you will need to use either SwingUtilities.invokeLater()
// or synchronized()
}
}
public class Server {
private AziendaGUI gui;
public Server(AziendaGUI gui) {
this.gui = gui;
}
public start() {
// start server threads
// when you want to update the GUI
gui.someGUIupdate("hello world");
// these calls will probably be in other methods in your server class
// that do the actual IO handling
}
}
public class Main {
Main() {
// create and display GUI
AziendaGUI gui = new AziendaGUI();
// create and start server
Server s = new Server(gui);
s.start();
}
public static void main(String args[]) {
Main m = new Main();
}
}
Most people would probably put these classes in separate files.
Again, I would like to point-out that the GUI class is being accessed by multiple threads so you must use some form of concurrency control.
Upvotes: 2
Reputation: 2600
In most cases the Server and the Client are completely different Processes, that do not run within one Process... they usually even run on different machines. So this seperation should also be visible in your applications design. In the minimum you will need two classes, with their own main()-method so that you can start a server-process and a client-process.
public Class Server {
public static void main(...) {
//Start a Thread that opens ServerSocket and listen for requests.
}
}
public Class Client {
public static void main(...) {
//Start your GUI and conect to the Server
}
}
There is one fundamental design-guide-line called: "Seperation of Concern". That means, put everything, that belongs to one thing in one code-unit and do not mix things that have a different nature or a different behavior in the same code unit. Your client and your server have different natures and different behaviours, so they need to be in different classes. This design makes it much easier to understand the code in the end...
Try to think in abstract categories and create your classes from these categories.
Upvotes: 0