来自combobox对象的Java Swing getvalue
我使用swing用Java制作了一个简单的项目。下面是代码,我想从放置在表中的组合框中获取所选值,我如何才能做到这一点?如果将这些数据放在数组中,那就太好了。谢谢你的回答来自combobox对象的Java Swing getvalue,java,eclipse,swing,import,combobox,Java,Eclipse,Swing,Import,Combobox,我使用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
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);
}
}
导入java.awt.EventQueue;
导入java.awt.Font;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.util.Date;
导入javax.swing.DefaultCellEditor;
导入javax.swing.JButton;
导入javax.swing.JComboBox;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JTable;
导入javax.swing.ListSelectionModel;
导入javax.swing.SwingConstants;
导入javax.swing.table.DefaultTableModel;
导入javax.swing.table.TableColumn;
导入javax.swing.table.TableModel;
导入javax.swing.JFormattedTextField;
公共类SWD_GUI实现ActionListener{
按钮;
公共框架;
专用JTable表;
专用JTable表_1;
专用JTable表2;
专用JTable表_3;
私有JFormattedTextField formattedTextField;
JComboBox组合框;
表1列;
/**
*启动应用程序。
*/
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
公开募捐{
试一试{
SWD_GUI窗口=新的SWD_GUI();
window.frame.setVisible(true);
}
捕获(例外e)
{
e、 printStackTrace();
}
}
});
}
/**
*创建应用程序。
*/
公共社署(社署){
初始化();
}
公共无效初始化(){
frame=新的JFrame();
框架.立根(100100846546);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.pack();
frame.getContentPane().setLayout(null);
JLabel lblSwdTelephone=新JLabel(“社署电话”);
lblSwdTelephone.setHorizontalAlignment(SwingConstants.CENTER);
lblSwdTelephone.setFont(新字体(“Tahoma”,Font.BOLD,16));
lblSwdTelephone.setBounds(10,11,810,20);
frame.getContentPane().add(lblSwdTelephone);
按钮=新的JButton(“做吧!”);
按钮.立根(379432,89,23);
frame.getContentPane().add(按钮);
addActionListener(这个);
createTables();
}
私有void createTables(){
createFirstTable();
createSecondTable();
createThirdTable();
createFourthTable();
}
私有void createFourthTable(){
TableModel TableModel_3=新的DefaultTableModel(新对象[][]{
{“电池”、“三星”、“HTC”、“LG”},
{“三星”,新双精度(1.0),空,空},
{“HTC”,空,新双精度(1.0),空},
{“LG”,null,null,新双精度(1.0)},},新字符串[]{
“新列”、“价格”、“规格”、“价格”}){
私有静态最终长serialVersionUID=1L;
boolean[]columnEditables=新的boolean[]{false,true,true,true};
boolean[]rowEditables=新的boolean[]{false,true,true,true};
公共布尔值可编辑(int行,int列){
返回columnEditables[列]&行Editables[行];
}
};
createTable(表3,表模型3,304);
}
私有void createThirdTable(){
TableModel TableModel_2=新的DefaultTableModel(新对象[][]{
{“规格”、“三星”、“HTC”、“LG”},
{“三星”,新双精度(1.0),空,空},
{“HTC”,空,新双精度(1.0),空},
{“LG”,null,null,新双精度(1.0)},},新字符串[]{
“新列”、“价格”、“规格”、“价格”}){
私有静态最终长serialVersionUID=1L;
boolean[]columnEditables=新的boolean[]{false,true,true,true};
boolean[]rowEditables=新的boolean[]{false,true,true,true};
公共布尔值可编辑(int行,int列){
返回columnEditables[列]&行Editables[行];
}
};
createTable(表2,表模型2,214);
}
私有void createSecondTable(){
TableModel TableModel_1=新的DefaultTableModel(新对象[][]{
{“价格”、“三星”、“HTC”、“LG”},
{“三星”,新双精度(1.0),空,空},
{“HTC”,空,新双精度(1.0),空},
{“LG”,null,null,新双精度(1.0)},},新字符串[]{
“新列”、“价格”、“规格”、“价格”}){
私有静态最终长serialVersionUID=1L;
boolean[]columnEditables=新的boolean[]{false,true,true,true};
boolean[]rowEditables=新的boolean[]{false,true,true,true};
公共布尔值可编辑(int行,int列){
ComboItem item = (ComboItem)table.getValueAt(...);
System.out.println( item.getValue() );