我下面有用于在表格的每一行中放置复选框的代码。如何获取选中复选框的值?就像说我需要选定复选框的公司名称的值...
package JavaApplication1;
import javax.swing.*;
import javax.swing.table.*;
public class JavaApplication1 extends JFrame {
private static final long serialVersionUID = 1L;
private JTable table;
public JavaApplication1() {
Object[] columnNames = {"Type", "Company", "Shares", "Price", "Boolean"};
Object[][] data = {
{"Buy", "IBM", new Integer(1000), new Double(80.50), false},
{"Sell", "MicroSoft", new Integer(2000), new Double(6.25), true},
{"Sell", "Apple", new Integer(3000), new Double(7.35), true},
{"Buy", "Nortel", new Integer(4000), new Double(20.00), false}
};
DefaultTableModel model = new DefaultTableModel(data, columnNames);
table = new JTable(model) {
private static …Run Code Online (Sandbox Code Playgroud) 我是编程的新手,我无法添加JCheckbox到JList.没有错误但没有显示任何内容.
JFrame f=new JFrame();
String[] labels={"a","b","c","d","e"};
JCheckBox[] ch=new JCheckBox[labels.length];
JList list=new JList();
for (int i = 0; i < labels.length; i++) {
ch[i]=new JCheckBox("CheckBox"+i);
list.add(ch[i]);
}
JScrollPane pane=new JScrollPane(list);
f.add(pane);
f.setVisible(true);
Run Code Online (Sandbox Code Playgroud) 我似乎无法确定我的代码丢失了什么..请帮助..我想在我的代码上创建一个复选框,但我无法正确...这是从另一个框架,它不是我的主程序. .
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Start extends JFrame implements ItemListener
{
JLabel num1 = new JLabel("1) Is Internet Explorer a web browser?");
JCheckBox true = new JCheckBox("True", false);
JCheckBox false = new JCheckBox("False", false);
String output, insChosen;
public Start()
{
setBounds(100,100,500,400);
setResizable(false);
num1.setFont(new Font("Arial", Font.ITALIC,12));
add(num1);
true.addItemListener(this);
false.addItemListener(this);
add(true);
add(false);
}
public void itemStateChanged(ItemEvent check)
{
}
}
Run Code Online (Sandbox Code Playgroud) 我正在编写一个Java应用程序,它有多个项目可供测试.但是 - 由于硬件限制,我只能并行测试4个设备.我想知道 - 在属 - 如何让Java将所选项目的数量限制为仅四个(无论选择哪个项目)?我正在阅读Java文档,我似乎无法找到答案.
我对Java很新 - 所以请原谅我的无知.我只需要了解在哪里/如何开始.
如何限制在Java中选中的复选框的数量?
我有三个JCheckBox如下:
final JCheckBox c1 = new JCheckBox("A");
final JCheckBox c2 = new JCheckBox("B");
final JCheckBox c3 = new JCheckBox("C");
Run Code Online (Sandbox Code Playgroud)
我通过ButtonGroup为这个复选框创建了一个组,如下所示:
final ButtonGroup bg = new ButtonGroup();
bg.add(c1);
bg.add(c2);
bg.add(c3);
Run Code Online (Sandbox Code Playgroud)
我有一个按钮可以将选定的项目显示在如下标签中:
String SelectedItem="";
Enumeration<AbstractButton> items= bg.getElements();
while (items.hasMoreElements()) {
AbstractButton btn = items.nextElement();
if(btn.isSelected())
{
SelectedItem+=btn.getText()+",";
}
}
lblA.setText(SelectedItem);
Run Code Online (Sandbox Code Playgroud)
这工作正常,但我无法在运行时选中多个复选框.
我正在ToolBar用一个JButton和一个JCheckBoxs 创建一个隐藏或显示列的列JTable.
的JButton主要目的是,一旦点击重置所有其他JCheckBox的ToolBar要么全部选中或全部选中.实际上我假装一旦点击重置就检查所有这些.
我可以创建它们并放置它们,这里没有问题.
我的问题是如何让JButton一旦点击就重置了所有的JCheckBox ToolBar.
这是我的代码,我在其中创建并添加Action.
final JToolBar toolBarTop = new JToolBar();
// The Reset Button
toolBarTop.add(new JButton(new AbstractAction("Reset") {
@Override
public void actionPerformed(ActionEvent e) {
columnModel.setAllColumnsVisible();
}
}));
// Create a JCheckBox for each column
for(int i = 0; i < labelsCheckBox.size(); i++)
{
final int index = i;
toolBarTop.add(new JCheckBox(new AbstractAction(labelsCheckBox.get(i)) {
@Override
public void actionPerformed(ActionEvent e) {
TableColumn column …Run Code Online (Sandbox Code Playgroud) 我正在使用swing来创建桌面应用程序.我创建了从数据库提供数据并将其插入Jtable的功能.我想要使用提供附加功能,包括附加列和复选框以及用于删除该perticuler列的按钮(已检查) )单击按钮时.i已使用netbeans并提供最大拖放选项.我无法弄清楚在当前代码中插入复选框实例的方式和位置,用于插入每行的复选框.为了提供每行的复选框,必须生成复选框的多个实例
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
java.sql.Connection con = new DBConnection().getConnection();
PreparedStatement pst;
String Q;
Q = "select * from userregister ";
try {
pst = con.prepareStatement(Q);
ResultSet rs = null;
rs = pst.executeQuery();
String a, b, c, d;
int x = 0;
// DefaultTableModel dt = new DefaultTableModel(data, columnNames);
JCheckBox c1 = new JCheckBox();
for (int i = 0; rs.next(); i++) {
a = rs.getString(1);
b = rs.getString(2);
c = rs.getString(3);
d = rs.getString(4);
jTable2.setValueAt(a, i, 0);
jTable2.setValueAt(b, …Run Code Online (Sandbox Code Playgroud) 我改Nimbus的外观和感觉,因为它是描述在这里.
它工作正常,除了JCheckBoxMenuItems,其中校验方块消失了.选择颜色也不正确.
这是带有JCheckBoxMenuItems的popupmenu的代码:
public class FilterByNodeUI
{
private JPopupMenu filterMenu;
private PopupMenuButton menu;
private JMenu eventLogFilters, commandFilters;
private JCheckBoxMenuItem[] commandsPeriodicalItems, commandsPeriodicalAllPortsItems;
private JCheckBoxMenuItem alarm, connection, standard;
private String id;
public FilterByNodeUI(Node node)
{
this.id = node.getIp();
filterMenu = new JPopupMenu();
eventLogFilter();
int cpNbr = node.getCommands().getCommandsPeriodical().size();
int cpapNbr = node.getCommands().getCommandsPeriodicalAllPorts().size();
commandsPeriodicalItems = new JCheckBoxMenuItem[cpNbr];
commandsPeriodicalAllPortsItems = new JCheckBoxMenuItem[cpapNbr];
commandFilters = new JMenu("Commands");
for(int i = 0; i < cpNbr; i++)
{
commandsPeriodicalItems[i] = menuItemFactory(node.getCommands().getCommandsPeriodical().get(i).getCommand());
commandFilters.add(commandsPeriodicalItems[i]);
}
commandFilters.addSeparator();
for(int i …Run Code Online (Sandbox Code Playgroud) 我在表单上放了两个复选框.如果用户设置check1复选框而不是复选checkbox2,反之亦然.
chRISING0 = new JCheckBox("RISING");
chRISING0.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
chFR0.setSelected(false);
}
});
chRISING0.setFont(new Font("Tahoma", Font.PLAIN, 9));
chRISING0.setBounds(42, 15, 84, 23);
pnlOUT1.add(chRISING0);
chFR0 = new JCheckBox(fpp.loadLanguage("chFR0"));
chFR0.setFont(new Font("Tahoma", Font.PLAIN, 9));
chFR0.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
chRISING0.setSelected(false);
}
});
chFR0.setBounds(42, 35, 67, 23);
pnlOUT1.add(chFR0);
Run Code Online (Sandbox Code Playgroud)
当我检查chRISING0并在chFR0上移动鼠标时,会发生奇怪的问题.比chRISING0取消选中,即使我没有按下chFR0.为什么?我做错了什么?
我正在创建一个简单的程序,用户可以在其中勾选不同颜色的复选框。如果在多个方框中打勾,则应该显示颜色的混合。示例蓝色和黄色被打勾,然后标签显示绿色。我有3个问题。
class LabDemo extends JFrame implements ActionListener {
JLabel displayColor = new JLabel("No chosen color");
JCheckBox blue = new JCheckBox("Blue");
JCheckBox yellow = new JCheckBox("Yellow");
JCheckBox red = new JCheckBox("Red");
JPanel panel = new JPanel();
public LabDemo() {
panel.setLayout(new GridLayout(4,1));
blue.addActionListener(this);
yellow.addActionListener(this);
red.addActionListener(this);
this.add(panel);
displayColor.setBackground(Color.WHITE);
panel.add(blue); panel.add(yellow); panel.add(red); panel.add(displayColor);
setSize(300,300);
setLocation(100,100);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
if (blue.isSelected()) {
displayColor.setText("Blue");
panel.setBackground(Color.BLUE);
}
else if (yellow.isSelected()) {
displayColor.setText("Yellow");
panel.setBackground(Color.YELLOW);
}
else if (red.isSelected()) { …Run Code Online (Sandbox Code Playgroud)