我想通过此函数将JScrollPane添加到JFrame createDatabaseViewGUI可以给我指导如何执行此操作,因此我可以将此jtable和jscrollpane添加到jframe并使其正常工作
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);
}
});
}
}
}
最佳答案
我可以猜到,您在这里遇到问题:rmic.createDatabaseViewGUI()
。此方法不能为void
。它必须返回值。
jFrame.getContentPane(rmic.createDatabaseViewGUI());
从documentation:
setContentPane(Container contentPane)
Sets the contentPane property.