Swing Q & A

zhaozj2021-02-16  51

Swing Q & A

Translation: nxyc_twz@163.com

Where can I find information suitable for beginners?

You can refer to the Swing page of Java Guide: http://java.sun.com/docs/books/tutorial/uiswing/

Why can't I get focus when I entered a cell?

When you click on a cell, it gets the focus; but when moving the cursor to the cell, it does not get the focus. In J2SE 1.4, an attribute called SurrendersFocusonkeyStroke is added to solve this problem and is maintained backward compatible.

Reference: http://java.sun.com/j2se/1.4/docs/guide/swing/swingchanges.html#jtable

How do I set the cell line, column color?

Each cell is built using the DefaultTableCellrenderer, and you can make specific modifications through the subclass of this class.

The following code demonstrates how to implement it. It has so many more expansion, so that mycellrendere can include information such as setting rows, columns, borders, and text settings.

Import javax.swing. *; import javax.swing.table. *; import java.awt.component; import java.awt.color;

Class TableTest Extends Jframe {

Taketest () {

Final mycellrenderer mcr = new mycellrenderer ();

JTable MyTable = New JTABLE (New MyTableModel ()) {public TableCellrenderer getcellrenderer (int Row, int column) {return mcr;}}

GetContentPane (). add (new jscrollpane (mytable); setsize (640, 480); show ();}

Public static void main (string args []) {

New tabletest ();}}

Class MyTableModel Extends AbstractTableModel {

String [] columnNames = {"name", "age", "sex"}; string [] [] columndata = {{"bob", "23", "m"}, {"claire", "99", "F"}, {"SPOT", "7", "f"}, {"phil", "69", "m"},}

public Object getValueAt (int row, int col) {return columnData [row] [col];} public String getColumnName (int col) {return columnNames [col];} public int getColumnCount () {return columnNames.length;} public int Gtrowcount () {return columndata.length;}}

Class mycellrenderer extends defaulttablecellrenderer {

Public Component GetTableCellrenderercomponent (JTable Table, Object Value, Boolean ISSelected, Boolean Hasfocus, Int Row, Int Column) {

// Get the default settings Component Cell = Super.getTableCellrenderComponent (Table, Value, ISSelected, Hasfocus, Row, Column);

IF (row% 2 == 0) cell.setBackground; Else Cell.setBackground (color.gray);

IF (Column% 2 == 0) Cell.SetBackground; Color.green

Return Cell;}}}

How do I create a group of edlies in the table?

Check the file: [RowHeadeRexample.java]

This solution is used to create a component in the JScrollPane line. This component adds JLIST to the line.

Jlist has: - listModel; define list data - RowHeaderRenderer; Define the sensation of each cell

Which form mode should I choose?

The table model is provided: javax.swing.table.tablemodel (interface) javax.swing.Table.AbstractTableModel (TableModel class) javax.swing.Table.defaultTableModel (inherited from AbstractTableModel class)

Javax.swing.table.tableModel

This interface provides: Listener registration, unit value access, column name, list storage, and creation of a table model basis.

The main advantage of implementing the TableModel interface is that the definition of the method can be customized and the internal data description can be expanded to implement variable applications.

Javax.swing.Table.AbstractTableModel

AbstractTableModel is a simple way to use Table shape data, just simply by overloading three methods getValueat, getColumncount, and getRowcount.

The AbstractTable model also provides event processing, such as FireTableDataChanged, FireRowsUpdated, etc. ...

AbstractTableModel is built on top of TableModel, and the internal description of its data can be static (saving storage) or dynamic, depending on the specific application.

The specific content can be obtained by using the buffer mechanism GetValueat method, and the performance of the display and scrolling can be greatly improved.

Javax.swing.table.defaulttablemodel

If the parameters are not specified in the JTable constructor, it will make the default table mode.

The DEFAULTTABLEMODELS internal data description is a vector system. If the data is not a form of form, this is a good method.

Although data is set, it is performed, and the display should be the same as the data buffer. If the amount of data is large or has buffered (such as a database), the copies of the buffered data may be an inappropriate selection.

A suggestion using DefaultTableModel is to pay attention to its volatility. The implementation of SetValueat and its rows / column modifications include all variable data after insertion, add, delete, and table build.

Other recommendations are to have a good design.

How to ban users from moving their heads or reset columns?

To limit the list, use table.gettableHeader (). SetReorderingAllowed (false);

To limit the column reset size, use Table.getTableHeader (). SetResizingAllowed (false); to limit individual columns to reset size Table.getColumnModel (). GetColumn (index) .setresizable (false / true);

Note: These do not apply to the previous version of J2SE 1.3.

(To be renewed ...)

转载请注明原文地址:https://www.9cbs.com/read-19919.html

New Post(0)