我对Java还是个新手,对Gui还是个完全的新手。我无论如何也弄不清楚如何将字符串数组中的数据添加到Jtable中。我试着阅读oracle文档,但它对我的特殊情况没有意义。如果你们能给我指出正确的方向,我会很感激的。我也从一个文件检索我的数组数据。我不确定这是否有影响。
public class TemplateGui extends JFrame {
private JTable tableHotelSecurity, securityFlagsTable;
private final ButtonGroup buttonGroup = new ButtonGroup();
private JTextField textField;
private static String [] sortedRoles_Flags,finalFlagsArr,finalHSArr;
private static String finalFlags="",finalHS="",column;
public TemplateGui(){
super("Galaxy Template Generator V1.0");
getContentPane().setForeground(new Color(0, 0, 0));
getContentPane().setLayout(null);
getContentPane().setBackground(new Color(51, 51, 51));
JScrollPane scrollPane = new JScrollPane();
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setBounds(10, 170, 189, 186);
getContentPane().add(scrollPane);
JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane_1.setBounds(222, 170, 372, 186);
getContentPane().add(scrollPane_1);
//radio buttons
JRadioButton rdbtnNewRadioButton = new JRadioButton("Central User ");
rdbtnNewRadioButton.setFont(new Font("Tahoma", Font.BOLD, 11));
rdbtnNewRadioButton.setBackground(Color.WHITE);
buttonGroup.add(rdbtnNewRadioButton);
rdbtnNewRadioButton.setBounds(222, 75, 127, 36);
getContentPane().add(rdbtnNewRadioButton);
final JRadioButton rdbtnPropertyUser = new JRadioButton("Property User");
rdbtnPropertyUser.setFont(new Font("Tahoma", Font.BOLD, 11));
rdbtnPropertyUser.setBackground(Color.WHITE);
buttonGroup.add(rdbtnPropertyUser);
rdbtnPropertyUser.setBounds(222, 38, 127, 34);
getContentPane().add(rdbtnPropertyUser);
textField = new JTextField();
textField.setFont(new Font("Tahoma", Font.BOLD, 18));
textField.setBounds(10, 35, 53, 34);
getContentPane().add(textField);
textField.setColumns(10);
JLabel lblHotelSecurity = new JLabel("Hotel Security (H S)");
lblHotelSecurity.setHorizontalAlignment(SwingConstants.CENTER);
lblHotelSecurity.setFont(new Font("Tahoma", Font.BOLD, 13));
lblHotelSecurity.setBounds(10, 144, 189, 23);
lblHotelSecurity.setBackground(new Color(204, 204, 204));
lblHotelSecurity.setOpaque(true);
getContentPane().add(lblHotelSecurity);
JLabel label = new JLabel("Security Flags (S F)");
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setFont(new Font("Tahoma", Font.BOLD, 13));
label.setBounds(222, 144, 372, 23);
label.setBackground(new Color(204, 204, 204));
label.setOpaque(true);
getContentPane().add(label);
JLabel lblEnterTemplateCode = new JLabel("ENTER TEMPLATE CODE");
lblEnterTemplateCode.setForeground(new Color(255, 255, 255));
lblEnterTemplateCode.setFont(new Font("Tahoma", Font.BOLD, 14));
lblEnterTemplateCode.setBounds(10, 9, 175, 23);
getContentPane().add(lblEnterTemplateCode);
JLabel lblSelectUserRole = new JLabel("SELECT USER ROLE LEVEL");
lblSelectUserRole.setForeground(new Color(255, 255, 255));
lblSelectUserRole.setFont(new Font("Tahoma", Font.BOLD, 14));
lblSelectUserRole.setBounds(222, 13, 195, 14);
getContentPane().add(lblSelectUserRole);
//Submit button action
Button button = new Button("Generate Template");
button.setFont(new Font("Dialog", Font.BOLD, 12));
button.setBackground(new Color(102, 255, 102));
button.setForeground(Color.BLACK);
button.setBounds(467, 83, 127, 41);
getContentPane().add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Query excell = new Query();
//get template text
String template = textField.getText().toUpperCase();
System.out.println(template);
if(rdbtnPropertyUser.isSelected()){
try {
//property user was selected
excell.runProcess(1);
System.out.println("you selected Property user");
} catch (IOException e) {
System.out.println("Error occured on line 70 Interface Class");
}
}
else{
try {
//Central User was selected
excell.runProcess(2);
System.out.println("you selected central user");
} catch (IOException e) {
System.out.println("Error occured on line 79 Interface Class");
}
}
System.out.println("NOW WERE HERE");
//get static variables from Excel Query
for(int i = 0; i< Query.sortedGF.length; i++)
{
if(Query.sortedGF[i].contains(template)){
sortedRoles_Flags =Query.sortedGF[i].split(" ");
System.out.println("THIS RAN"+" :"+i);
break;
}
}
System.out.println("NOW WERE HERE 103 " +Query.securityFlags.length);
//add data to table
int j=0;
int sizeOfFlags = Query.securityFlags.length;
System.out.println("Size Of the Flags is:"+" "+sizeOfFlags);
System.out.println("Size Of the Flags is:"+" "+sortedRoles_Flags.length);
//Add HS to FinalHS Variable only if Yes
for(int i=0;i< sortedRoles_Flags.length-sizeOfFlags;i++)
{
if(sortedRoles_Flags[i].matches("Y|y|Y\?|\?Y|y\?|\?y"))
{
System.out.println("Hotel security:"+" "+sortedRoles_Flags[i]+" HS Added: "+Query.hotelSecurity[i]);
finalHS += Query.hotelSecurity[i]+" ";
System.out.println("Hotel security:"+" "+finalHS);
}
}
//add Security Flags to Final Flags
for(int i=(sortedRoles_Flags.length-sizeOfFlags);i< sortedRoles_Flags.length;i++)
{
finalFlags += Query.securityFlags[j]+": "+ sortedRoles_Flags[i]+" + ";
j++;
}
//Leave open just incase they would prefer a text file for template in which case we just write it
System.out.println(finalFlags);
System.out.println(finalHS);
//Convert to String Arrays in order to add to our JTable
finalFlagsArr= finalFlags.split("\+");
finalHSArr = finalHS.split(" ");
}
});
//content to be in the table
DefaultTableModel modelH = new DefaultTableModel();
tableHotelSecurity = new JTable(modelH);
scrollPane.setViewportView(tableHotelSecurity);
DefaultTableModel modelS = new DefaultTableModel();
securityFlagsTable = new JTable(modelS);
scrollPane_1.setViewportView(securityFlagsTable);
}
}
http://docs.oracle.com/javase/tutorial/uiswing/components/table.html
主要思想是,swing是建立在MVC架构的原则之上的。这意味着负责显示表的组件(JTable)和知道表中内容的组件(TableModel)是分开的。您需要以某种方式将模型传递给表。你可以使用DefaultTableModel,这就是你所做的,或者定义你自己的实现接口TableModel的模型。首先,需要将一个数组传递给DefaultTableModel的构造函数。对于后者,您需要实现在特定位置查询数据的方法,但只有在开箱即用的东西不能为您做这件事时,才值得考虑。
关于错误消息的边注:如果你对编程很认真,不要习惯坏模式。现在看来似乎更容易了,但出于某种原因,这样做是不明智的。它们通常不能很好地从一个小型项目扩展到一个由多人开发的完整项目。Stacktrace是你的朋友。使用日志记录器而不是sysout。从长远来看,如果你现在习惯了好的做法,对你来说会更容易。