swing获取⽂本框内容_Swing使⽤JTable详解
步骤5:进⼀步理解TableModel步骤6:TableModel 与DAO结
步骤6:TableModel 与DAO结
步骤4:TableModel步骤5:进⼀步理解TableModel
步骤3:列宽步骤4:TableModel
步骤1:基本表格
步骤1:基本表格步骤2:JScrollPane
步骤2:JScrollPane步骤3:列宽
步骤10:选中指定⾏
步骤9:输⼊项验证步骤10:选中指定⾏
步骤7:TableSelectionModel步骤8:更新Table
步骤8:更新Table步骤9:输⼊项验证
合步骤7:TableSelectionModel
⽰例 11 :: 基本表格
基本表格
⽰例
显⽰⼀个Table需要两组数据
1. ⼀维数组: String[]columnNames 表⽰表格的标题
2. ⼆维数组: String[][] heros 表格中的内容
默认情况下,表格的标题是不会显⽰出来了,除⾮使⽤了JScrollPane
package gui;import java.awt.BorderLayout;import javax.swing.JFrame;import javax.swing.JTable;public class TestGUI {public static void main(String[] args) {JFrame f =new JFrame("LoL");f.setSize(400,300);f.setLocation(200,200);f.setLayout(new BorderLayout());// 表格上的titleString[] columnNames =new String[] {"id","name","hp","damage"};// 表格中的内容,是⼀个⼆维数组String[][] heros =new String[][] { {"1","盖伦","616","100"},{"2","提莫","512","102"}, {"3","奎因","832","200"} };JTable t =new JTable(heros, columnNames);f.add(t,
JScrollPane BorderLayout.CENTER);f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.setVi
sible(true);}}⽰例
⽰例 22 :: JScrollPane JScrollPane: 带滚动条的Panel
把table放进去就可以看到table的title
同样的把textarea放进去,并且textarea内容够长的话,就会看到滚动条
package gui;import java.awt.BorderLayout;import javax.swing.JFrame;import javax.swing.JScrollPane;import javax.swing.JTable;public class TestGUI {public static void main(Strin
g[] args) {JFrame f =new JFrame("LoL");f.setSize(400,300);f.setLocation(200,
200);f.setLayout(new BorderLayout());String[] columnNames =new String[] {"id","name","hp","damage"};String[][] heros =new String[][] { {"1","盖伦","616","100"},{"2","提莫","512","102"}, {"3","奎因","832","200"} };JTable t =new JTable(heros, columnNames);// 根据t 创建 JScrollPaneJScrollPane sp =new JScrollPane(t);//或则创建⼀个空的JScrollPane,再通过setViewportView把table放在JScrollPane 中// JScrollPane sp = new JScrollPane(t);// sp.setViewportView(t);// 把sp⽽⾮JTable加⼊到JFrame上,f.add(sp,
列宽
⽰例 33 :: 列宽
BorderLayout.CENTER);f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.setVisible(true);}}⽰例
设置列宽度
package gui;import java.awt.BorderLayout;import javax.swing.JFrame;import javax.swing.JScrollPane;import javax.swing.JTable;public class TestGUI {public static void main(String[] args) {JFrame f =new JFrame("LoL");f.setSize(400,300);f.setLocation(200,
200);f.setLayout(new BorderLayout());String[] columnNames =new String[] {"id","name","hp","damage"};String[][] heros =new String[][] { {"1","盖伦","616","100"},{"2","提莫","512","102"}, {"3","奎因","832","200"} };JTable t =new JTable(heros, columnNames);JScrollPane sp =new JScrollPane(t);// 设置列宽度t.getColumnModel().getColumn(0).setPreferredWidth(10);f.add(sp,
TableModel
⽰例 44 :: TableModel BorderLayout.CENTER);f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.setVisible(true);}}⽰例
⾸先说下TableModel的设计思想,在Model这种思想的指导下,数据和显⽰分离开来了。 ⽐如对于JTable⽽⾔,有数据部分,也有显⽰部分(⽐如列宽等信息)。 数据部分,专门做⼀个类,叫做TableModel,就⽤于存放要显⽰的数据。
使⽤TableModel的⽅式存放Table需要显⽰的数据
实现了接⼝TableModel
继承AbstractTableModel ,进⽽实现
HeroTableModel 继承
在HeroTableModel 中提供⼀个table显⽰需要的所有信息
1. getRowCount 返回⼀共有多少⾏
2. getColumnCount 返回⼀共有多少列
3. getColumnName 每⼀列的名字
4. isCellEditable 单元格是否可以修改
5. getValueAt 每⼀个单元格⾥的值
渲染第⼀个单元格的数据的时候,就会调⽤⽅法TabelModel的getValueAt(0,0) ,把返回值拿到并显⽰
当图形界⾯需要渲染
HeroTableModel.java
TestGUI.java
package gui;import javax.swing.table.AbstractTableModel;public class HeroTableModel extends AbstractTableModel {String[] columnNames =new String[] {"id","name","hp","damage"};String[][] heros =new String[][] { {"1","盖伦","616","100"},{"2","提莫", "512","102"}, {"3","奎因","832","200"} };// 返回⼀共有多少⾏public int getRowCount() {// TODO Auto-generated method stubreturn heros.length;}//
返回⼀共有多少列public int getColumnCount() {// TODO Auto-generated method stubreturn columnNames.length;}// 获取每⼀列的名称public String getColumnName(int columnIndex) {// TODO Auto-generated method stubreturn columnNames[columnIndex];}// 单元格是否可以修改public boolean isCellEditable(int rowIndex,int columnIndex) {return false;}// 每⼀个单元格⾥的值public Object getValueAt(int rowIndex,int columnIndex) {// TODO Auto-generated method stubreturn heros[rowIndex][columnIndex];}}package gui;import java.awt.BorderLayout;import javax.swing.JFrame;import javax.swing.JScrollPane;import javax.swing.JTable;public class TestGUI {public static void main(String[] args) {JFrame f =new JFrame("LoL");f.setSize(400,300);f.setLocation(200,200);f.setLayout(new BorderLayout());//创建⼀个TableModelHeroTableModel htm=new HeroTableModel();//根据 TableModel来创建 TableJTable t =new JTable(htm);JScrollPane sp =new JScrollPane(t);f.add(sp,
进⼀步理解TableModel
⽰例 55 :: 进⼀步理解TableModel BorderLayout.CENTER);f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.setVisible(true);}}⽰例
在使⽤TableModel之前,是使⽤String[] columnNames =。。。String[][] heros = 。。。JTable t =new JTable(heros, columnNames);
这样的风格创建⼀个JTable的
所以实际上调⽤的是如下的构造⽅法:JTable(Object[][] rowData, Object[] columnNames)
如图所⽰,在JTable的的源代码中,它就会根据rowData和columnNames去创建⼀个TableModel对象
TableModel 与DAO结合
⽰例 66 :: TableModel 与DAO结合
⽰例
通过TableModel与DAO结合显⽰数据库中Hero信息。
DAO使⽤HeroDAO
在TableModel中,使⽤从DAO返回的List作为TableModel的数据
只需要修改HeroTableModel,⽆需修改TestGUI。 这正好演绎了Model设计思想中的数据分离的好处,当只需要数据发⽣变化的时候,修改Model即可,界⾯GUI部分,不需要做任何改动
package gui;import java.util.List;import javax.swing.table.AbstractTableModel;import jdbc.HeroDAO;import charactor.Hero;public class HeroTableModel extends AbstractTableModel {String[] columnNames =new String[] {"id","name","hp","damage"};// 使⽤从DAO返回的List作为TableModel的数据public List<Hero> heros =new HeroDAO().list();// heros.size返回⼀共有多少⾏public int getRowCount() {// TODO Auto-generated method stubreturn heros.size();}public int getColumnCount() {// TODO Auto-generated method stubreturn columnNames.length;}public String getColumnName(int columnIndex) {// TODO Auto-generated method stubreturn
columnNames[columnIndex];}public boolean isCellEditable(int rowIndex,int columnIndex) {return false;}// 先通过(rowIndex)获取⾏对应的Hero对象// 然后根据columnIndex返回对应的属性public Object getValueAt(int rowIndex,int columnIndex) {Hero h =
<(rowIndex);if(0== columnIndex)return h.id;if(1== columnIndex)return h.name;if(2== columnIndex)return h.hp;if(3== columnIndex)return h.damage;return null;}}⽰例
TableSelectionModel
⽰例 77 :: TableSelectionModel
通过table可以获取⼀个 TableSelectionModel,专门⽤于监听jtable选中项的变化
package gui;import java.awt.BorderLayout;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.event.ListSelectionEvent;import
javax.swing.event.ListSelectionListener;import charactor.Hero;public class TestGUI {public static void main(String[] args) {JFrame f =new JFrame("LoL");f.setSize(400,300);f.setLocation(200,200);f.setLayout(new BorderLayout());final HeroTableModel htm =new HeroTableModel();final JTable t =new JTable(htm);// 准备⼀个Panel上⾯
放⼀个Label⽤于显⽰哪条被选中了JPanel p =new JPanel();final JLabel l =new JLabel("暂时未选中条⽬");p.add(l);JScrollPane sp =new JScrollPane(t);// 使⽤selection来监听table的哪个条⽬被选中t.getSelectionModel().addListSelectionListener(new ListSelectionListener() {// 当选择了某⼀⾏的时候触发该事件public void valueChanged(ListSelectionEvent e) {// 获取哪⼀⾏被选中了int row = t.getSelectedRow();// 根据选中的⾏,到HeroTableModel中获取对应的对象Hero h = (row);// 更新标签内容l.setText("当前选中的英雄是: "+ h.name);}});f.add(p, BorderLayout.NORTH);f.add(sp,
⽰例 88 :: 更新Table
更新Table BorderLayout.CENTER);f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.setVisible(true);}}⽰例
以新增数据到数据库中,然后更新Table为例
package gui;import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.event.ActionEvent;import
java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;importhtmlborder
javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.JTextField;import jdbc.HeroDAO;import charactor.Hero;public class TestGUI {public static void main(String[] args) {JFrame f =new JFrame("LoL");f.setSize(400,
300);f.setLocation(200,200);f.setLayout(new BorderLayout());final HeroTableModel htm =new HeroTableModel();final JTable t =new JTable(htm);// 增加⼀个 panel⽤于放置名称,⾎量输⼊框和增加按钮JPanel p =new JPanel();final JLabel lName =new JLabel("名称");final JTextField tfName =new JTextField("");final JLabel lHp =new JLabel("⾎量");final JTextField tfHp =new JTextField("");JButton bAdd =new JButton("增加");tfName.setPreferredSize(new Dimension(80,30));tfHp.setPreferredSize(new Dimension(80,
30));p.add(lName);p.add(tfName);p.add(lHp);p.add(tfHp);p.add(bAdd);// 为增加按钮添加监听bAdd.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {HeroDAO dao =new HeroDAO();// 根据输⼊框数据创建⼀个Hero对象Hero h = new Hero();h.name = Text();h.hp = Integer.Text());// 通过dao把该对象加⼊到数据库dao.add(h);// 通过dao更新tablemodel中的数据htm.heros = dao.list();// 调⽤JTable的updateUI,刷新界⾯。// 刷新界⾯的时候,会到tablemodel中去取最新的数据// 就能看到新加进去的数据了t.updateUI();}});JScrollPane sp =new JScrollPane(t);f.add(p, BorderLayout.NORTH);f.add(sp,
⽰例 99 :: 输⼊项验证
输⼊项验证BorderLayout.CENTER);f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.setVisi
ble(true);}}⽰例
“感觉上” 界⾯就卡住了。 这是不友好的⼈机交互⾏为。
如果⽤户输⼊的名称为空,或者⾎量不是⼩数,在提交数据的时候都会报错。“感觉上”
所以需要加上输⼊项的验证,如果输⼊的数据不合格,应该弹出对话框提⽰⽤户具体原因。
package gui;import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.event.ActionEvent;import
java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import
javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTable;import
javax.swing.JTextField;import jdbc.HeroDAO;import charactor.Hero;public class TestGUI {public static void main(String[] args) {final JFrame f =new JFrame("LoL");f.setSize(400,300);f.setLocation(200,200);f.setLayout(new BorderLayout());final HeroTableModel htm = new HeroTableModel();final JTable t =new JTable(htm);JPanel p =new JPanel();final JLabel lName =new JLabel("名称");final JTextField tfName =new JTextField("");final JLabel lHp =new JLabel("⾎量");final JTextField tfHp =new JTextField("");JButton bAdd = new JButton("增加");tfName.setPreferredSize(new Dimension(80,30));tfHp.setPreferredSize(new Dimension(80,
30));p.add(lName);p.add(tfName);p.add(lHp);p.add(tfHp);p.add(bAdd);bAdd.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {HeroDAO dao =new HeroDAO();Hero h =new Hero();String name = Text();// 通过name长度判断名称是否为空if
(name.length() ==0) {// 弹出对话框提⽰⽤户JOptionPane.showMessageDialog(f,"名称不能为空");// 名称输⼊框获取焦
点abFocus();return;}String hp = Text().trim();try{// 把hp转换为浮点型,如果出现异常NumberFormatException表⽰不是浮点型格式Float.parseFloat(hp);}catch(NumberFormatException e1) {JOptionPane.showMessageDialog(f,"⾎量只能是⼩数
");abFocus();return;}h.name = name;h.hp = Float.parseFloat(hp);dao.add(h);htm.heros = dao.list();t.updateUI();}});JScrollPane sp = new JScrollPane(t);f.add(p, BorderLayout.NORTH);f.add(sp,
10 :: 选中指定⾏
选中指定⾏
⽰例 10
BorderLayout.CENTER);f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.setVisible(true);}}⽰例
1. table初始化后,应该默认选中第⼀⾏
2. 增加数据后,也应该选中新增的这⼀条
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论