private static final String[] NAMES = { "Integer numbers", "Real numbers", "Other tokens" }
增加这个,就能增加列,自己琢磨
import java.awt.*
import java.awt.event.*
import java.io.File
import java.text.*
import java.util.*
import java.util.concurrent.ConcurrentHashMap
import javax.swing.*
import javax.swing.table.*
public class TokenCategorizer extends JFrame
{
private static final long serialVersionUID = 1L
private static final String FILE_NAME = "test.txt"
private static final String[] NAMES = { "Integer numbers", "Real numbers", "Other tokens" }
private static String[][] data = null
private static JList jList = null
private static JTable tableView = null
private static LinkedList<String[]> list = new LinkedList<String[]> ()
private static String[] strs = new String[NAMES.length]
private static Vector<SupportedLaF> lafVector = new Vector<SupportedLaF> ()
private static Map<Integer, Vector<String>> tabelDataMap = new ConcurrentHashMap<Integer, Vector<String>> ()
private static final String PLASTIC3D = "com.jgoodies.looks.plastic.Plastic3DLookAndFeel"
private static final String PLASTIC = "com.jgoodies.looks.plastic.PlasticLookAndFeel"
private static final String PLASTXP = "com.jgoodies.looks.plastic.PlasticXPLookAndFeel"
public TokenCategorizer ( String title )
{
UIManager.installLookAndFeel ("Plastic3D", PLASTIC3D)
UIManager.installLookAndFeel ("Plastic", PLASTIC)
UIManager.installLookAndFeel ("PlasticXP", PLASTXP)
UIManager.LookAndFeelInfo[] installedLafs = UIManager.getInstalledLookAndFeels ()
for ( UIManager.LookAndFeelInfo lafInfo : installedLafs )
{
try
{
Class<?> lnfClass = Class.forName (lafInfo.getClassName ())
LookAndFeel laf = (LookAndFeel) ( lnfClass.newInstance () )
if (laf.isSupportedLookAndFeel ())
{
String name = lafInfo.getName ()
lafVector.add (new SupportedLaF (name, laf))
}
}
catch (Exception e)
{
continue
}
}
this.setTitle (title)
}
private static class SupportedLaF
{
String name
LookAndFeel laf
SupportedLaF ( String name, LookAndFeel laf )
{
this.name = name
this.laf = laf
}
public String toString ()
{
return name
}
}
private static void createTable ( TokenCategorizer tc )
{
TableModel dataModel = new AbstractTableModel ()
{
private static final long serialVersionUID = 1L
public int getColumnCount ()
{
return NAMES.length
}
public int getRowCount ()
{
return data[0].length
}
public Object getValueAt ( int row, int col )
{
return data[col][row]
}
public String getColumnName ( int column )
{
return NAMES[column]
}
}
tableView = new JTable (dataModel)
tableView.setSelectionMode (ListSelectionModel.SINGLE_SELECTION)
tableView.setRowSelectionInterval (0, 0)
initTableData (tableView, list, strs)
tableView.addMouseListener (new MouseAdapter ()
{
@Override
public void mouseClicked ( MouseEvent e )
{
initTableData (tableView, list, strs)
}
})
JScrollPane scrollpane = new JScrollPane (tableView)
scrollpane.setPreferredSize (new Dimension (500, 400))
tc.add (scrollpane)
}
private static void initTableData ( final JTable tableView, final LinkedList<String[]> list, final String[] strs )
{
try
{
list.clear ()
int row = tableView.getSelectedRow ()
Vector<String> v = null
if (null != ( v = tabelDataMap.get (row) ))
{
jList.setListData (v)
}
else
{
for ( int j = 0 j < NAMES.length j++ )
{
strs[j] = data[j][row]
}
permutations (list, NAMES.length, strs, 0, -1)
v = new Vector<String> ()
for ( String[] strings : list )
{
v.add (Arrays.toString (strings).replaceAll ("[\\[\\]]", ""))
}
jList.setListData (v)
tabelDataMap.put (row, v)
}
}
catch (Exception e)
{}
}
private static void createList ( TokenCategorizer tc )
{
ListModel dataModel = new AbstractListModel ()
{
private static final long serialVersionUID = 1L
@Override
public int getSize ()
{
return NAMES.length
}
@Override
public Object getElementAt ( int index )
{
return NAMES[index]
}
}
jList = new JList (dataModel)
jList.addMouseListener (new MouseAdapter()
{
@Override
public void mouseClicked ( MouseEvent e )
{
System.out.println (jList.getSelectedValue ())
}
})
}
private static void initFrame ( TokenCategorizer tc )
{
tc.setLayout (new FlowLayout (FlowLayout.LEFT))
tc.setSize (800, 400)
tc.setLocationRelativeTo (null)
tc.pack ()
tc.setResizable (false)
tc.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE)
tc.setVisible (true)
}
private static LinkedList<String[]> permutations ( LinkedList<String[]> list, int count, String[] array, int ind,
int start, int... indexs )
{
start++
if (start > count - 1)
{
return null
}
if (start == 0)
{
indexs = new int[array.length]
}
for ( indexs[start] = 0 indexs[start] < array.length indexs[start]++ )
{
permutations (list, count, array, 0, start, indexs)
if (start == count - 1)
{
String[] temp = new String[count]
for ( int i = count - 1 i >= 0 i-- )
{
temp[start - i] = array[indexs[start - i]]
}
boolean flag = true
L: for ( int i = 0 i < temp.length i++ )
{
for ( int j = i + 1 j < temp.length j++ )
{
if (temp[i].equals (temp[j]))
{
flag = false
break L
}
}
}
if (flag)
{
list.add (temp)
}
}
}
return list
}
private static void initOthers ( final TokenCategorizer tc )
{
JPanel jPanel = new JPanel ()
BoxLayout boxLayout = new BoxLayout (jPanel, BoxLayout.Y_AXIS)
jPanel.setLayout (boxLayout)
jPanel.setPreferredSize (new Dimension (300, 400))
JScrollPane scrollpane = new JScrollPane (jList)
scrollpane.setPreferredSize (new Dimension (300, 400))
jPanel.add (scrollpane)
final JFileChooser chooser = new JFileChooser ()
JPanel bottomPanel = new JPanel ()
JButton jButton = new JButton ("Import File Data")
jButton.addActionListener (new ActionListener ()
{
@Override
public void actionPerformed ( ActionEvent e )
{
int returnVal = chooser.showOpenDialog (tc)
if (returnVal == JFileChooser.APPROVE_OPTION)
{
File file = chooser.getSelectedFile ()
loadFileData (file.getPath ())
SwingUtilities.updateComponentTreeUI (tc)
tabelDataMap.clear ()
tableView.setRowSelectionInterval (0, 0)
initTableData (tableView, list, strs)
}
}
})
bottomPanel.setLayout (new FlowLayout (FlowLayout.LEFT))
final JComboBox jcb = new JComboBox (lafVector)
jcb.addActionListener (new ActionListener ()
{
@Override
public void actionPerformed ( ActionEvent e )
{
SupportedLaF supportedLaF = ( (SupportedLaF) jcb.getSelectedItem () )
LookAndFeel laf = supportedLaF.laf
try
{
UIManager.setLookAndFeel (laf)
SwingUtilities.updateComponentTreeUI (tc)
if (null != chooser)
{
SwingUtilities.updateComponentTreeUI (chooser)
}
}
catch (UnsupportedLookAndFeelException exc)
{
( (DefaultComboBoxModel) jcb.getModel () ).removeElement (supportedLaF)
}
}
})
bottomPanel.add (jcb)
bottomPanel.add (jButton)
jPanel.add (bottomPanel)
tc.add (jPanel)
}
private static void loadFileData ( String fileName )
{
Locale locale = new Locale ("en", "US")
Scanner scanner = null
String result = ""
String line = null
LinkedList<String> listInt = new LinkedList<String> ()
LinkedList<String> listDouble = new LinkedList<String> ()
LinkedList<String> listOthers = new LinkedList<String> ()
try
{
scanner = new Scanner (new File (fileName)).useDelimiter ("[\\s\r\n\t\f]+")
while (scanner.hasNext ())
{
line = scanner.next ()
result += line + " "
if (line.matches ("^(\\+|\\-)?[\\d,]+$"))
{
NumberFormat nf = NumberFormat.getInstance (locale)
listInt.add (nf.parse (line).toString ())
}
else if (line.matches ("^[\\dE\\.\\+\\-]+$"))
{
NumberFormat f = NumberFormat.getInstance (locale)
if (f instanceof DecimalFormat)
{
( (DecimalFormat) f ).setDecimalSeparatorAlwaysShown (true)
}
listDouble.add (f.parse (line).toString ())
}
else
{
listOthers.add (line)
}
}
}
catch (Exception e)
{}
scanner.close ()
int max =
listInt.size () > listDouble.size () ? listInt.size () > listOthers.size () ? listInt.size ()
: listOthers.size () : listDouble.size () > listOthers.size () ? listDouble.size ()
: listOthers.size ()
int size1 = listInt.size ()
for ( int i = size1 i < max i++ )
{
listInt.add (" ")
}
int size2 = listDouble.size ()
for ( int i = size2 i < max i++ )
{
listDouble.add (" ")
}
int size3 = listOthers.size ()
for ( int i = size3 i < max i++ )
{
listOthers.add (" ")
}
String reg = "[\\[\\]]"
String intdata = listInt.toString ().replaceAll (reg, "")
String realdata = listDouble.toString ().replaceAll (reg, "")
String otherdata = listOthers.toString ().replaceAll (reg, "")
System.out.println ("Text file: ")
System.out.println (result)
System.out.println ("sumDouble: ")
System.out.println (realdata.replaceAll ("[,\\s]+$", ""))
System.out.println ("sumInt: ")
System.out.println (intdata.replaceAll ("[,\\s]+$", ""))
System.out.println ("sumOther: ")
System.out.println (otherdata.replaceAll ("[,\\s]+$", ""))
String[] ints = intdata.split (",\\s")
String[] reals = realdata.split (",\\s")
String[] others = otherdata.split (",\\s")
data = new String[][] { ints, reals, others }
}
public static void main ( String[] args )
{
loadFileData (FILE_NAME)
TokenCategorizer tc = new TokenCategorizer ("Categories/columns containing tokens")
createList (tc)
createTable (tc)
initOthers (tc)
initFrame (tc)
}
}
忘记你的Jtable把,你要处理的是数据。public class Frame extends javax.swing.JFrame {
TableModel model = new TableModel()
/** Creates new form Frame */
public Frame() {
initComponents()
this.jTable1.setModel(model)
}
class TableModel extends AbstractTableModel {
String[] values = {"乌龟", "海龟", "鸵鸟", "老鸟"}
private int n = 3
@Override
public int getRowCount() {
return (n + 1) * 2
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true
}
public void setIndex(int n) {
this.n = n
this.fireTableStructureChanged()
}
@Override
public int getColumnCount() {
return (n + 1) * 2
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return values[n]
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane()
jTable1 = new javax.swing.JTable()
jPanel1 = new javax.swing.JPanel()
乌龟 = new javax.swing.JButton()
海龟 = new javax.swing.JButton()
鸵鸟 = new javax.swing.JButton()
老鸟 = new javax.swing.JButton()
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE)
jScrollPane1.setName("jScrollPane1")// NOI18N
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
))
jTable1.setName("jTable1")// NOI18N
jScrollPane1.setViewportView(jTable1)
jPanel1.setName("jPanel1")// NOI18N
乌龟.setText("乌龟")
乌龟.setName("乌龟")// NOI18N
乌龟.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
乌龟ActionPerformed(evt)
}
})
海龟.setText("海龟")
海龟.setName("海龟")// NOI18N
海龟.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
海龟ActionPerformed(evt)
}
})
鸵鸟.setText("鸵鸟")
鸵鸟.setName("鸵鸟")// NOI18N
鸵鸟.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
鸵鸟ActionPerformed(evt)
}
})
老鸟.setText("老鸟")
老鸟.setName("老鸟")// NOI18N
老鸟.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
老鸟ActionPerformed(evt)
}
})
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1)
jPanel1.setLayout(jPanel1Layout)
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(21, 21, 21)
.addComponent(乌龟)
.addGap(18, 18, 18)
.addComponent(海龟)
.addGap(18, 18, 18)
.addComponent(鸵鸟)
.addGap(18, 18, 18)
.addComponent(老鸟)
.addContainerGap(117, Short.MAX_VALUE))
)
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(乌龟)
.addComponent(海龟)
.addComponent(鸵鸟)
.addComponent(老鸟))
.addContainerGap(16, Short.MAX_VALUE))
)
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane())
getContentPane().setLayout(layout)
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 412, Short.MAX_VALUE)
)
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE))
)
pack()
}// </editor-fold>
private void 乌龟ActionPerformed(java.awt.event.ActionEvent evt) {
model.setIndex(0)
}
private void 海龟ActionPerformed(java.awt.event.ActionEvent evt) {
model.setIndex(1)
}
private void 鸵鸟ActionPerformed(java.awt.event.ActionEvent evt) {
model.setIndex(2)
}
private void 老鸟ActionPerformed(java.awt.event.ActionEvent evt) {
model.setIndex(3)
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Frame().setVisible(true)
}
})
}
// Variables declaration - do not modify
private javax.swing.JPanel jPanel1
private javax.swing.JScrollPane jScrollPane1
private javax.swing.JTable jTable1
private javax.swing.JButton 乌龟
private javax.swing.JButton 海龟
private javax.swing.JButton 老鸟
private javax.swing.JButton 鸵鸟
// End of variables declaration
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)