Test JTable
package vinh.swing;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.HeadlessException;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Toolkit;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Vector;
import javax.imageio.ImageIO;
import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SpringLayout;
import javax.swing.event.TableColumnModelEvent;
import javax.swing.event.TableColumnModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.text.TableView.TableCell;
import vinh.resource.ResourceUtil;
import vinh.swing.table.VTable;
public class VFrame2 extends JFrame {
private static final long serialVersionUID = 8105191055068057884L;
private SpringLayout springLayout = new SpringLayout();
private JPanel mainPanel = new JPanel(springLayout);
public VFrame2() throws HeadlessException {
// TODO Auto-generated constructor stub
super();
setLocationByPlatform(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setMinimumSize(new Dimension(600, 800));
JMenu menuXml = MenuUtil.getJMenuFromeResource("xml");
JMenuBar menuChinh = new JMenuBar();
menuChinh.add(menuXml);
setJMenuBar(menuChinh);
getContentPane().add(mainPanel, BorderLayout.CENTER);
JLabel labelSample = new JLabel("Parameter 01:");
mainPanel.add(labelSample);
springLayout.putConstraint(SpringLayout.NORTH, labelSample, 10, SpringLayout.NORTH, mainPanel);
springLayout.putConstraint(SpringLayout.WEST, labelSample, 10, SpringLayout.WEST, mainPanel);
labelSample.setToolTipText("<html><img src=\"" + ResourceUtil.baseURL + "image/checked.png"
+ "\"> <h1>Tooltip của Nguyễn Thành Vinh</h1></html> ");
JTextField jTextTest = new JTextField(20);
mainPanel.add(jTextTest);
springLayout.putConstraint(SpringLayout.BASELINE, jTextTest, 0, SpringLayout.BASELINE, labelSample);
springLayout.putConstraint(SpringLayout.WEST, jTextTest, 10, SpringLayout.EAST, labelSample);
JLabel labelSample02 = new JLabel("Parameter 02:");
mainPanel.add(labelSample02);
springLayout.putConstraint(SpringLayout.NORTH, labelSample02, 10, SpringLayout.SOUTH, labelSample);
springLayout.putConstraint(SpringLayout.WEST, labelSample02, 10, SpringLayout.WEST, mainPanel);
JTextField jTextTest02 = new JTextField(20);
mainPanel.add(jTextTest02);
springLayout.putConstraint(SpringLayout.BASELINE, jTextTest02, 0, SpringLayout.BASELINE, labelSample02);
springLayout.putConstraint(SpringLayout.WEST, jTextTest02, 10, SpringLayout.EAST, labelSample02);
ImageIcon iconpi;
try {
iconpi = new ImageIcon(ImageIO.read(ResourceUtil.getFileFromResource("image/checked.png")));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
iconpi = new ImageIcon();
}
JButton buttonTest = new JButton("Button Test", iconpi);
buttonTest.setVerticalTextPosition(AbstractButton.BOTTOM);
buttonTest.setHorizontalTextPosition(AbstractButton.CENTER);
buttonTest.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("Da kich button");
jTextTest.setText("vinh dep trai");
jTextTest.setEnabled(false);
buttonTest.setToolTipText("<html><img src=\"" + ResourceUtil.baseURL + "image/checked.png"
+ "\"> <h1>Click vào button test đi bạn, đéo tin thì kích đi</h1></html> ");
}
});
mainPanel.add(buttonTest);
springLayout.putConstraint(SpringLayout.NORTH, buttonTest, 10, SpringLayout.SOUTH, labelSample02);
springLayout.putConstraint(SpringLayout.WEST, buttonTest, 10, SpringLayout.WEST, mainPanel);
JTextArea textArea02 = new JTextArea("input here", 10, 10);
textArea02.setTabSize(3);
JScrollPane textArea01 = new JScrollPane(textArea02);
textArea02.setToolTipText("<html><img src=\"" + ResourceUtil.baseURL + "image/checked.png"
+ "\"> <h1>Click vào button test đi bạn, đéo tin thì kích đi</h1></html> ");
JButton buttonCopy = new JButton("Copy");
BorderLayout business01 = new BorderLayout();
JPanel business02 = new JPanel(business01);
business02.add(textArea01, BorderLayout.CENTER);
business02.add(buttonCopy, BorderLayout.LINE_END);
mainPanel.add(business02);
buttonCopy.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
StringSelection selection = new StringSelection(textArea02.getText());
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(selection, selection);
}
});
springLayout.putConstraint(SpringLayout.NORTH, business02, 10, SpringLayout.SOUTH, buttonTest);
springLayout.putConstraint(SpringLayout.WEST, business02, 10, SpringLayout.WEST, mainPanel);
springLayout.putConstraint(SpringLayout.EAST, business02, -10, SpringLayout.EAST, mainPanel);
ParameterModel vinhModel = new ParameterModel();
DefaultTableColumnModel columnModel = new DefaultTableColumnModel();
// columnModel.
JTable vinhTable = new JTable(vinhModel, null);
JScrollPane tabletemp = new JScrollPane(vinhTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
mainPanel.add(tabletemp);
springLayout.putConstraint(SpringLayout.NORTH, tabletemp, 10, SpringLayout.SOUTH, business02);
springLayout.putConstraint(SpringLayout.WEST, tabletemp, 10, SpringLayout.WEST, mainPanel);
JButton buttonAddRow = new JButton("add");
buttonAddRow.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
vinhModel.addData();
}
});
mainPanel.add(buttonAddRow);
springLayout.putConstraint(SpringLayout.WEST, buttonAddRow, 10, SpringLayout.EAST, tabletemp);
springLayout.putConstraint(SpringLayout.BASELINE, buttonAddRow, 10, SpringLayout.BASELINE, tabletemp);
JButton buttonRemoveRow = new JButton("remove");
buttonRemoveRow.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
vinhModel.removeRow();
}
});
mainPanel.add(buttonRemoveRow);
springLayout.putConstraint(SpringLayout.NORTH, buttonRemoveRow, 10, SpringLayout.SOUTH, buttonAddRow);
springLayout.putConstraint(SpringLayout.WEST, buttonRemoveRow, 10, SpringLayout.EAST, tabletemp);
pack();
}
public void showVFrame() {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
setVisible(true);
}
});
}
public class Parameter {
public String moTaDau;
public String tenThamSo;
public String defaultValue;
public String moTaSau;
public Parameter(String a1, String a2, String a3, String a4) {
moTaDau = a1;
tenThamSo = a2;
defaultValue = a3;
moTaSau = a4;
}
}
public class ParameterModel extends AbstractTableModel {
@Override
public String getColumnName(int column) {
switch (column) {
case 0:
return "Mô tả đầu";
case 1:
return "tên tham số";
case 2:
return "default value";
case 3:
return "Mô tả sau";
default:
return null;
}
}
public ArrayList<Parameter> data = new ArrayList<VFrame2.Parameter>();
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
switch (columnIndex) {
case 0:
data.get(rowIndex).moTaDau = (String) aValue;
break;
case 1:
data.get(rowIndex).tenThamSo = (String) aValue;
break;
case 2:
data.get(rowIndex).defaultValue = (String) aValue;
break;
case 3:
data.get(rowIndex).moTaSau = (String) aValue;
break;
default:
}
fireTableCellUpdated(rowIndex, columnIndex);
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
return true;
}
@Override
public int getRowCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public int getColumnCount() {
// TODO Auto-generated method stub
return 4;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
switch (columnIndex) {
case 0:
return data.get(rowIndex).moTaDau;
case 1:
return data.get(rowIndex).tenThamSo;
case 2:
return data.get(rowIndex).defaultValue;
case 3:
return data.get(rowIndex).moTaSau;
default:
return null;
}
}
public void addData(Parameter input) {
this.data.add(input);
fireTableRowsInserted(data.size(), data.size());
}
public void addData() {
this.data.add(new Parameter("", "", "", ""));
fireTableRowsInserted(data.size(), data.size());
}
public void removeRow() {
if (!data.isEmpty()) {
data.remove(data.size() - 1);
fireTableRowsDeleted(data.size(), data.size());
}
}
}
}
Không có nhận xét nào:
Đăng nhận xét