我使用swing在Java中创建了一个简单的项目。下面是代码,我想从表格中放入组合框中选择值,我该怎么做?如果将数据放在数组中,那将很好。感谢所有答案。
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import javax.swing.JFormattedTextField;
public class SWD_GUI implements ActionListener {
JButton button;
public JFrame frame;
private JTable table;
private JTable table_1;
private JTable table_2;
private JTable table_3;
private JFormattedTextField formattedTextField;
JComboBox<ComboItem> comboBox;
TableColumn column_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SWD_GUI window = new SWD_GUI();
window.frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public SWD_GUI() {
initialize();
}
public void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 846, 546);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.pack();
frame.getContentPane().setLayout(null);
JLabel lblSwdTelephone = new JLabel("SWD TELEPHONE");
lblSwdTelephone.setHorizontalAlignment(SwingConstants.CENTER);
lblSwdTelephone.setFont(new Font("Tahoma", Font.BOLD, 16));
lblSwdTelephone.setBounds(10, 11, 810, 20);
frame.getContentPane().add(lblSwdTelephone);
button = new JButton("DO IT!");
button.setBounds(379, 432, 89, 23);
frame.getContentPane().add(button);
button.addActionListener(this);
createTables();
}
private void createTables() {
createFirstTable();
createSecondTable();
createThirdTable();
createFourthTable();
}
private void createFourthTable() {
TableModel tableModel_3 = new DefaultTableModel(new Object[][] {
{ "BATTERY", "Samsung", "HTC", "LG" },
{ "Samsung", new Double(1.0), null, null },
{ "HTC", null, new Double(1.0), null },
{ "LG", null, null, new Double(1.0) }, }, new String[] {
"New column", "Price", "Specification", "Baterry" }) {
private static final long serialVersionUID = 1L;
boolean[] columnEditables = new boolean[] { false, true, true, true };
boolean[] rowEditables = new boolean[] { false, true, true, true };
public boolean isCellEditable(int row, int column) {
return columnEditables[column] && rowEditables[row];
}
};
createTable(table_3, tableModel_3, 304);
}
private void createThirdTable() {
TableModel tableModel_2 = new DefaultTableModel(new Object[][] {
{ "SPECIFICATION", "Samsung", "HTC", "LG" },
{ "Samsung", new Double(1.0), null, null },
{ "HTC", null, new Double(1.0), null },
{ "LG", null, null, new Double(1.0) }, }, new String[] {
"New column", "Price", "Specification", "Baterry" }) {
private static final long serialVersionUID = 1L;
boolean[] columnEditables = new boolean[] { false, true, true, true };
boolean[] rowEditables = new boolean[] { false, true, true, true };
public boolean isCellEditable(int row, int column) {
return columnEditables[column] && rowEditables[row];
}
};
createTable(table_2, tableModel_2, 214);
}
private void createSecondTable() {
TableModel tableModel_1 = new DefaultTableModel(new Object[][] {
{ "PRICE", "Samsung", "HTC", "LG" },
{ "Samsung", new Double(1.0), null, null },
{ "HTC", null, new Double(1.0), null },
{ "LG", null, null, new Double(1.0) }, }, new String[] {
"New column", "Price", "Specification", "Baterry" }) {
private static final long serialVersionUID = 1L;
boolean[] columnEditables = new boolean[] { false, true, true, true };
boolean[] rowEditables = new boolean[] { false, true, true, true };
public boolean isCellEditable(int row, int column) {
return columnEditables[column] && rowEditables[row];
}
};
createTable(table_1, tableModel_1, 124);
}
private void createFirstTable() {
TableModel tableModel = new DefaultTableModel(new Object[][] {
{ null, "Price", "Specification", "Baterry" },
{ "Price", new Double(1.0), null, null },
{ "Specification", null, new Double(1.0), null },
{ "Baterry", null, null, new Double(1.0) }, }, new String[] {
"New column", "Price", "Specification", "Baterry" }) {
private static final long serialVersionUID = 1L;
boolean[] columnEditables = new boolean[] { false, true, true, true };
boolean[] rowEditables = new boolean[] { false, true, true, true };
public boolean isCellEditable(int row, int column) {
return columnEditables[column] && rowEditables[row];
}
};
createTable(table, tableModel, 39);
}
private void createTable(JTable table, TableModel tableModel, int y) {
table = new JTable();
table.setColumnSelectionAllowed(true);
table.setCellSelectionEnabled(true);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setFillsViewportHeight(true);
table.setBounds(10, y, 424, 64);
frame.getContentPane().add(table);
table.setModel(tableModel);
addColumn(table, 1);
addColumn(table, 2);
addColumn(table, 3);
}
class ComboItem
{
public String key;
public String value;
public ComboItem(String key, String value)
{
this.key = key;
this.value = value;
}
//@Override
public String toString()
{
return key;
}
public String getKey()
{
return key;
}
public String getValue()
{
return value;
}
}
public void addColumn(JTable table, int columnIndex) {
{
TableColumn column_1 = table.getColumnModel()
.getColumn(columnIndex);
JComboBox<Object> comboBox = new JComboBox<Object>();
comboBox.addItem(new ComboItem("1", "1.0"));
comboBox.addItem(new ComboItem("3", "3.0"));
comboBox.addItem(new ComboItem("5", "5.0"));
comboBox.addItem(new ComboItem("7", "7.0"));
comboBox.addItem(new ComboItem("1/3", "0.333333333"));
comboBox.addItem(new ComboItem("1/5", "0.2"));
comboBox.addItem(new ComboItem("1/7", "0,1428571428571429"));
//comboBox.addItem((double) 1);
//comboBox.addItem((double) 3);
//comboBox.addItem((double) 5);
//comboBox.addItem((double) 7);
//comboBox.addItem((double) 9);
//comboBox.addItem((double) (1.0/3.0));
//comboBox.addItem((double) (1.0/5.0));
//comboBox.addItem((double) (1.0/7.0));
//comboBox.addItem((double) (1.0/9.0));
column_1.setCellEditor(new DefaultCellEditor(comboBox));
Object item = comboBox.getSelectedItem();
String value = ((ComboItem)item).getValue();
System.out.println(value);
}
formattedTextField = new JFormattedTextField();
formattedTextField.setBounds(462, 36, 358, 333);
frame.getContentPane().add(formattedTextField);
}
//public void getContents() {
// TODO Auto-generated method stub
//}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stud
Object source = e.getSource();
if (source == button) {
}
//for(int i = 0; i<7; i++){
//System.out.println(anArrays[i]);
//}
// getContents();
//int row = table.getSelectedRow();
//String idS = table.getValueAt(row, 0).toString();
//String al = column_1.getMaxWidth().toString();
//System.out.println(al);
// System.out.println(item);
// formattedTextField.add(table);
//formattedTextField.setValue(table);
}
}
答案 0 :(得分:0)
您从表中获取数据,而不是组合框。
ComboItem item = (ComboItem)table.getValueAt(...);
System.out.println( item.getValue() );