- 浏览: 223701 次
- 性别:
- 来自: 西安
文章分类
最新评论
-
xiaoxiaowuxian1:
终于找到了,感谢~
大话设计模式(带目录完整版)PDF -
jiaoronggui:
一键安装,很方便
BitNami Redmine Stack -
s21109:
不错 很详细
java文件复制 -
ryanflyer:
白下了半天,无法使用
大话设计模式(带目录完整版)PDF -
xbgd:
sunwang810812 写道根本不能用啊用7z解压试试
大话设计模式(带目录完整版)PDF
[url][/url]
package components;
/*
* TableRenderDemo.java requires no other files.
*/
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
/**
* TableRenderDemo is just like TableDemo, except that it
* explicitly initializes column sizes and it uses a combo box
* as an editor for the Sport column.
*/
public class TableRenderDemo extends JPanel {
private boolean DEBUG = true;
public TableRenderDemo(){
super(new GridLayout(1,0));
JTable table = new JTable(new MyTableModel());
table.setPreferredScrollableViewportSize(new Dimension(500,70));
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Set up column sizes
initColumnSizes(table);
//Fiddle with the Sport column's cell editors/renderers.
setUpSportColumn(table,table.getColumnModel().getColumn(1));
//Add the scroll pane to this panel
add(scrollPane);
}
/*
* This method picks good column sizes.
* If all column heads are wider than the column's cells'
* contents, then you can just use column.sizeWidthToFit().
*/
private void initColumnSizes(JTable table){
MyTableModel model = (MyTableModel)table.getModel();
TableColumn column = null;
Component comp = null;
int headerWidth = 0;
int cellWidth = 0;
Object [] longValues = model.longValues;
TableCellRenderer headerRenderer =
table.getTableHeader().getDefaultRenderer();
for(int i = 0;i < table.getColumnCount();i++){
column = table.getColumnModel().getColumn(i);
comp = headerRenderer.getTableCellRendererComponent(
null, column.getHeaderValue(),
false, false, 0, 0);
headerWidth = comp.getPreferredSize().width;
comp = table.getDefaultRenderer(
model.getColumnClass(i)).getTableCellRendererComponent(
table, longValues[i],
false, false, 0, i);
cellWidth = comp.getPreferredSize().width;
if(DEBUG){
System.out.println("Initializing width of column "
+ i +"."
+"headerWidth = "+headerWidth
+"cellWidth = "+cellWidth);
}
column.setPreferredWidth(Math.max(headerWidth, cellWidth));
}
}
public void setUpSportColumn(JTable table,
TableColumn sportColumn){
JComboBox comboBox = new JComboBox();
comboBox.addItem("java");
comboBox.addItem(".NET");
comboBox.addItem("C++");
comboBox.addItem("JSP");
comboBox.addItem("PHP");
sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
//Set up tool tips for the sport cells.
DefaultTableCellRenderer renderer =
new DefaultTableCellRenderer();
renderer.setToolTipText("Click for combo box");
sportColumn.setCellRenderer(renderer);
}
class MyTableModel extends AbstractTableModel {
private String[] columnNames = {"姓名",
"专业",
"工龄",
"健在"};
private Object[][] data = {
{"吕振","java",new Integer(3),new Boolean(true)},
{"张沛",".NET",new Integer(4),new Boolean(true)},
{"岳飞","weapon",new Integer(100),new Boolean(false)},
{"张艺谋","film",new Integer(50),new Boolean(true)}
};
public final Object[] longValues = {"吕振","None of the above",
new Integer(29),Boolean.TRUE};
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
/*
* JTable uses this method to determine the default renderer/
* editor for each cell. If we didn't implement this method,
* then the last column would contain text ("true"/"false"),
* rather than a check box.
*/
public Class getColumnClass(int c) {
//实现了如果是boolean自动转成JCheckbox
/*需要自己的celleditor这么麻烦吧。jtable自动支持Jcheckbox,
* 只要覆盖tablemodel的getColumnClass返回一个boolean的class,
* jtable会自动画一个Jcheckbox给你,
* 你的value是true还是false直接读table里那个cell的值就可以*/
System.out.println("=============="+getValueAt(0, c).getClass());
return getValueAt(0, c).getClass();
}
/*
* Don't need to implement this method unless your table's
* editable.
*/
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
if (col < 2) {
return false;
} else {
return true;
}
}
/*
* Don't need to implement this method unless your table's
* data can change.
*/
public void setValueAt(Object value, int row, int col) {
if (DEBUG) {
System.out.println("Setting value at " + row + "," + col
+ " to " + value
+ " (an instance of "
+ value.getClass() + ")");
}
data[row][col] = value;
fireTableCellUpdated(row, col);
if (DEBUG) {
System.out.println("New value of data:");
printDebugData();
}
}
private void printDebugData() {
int numRows = getRowCount();
int numCols = getColumnCount();
for (int i=0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j=0; j < numCols; j++) {
System.out.print(" " + data[i][j]);
}
System.out.println();
}
System.out.println("--------------------------");
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("TableRenderDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
TableRenderDemo newContentPane = new TableRenderDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
package components;
/*
* TableRenderDemo.java requires no other files.
*/
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
/**
* TableRenderDemo is just like TableDemo, except that it
* explicitly initializes column sizes and it uses a combo box
* as an editor for the Sport column.
*/
public class TableRenderDemo extends JPanel {
private boolean DEBUG = true;
public TableRenderDemo(){
super(new GridLayout(1,0));
JTable table = new JTable(new MyTableModel());
table.setPreferredScrollableViewportSize(new Dimension(500,70));
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Set up column sizes
initColumnSizes(table);
//Fiddle with the Sport column's cell editors/renderers.
setUpSportColumn(table,table.getColumnModel().getColumn(1));
//Add the scroll pane to this panel
add(scrollPane);
}
/*
* This method picks good column sizes.
* If all column heads are wider than the column's cells'
* contents, then you can just use column.sizeWidthToFit().
*/
private void initColumnSizes(JTable table){
MyTableModel model = (MyTableModel)table.getModel();
TableColumn column = null;
Component comp = null;
int headerWidth = 0;
int cellWidth = 0;
Object [] longValues = model.longValues;
TableCellRenderer headerRenderer =
table.getTableHeader().getDefaultRenderer();
for(int i = 0;i < table.getColumnCount();i++){
column = table.getColumnModel().getColumn(i);
comp = headerRenderer.getTableCellRendererComponent(
null, column.getHeaderValue(),
false, false, 0, 0);
headerWidth = comp.getPreferredSize().width;
comp = table.getDefaultRenderer(
model.getColumnClass(i)).getTableCellRendererComponent(
table, longValues[i],
false, false, 0, i);
cellWidth = comp.getPreferredSize().width;
if(DEBUG){
System.out.println("Initializing width of column "
+ i +"."
+"headerWidth = "+headerWidth
+"cellWidth = "+cellWidth);
}
column.setPreferredWidth(Math.max(headerWidth, cellWidth));
}
}
public void setUpSportColumn(JTable table,
TableColumn sportColumn){
JComboBox comboBox = new JComboBox();
comboBox.addItem("java");
comboBox.addItem(".NET");
comboBox.addItem("C++");
comboBox.addItem("JSP");
comboBox.addItem("PHP");
sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
//Set up tool tips for the sport cells.
DefaultTableCellRenderer renderer =
new DefaultTableCellRenderer();
renderer.setToolTipText("Click for combo box");
sportColumn.setCellRenderer(renderer);
}
class MyTableModel extends AbstractTableModel {
private String[] columnNames = {"姓名",
"专业",
"工龄",
"健在"};
private Object[][] data = {
{"吕振","java",new Integer(3),new Boolean(true)},
{"张沛",".NET",new Integer(4),new Boolean(true)},
{"岳飞","weapon",new Integer(100),new Boolean(false)},
{"张艺谋","film",new Integer(50),new Boolean(true)}
};
public final Object[] longValues = {"吕振","None of the above",
new Integer(29),Boolean.TRUE};
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
/*
* JTable uses this method to determine the default renderer/
* editor for each cell. If we didn't implement this method,
* then the last column would contain text ("true"/"false"),
* rather than a check box.
*/
public Class getColumnClass(int c) {
//实现了如果是boolean自动转成JCheckbox
/*需要自己的celleditor这么麻烦吧。jtable自动支持Jcheckbox,
* 只要覆盖tablemodel的getColumnClass返回一个boolean的class,
* jtable会自动画一个Jcheckbox给你,
* 你的value是true还是false直接读table里那个cell的值就可以*/
System.out.println("=============="+getValueAt(0, c).getClass());
return getValueAt(0, c).getClass();
}
/*
* Don't need to implement this method unless your table's
* editable.
*/
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
if (col < 2) {
return false;
} else {
return true;
}
}
/*
* Don't need to implement this method unless your table's
* data can change.
*/
public void setValueAt(Object value, int row, int col) {
if (DEBUG) {
System.out.println("Setting value at " + row + "," + col
+ " to " + value
+ " (an instance of "
+ value.getClass() + ")");
}
data[row][col] = value;
fireTableCellUpdated(row, col);
if (DEBUG) {
System.out.println("New value of data:");
printDebugData();
}
}
private void printDebugData() {
int numRows = getRowCount();
int numCols = getColumnCount();
for (int i=0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j=0; j < numCols; j++) {
System.out.print(" " + data[i][j]);
}
System.out.println();
}
System.out.println("--------------------------");
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("TableRenderDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
TableRenderDemo newContentPane = new TableRenderDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
发表评论
-
WindowBuilder Pro
2011-06-26 22:14 2115http://www.eclipse.org/windowbu ... -
GUI框架
2011-04-26 22:55 871http://tikeswing.sourceforge.ne ... -
Java中用线程实现带有滚动效果的Label标签
2011-04-17 22:19 1545import java.awt.Graphics; impo ... -
用Swing实现的java气泡提示效果
2010-09-04 00:03 5447效果超炫,看附件截图 package monitor.ico ... -
Java Swing 进度条
2010-07-31 21:37 2114注:不是自己写的,转的。 参数Thread 就是做耗时任务的 ... -
Swing开发之JProgressBar篇
2010-07-31 21:31 16298JProgressBar是一个简单的 ... -
禁用swing窗体右上角关闭按钮
2010-06-29 22:05 1717其实还可以通过评比关闭按钮的事件来达到该效果 import ... -
Java进度条JProgressBar使用方法
2010-06-03 22:05 3052Java进度条JProgressBar使用方法 packag ... -
Swing创建单选按钮
2010-04-29 23:18 31751. import javax.swing.JFram ... -
swing 单选(互斥)按钮
2010-04-29 23:18 2438package org.moon.admin; import ... -
JTextPane禁止换行的解决办法
2010-04-28 22:50 1275解决办法,重写下列方法: Java: public b ... -
Swing 对JTextPane中字体颜色的设置
2010-04-09 20:52 2102import java.awt.BorderLayout; i ...
相关推荐
本篇将介绍一个Swing JTable组件的CheckBox管理类,名为`TableCheckboxManager`,它能够帮助开发者更方便地管理和操作这些复选框。 `TableCheckboxManager`通常负责以下任务: 1. **初始化复选框**:为JTable的某一...
在Java Swing中,`JTable` 是用于展示二维数据的组件,它允许用户查看和编辑数据。本示例探讨如何在`JTable`中添加`CheckBox`,并实现表头的`JCheckBox`全选联动功能。这在需要用户进行多选操作的界面中非常常见,...
在使用JTable时,我们需要将上述两个类组合起来使用。下面是一个简单的示例代码: ```java import java.awt.Dimension; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax....
在Java Swing中,`JTable`是一个非常重要的组件,用于展示二维数据表格。在这个特定的示例工程中,我们探讨了如何在`JTable`中使用自定义数据类型,特别是将Boolean类型的数据呈现为Checkbox的形式,同时根据...
在Java编程领域,Swing库是用于创建图形用户界面(GUI)的重要工具,而`JTable`是Swing提供的一种组件,它允许我们展示和操作二维数据。本篇将深入探讨`JTable`的详细使用,包括如何进行自定义渲染以及在单元格中...
在Java编程中,`JTable`是Swing库中的一个组件,用于展示表格数据。它在用户界面设计中非常常见,特别是在数据管理或数据显示的应用中。`JTable`不仅可以显示文本,还可以与其他组件如`JCheckBox`结合,以提供更丰富...
在给定的文件中,“checkbox.txt”可能包含了关于如何创建、使用和操作`JCheckBox`的代码示例或说明,而“JTable详细介绍.docx”则可能是对`JTable`组件的深入讲解,包括但不限于如何创建表格,设置数据模型,处理行...
Swing是在Java 1.2中引入的,它是一个更高级的GUI库,与AWT相比,Swing组件在所有平台上具有一致的外观和行为,因此被称为“轻量级”组件。Swing组件通常以J开头,如JFrame、JLabel和JButton。Swing不仅提供了AWT...
2. **java_Swing_(check,对话框,单选钮,复选框).pdf** - 这个文档可能专注于Swing中的特定组件,比如复选框(CheckBox)、单选钮(RadioButton)、对话框(Dialog)等,这些都是Swing中常见的交互元素。 3. **第十...
在Java Swing或JavaFX中,我们可以使用`setRenderer()`方法来自定义`JTable`中的Checkbox单元格渲染器,或者直接扩展`JCheckBox`类并重写`paintComponent()`方法。例如: ```java public class CustomCheckBox ...
3. **高级组件**: Swing还提供了如JTable、JTree、JTabbedPane等高级组件,使开发者能够轻松创建表格、树形视图和选项卡面板。 4. **模型-视图-控制器(MVC)架构**: Swing组件遵循MVC设计模式,使得代码结构更加...
在Java Swing库中,`JTable`是一个非常重要的组件,用于展示二维数据集,并允许用户进行交互。`JTable`可以被用作数据查看、编辑和管理的界面,广泛应用于各种桌面应用程序。在这个实例中,我们将深入理解`JTable`的...
Swing还包含了AWT的所有组件,并添加了许多新特性,如JTable、JTree和JTabbedPane等。 8. AWT的局限性: 由于AWT组件依赖于本地操作系统,因此它们的外观和行为可能会因平台而异。此外,由于AWT组件是重量级的,其...
Swing包含许多AWT组件的替代品,如`JFrame`、`JPanel`、`JButton`等,并且增加了更多的组件,如`JTable`、`JTree`、`JTabbedPane`等。 ### Swing的优势 - **可定制性**:Swing组件可以通过设置各种属性来改变外观和...
以上就是"Swing实例仿window"项目中的关键知识点,这些组件和效果结合使用,可以帮助开发者构建出与Windows系统界面类似的Java应用程序。通过熟练掌握这些Swing组件,你可以创建功能丰富、用户体验良好的桌面应用。
2. Swing:Swing是建立在AWT之上的一个更高级的GUI库,它提供了更多的组件和更好的跨平台一致性。Swing组件使用Java的轻量级组件模型,不依赖于本地操作系统,因此它们的外观可以更一致。Swing中的主要组件包括: -...
Swing是建立在AWT之上的更高级的GUI库,它提供了一组“轻量级”组件,这些组件不直接依赖于操作系统,因此具有更好的跨平台一致性。Swing组件通常比AWT组件功能更强大,外观也更加现代。 - **Swing组件**:Swing...
- 更丰富的组件:SWING扩展了AWT的组件库,添加了如JButton、JLabel、JTextField、JTextArea等,以及更多的组件,如JTable、JTree、JTabbedPane、JSplitPane等。 - 外观和感觉(LookAndFeel):SWING支持多种Look...
这些材料可能还会介绍一些高级主题,如自定义组件、Swing小组件(JComponent)的继承和扩展,以及如何使用JLayer进行高级特效。 美化包(可能包括第三方库如 Substance 或 JavaFX)则提供了预设的主题和样式,让你...
6. **组件集合**:Swing包含了许多AWT没有的组件,如JButton、JLabel、JTextArea、JComboBox等,以及更复杂的组件如JTree和JTable。 7. ** Nimbus Look and Feel**:Swing提供了一套跨平台的外观和感觉,允许开发者...