Created
June 1, 2012 08:20
-
-
Save keating/2850249 to your computer and use it in GitHub Desktop.
Some helpers for Java Swing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.JDialog; | |
import javax.swing.JFrame; | |
import javax.swing.JTable; | |
import javax.swing.UIManager; | |
import javax.swing.UnsupportedLookAndFeelException; | |
import javax.swing.table.TableColumnModel; | |
/** | |
* Swing helper for column of JTable | |
* @author keating_andy_given | |
*/ | |
public class CSHelp { | |
/** | |
* 设置JTable的列宽 | |
*/ | |
public static void columnWidth(JTable table, int width) { | |
TableColumnModel columnModel = table.getColumnModel(); | |
for(int i = 0; i < columnModel.getColumnCount(); i ++){ | |
columnModel.getColumn(i).setPreferredWidth(width); | |
} | |
} | |
/** | |
* 设置JTable某一列的列宽 | |
* @param table | |
* @param width | |
*/ | |
public static void columnWidth(JTable table, int columnNumber, int width) { | |
table.getColumnModel() | |
.getColumn(columnNumber).setPreferredWidth(width); | |
} | |
/** | |
* 隐藏JTable第一列 | |
*/ | |
public static void hideFirstColumn(JTable table) { | |
table.removeColumn(table.getColumnModel().getColumn(0)); | |
} | |
/** | |
* 设置style | |
*/ | |
public static void style() { | |
JFrame.setDefaultLookAndFeelDecorated(true); | |
JDialog.setDefaultLookAndFeelDecorated(true); | |
try { | |
UIManager.setLookAndFeel(new com.sun.java.swing.plaf.windows.WindowsLookAndFeel());//windows样式 | |
} catch (UnsupportedLookAndFeelException ex) { | |
Logger.getLogger(CSHelp.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Vector; | |
import javax.swing.table.DefaultTableModel; | |
/** | |
* Model for JTable (cannot be edit) | |
* | |
* @author 王吉文 | |
*/ | |
public class MyTableModel extends DefaultTableModel { | |
private static final long serialVersionUID = 1L; | |
// public MyTableModel(Vector<Vector<String>> data, | |
// Vector<String> column) { | |
// super(data, column); | |
// } | |
public MyTableModel(Vector<Vector<Object>> data, | |
Vector<String> column) { | |
super(data, column); | |
} | |
/** | |
* 不可编辑 | |
*/ | |
@Override | |
public boolean isCellEditable(int row, int column) { | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import javax.swing.JFileChooser; | |
import javax.swing.JOptionPane; | |
import javax.swing.filechooser.FileNameExtensionFilter; | |
/** | |
* save file dialog | |
* | |
* @author keating_andy_given | |
*/ | |
public class SaveChooser extends JFileChooser { | |
/*-------------------类成员定义--------------------*/ | |
private static final long serialVersionUID = 1L; | |
/*-------------------构造方法--------------------*/ | |
public SaveChooser() { | |
super(); | |
this.setAcceptAllFileFilterUsed(false); | |
this.setFileFilter(new FileNameExtensionFilter("BMP(*.bmp)", "bmp")); | |
this.setFileFilter(new FileNameExtensionFilter("GIF(*.gif)", "gif")); | |
this.setFileFilter(new FileNameExtensionFilter("PNG(*.png)", "png")); | |
this.setFileFilter(new FileNameExtensionFilter("JPG(*.jpg)", "jpg")); | |
} | |
/*-------------------业务逻辑--------------------*/ | |
@Override | |
public void approveSelection() { | |
File file = this.getSelectedFile(); | |
if (file.exists()) { | |
int copy = JOptionPane.showConfirmDialog(null, "是否要覆盖当前文件?", "保存", | |
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); | |
if (copy == JOptionPane.YES_OPTION) | |
super.approveSelection(); | |
} else | |
super.approveSelection(); | |
} | |
/*-------------------GET&SET--------------------*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment