Reputation: 19
I want to add the JScrollPane to the JFrame through this function createDatabaseViewGUI can any give me guide how to do this so i could add this jtable and jscrollpane to the jframe and get it working
package schoolclient;
import java.awt.Dimension;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import schoolserver.SchoolInterface;
public class RMIClient extends JFrame {
SchoolInterface sii = null;
Object data = null;
public static void main(String[] args) {
RMIClient rmic = new RMIClient();
rmic.getSchoolInstacne();
//rmic.createDatabaseViewGUI();
JFrame jFrame = new JFrame();
jFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
jFrame.setVisible(true);
jFrame.setSize(270, 320);
jFrame.setContentPane(rmic.createDatabaseViewGUI());
//Object[] o = rmic.getDatabaseToList();
//for(Object o : rmic.getDatabaseToList())
//System.out.println(o);
}
public void getSchoolInstacne() {
try {
sii = (SchoolInterface) Naming.lookup("SchoolServer");
}
catch (RemoteException | MalformedURLException | NotBoundException e) {
e.printStackTrace();
}
}
public ArrayList getDatabaseToList() {
ArrayList<Object> data = null;
try {
data = sii.getDatabsesName();
}
catch (Exception e) {
e.printStackTrace();
}
return data;
}
public void createDatabaseViewGUI() {
Object[] x = new Object[10];
int i = 0;
for(Object o : getDatabaseToList())
x[i++] = o;
DefaultTableModel defaultTable = new DefaultTableModel();
defaultTable.addColumn("Databses Names", x);
JTable jTable = new JTable(defaultTable);
jTable.setPreferredScrollableViewportSize(new Dimension(250, 350));
jTable.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(jTable);
jTable.getColumnModel().getColumn(0).setPreferredWidth(150);
int[] selection = jTable.getSelectedRows();
for (int j = 0; j < selection.length; j++) {
selection[j] = jTable.convertRowIndexToModel(selection[j]);
super.add(scrollPane);
jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
ListSelectionModel lsm = jTable.getSelectionModel();
lsm.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
ListSelectionModel lsm = (ListSelectionModel)e.getSource();
int row = jTable.getSelectedRow();
data = (Object)jTable.getValueAt(row, 1);
}
});
}
}
}
Upvotes: 0
Views: 178
Reputation: 19
package schoolclient;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import schoolserver.SchoolInterface;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
public class RMIClient1 extends JFrame {
private JPanel contentPane;
private JTable table;
SchoolInterface sii = null;
String data = null;
/**
* Launch the application.
*/
public static void main(String[] args) {
RMIClient1 frame = new RMIClient1();
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame.setResizable(false);
frame.setVisible(true);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Couldn't able to load GUI.");
//e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public RMIClient1() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 250, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
this.getSchoolInstacne();
table = new JTable(createView()){
public boolean isCellEditable(int row, int column) {
return false;
}
};
this.tableValues(table);
scrollPane.setViewportView(table);
}
public void getSchoolInstacne() {
try {
sii = (SchoolInterface) Naming.lookup("SchoolServer");
}
catch (RemoteException | MalformedURLException | NotBoundException e) {
JOptionPane.showMessageDialog(null, "Error in connecting to Server");
//e.printStackTrace();
}
}
public ArrayList getDatabaseToList() {
ArrayList<Object> data = null;
try {
data = sii.getDatabsesName();
}
catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Error in SQL");
//e.printStackTrace();
}
catch (RemoteException e) {
JOptionPane.showMessageDialog(null, "Error in connecting to Server");
//e.printStackTrace();
}
return data;
}
Object[] createArray() {
Object[] x = new Object[10];
int i = 0;
for(Object o : getDatabaseToList())
x[i++] = o;
return x;
}
DefaultTableModel createView() {
DefaultTableModel defaultTable = new DefaultTableModel();
defaultTable.addColumn("Databses", createArray());
return defaultTable;
}
public void tableValues(JTable table) {
table.getColumnModel().getColumn(0).setPreferredWidth(150);
int[] selection = table.getSelectedRows();
for (int j = 0; j < selection.length; j++) {
selection[j] = table.convertRowIndexToModel(selection[j]);
}
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
ListSelectionModel lsm = table.getSelectionModel();
lsm.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
// TODO Auto-generated method stub
if(e.getValueIsAdjusting()){
ListSelectionModel lsm = (ListSelectionModel)e.getSource();
int row = table.getSelectedRow();
data = (String) table.getValueAt(row, 0);
lsm.setValueIsAdjusting(false);
SchoolClientGUI frame = new SchoolClientGUI(data);
frame.setVisible(true);
}
}
});
}
/*public void setOpen(String data) {
SchoolClientGUI frame = new SchoolClientGUI(data);
frame.setVisible(true);
}
*/
}
Upvotes: 0
Reputation: 2222
I can guess, you problem here: rmic.createDatabaseViewGUI()
. This method can not be void
. It must return value.
jFrame.getContentPane(rmic.createDatabaseViewGUI());
From documentation:
setContentPane(Container contentPane)
Sets the contentPane property.
Upvotes: 1