[英]dynamically adding checkboxes in java swing
對給定代碼的引用..i可以在for循環(在try-catch塊內)的if語句內訪問print語句,但是未添加復選框。 我正在使用revalidating和repaint功能,但仍然無法正常工作。 除了添加的jscrollpane,try-catch塊中的for循環正在運行。 我哪里錯了?
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.anurag;
import com.anurag.HttpURLConnectionExample;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
/**
*
* @author Anurag
*/
public class MainGui {
static JFrame frame = new JFrame("Rest Testing");
public static void main(String args[]) {
frame.setLayout(new FlowLayout());
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JTextField path = new JTextField("getresponse.xls");
frame.add(path);
JButton upload = new JButton("Upload");
frame.add(upload);
upload.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
JScrollPane jscrlpLabel = new JScrollPane(new JLabel(
"<HTML>A<br>B<br>C<br>D<br>E<br>F<br>G<br>H<br></HTML>"));
jscrlpLabel.setPreferredSize(new Dimension(200, 100));
frame.add(jscrlpLabel);
frame.revalidate();
frame.repaint();
try{
System.out.println(path.getText());
HttpURLConnectionExample http = new HttpURLConnectionExample();
int noOfRows = http.setPath(path.getText());
Workbook workbook = Workbook.getWorkbook(new File(path.getText()));
Sheet sheet = workbook.getSheet(0);
Cell cell= sheet.findCell("S. No.");
int col=cell.getColumn();
int row=cell.getRow();
row=row+2;
//System.out.println("row="+row+"col="+col);
for(int i=row;i<noOfRows;i++,row++)
{
int p=http.readFile(col,row);
if(p==1){
JCheckBox cb = new JCheckBox("CheckBox");
JScrollPane jscrlp = new JScrollPane(cb);
jscrlp.setPreferredSize(new Dimension(140, 95));
frame.add(jscrlp);
frame.revalidate();
frame.repaint();
System.out.println("Checkbox created");
}
else if(p==2){
JCheckBox cb1 = new JCheckBox("CheckBox ");
Box box = Box.createVerticalBox();
box.add(cb1);
JScrollPane jScrollPane1 = new JScrollPane(box);
jScrollPane1.setPreferredSize(new Dimension(140, 95));
frame.add(jScrollPane1);
frame.revalidate();
frame.repaint();
System.out.println("Checkbox created with textfield");
}
}
http.getData();
}catch(Exception e){System.out.println("Exception is "+e);}
}
});
//JOptionPane.showMessageDialog(null, "Done", "Alert", WIDTH);
JCheckBox a = new JCheckBox("A");
JCheckBox b = new JCheckBox("B");
JLabel label = new JLabel("Option");
Box box = Box.createVerticalBox();
box.add(label);
box.add(a);
box.add(b);
JScrollPane jscrlpBox = new JScrollPane(box);
jscrlpBox.setPreferredSize(new Dimension(240, 150));
//f.add(jscrlpLabel);
frame.add(jscrlpBox);
frame.setVisible(true);
}
}
首先通讀使用JFC / Swing創建GUI , 在容器中布置組件 ,Swing中的並發以及如何使用滾動窗格。
Box
並不是真正的“可見”組件,它是與BoxLayout
一起使用的功能 JScrollPane
,這讓我感覺很奇怪 actionPerformed
方法存在 revaldiate
沒有什么意義,直到您完成整個UI的更新為止,從長遠來看,它將提供更好的性能 通過創建一個啟動JPanel
,你再包裝一個JScrollPane
周圍,添加JScrollPane
的基本UI
根據需要,從此JPanel
添加和刪除組件,例如...
public class MainGui {
public static void main(String args[]) {
new MainGui();
}
private JFrame frame = new JFrame("Rest Testing");
private JPanel checkboxes;
public MainGui() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JTextField path = new JTextField("getresponse.xls");
JPanel fields = new JPanel();
fields.add(path);
JButton upload = new JButton("Upload");
fields.add(upload);
frame.add(fields, BorderLayout.NORTH);
checkboxes = new JPanel(new GridBagLayout());
JScrollPane scrollPane = new JScrollPane(checkboxes);
frame.add(scrollPane);
upload.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.anchor = GridBagConstraints.WEST;
gbc.weightx = 1;
checkboxes.add(new JLabel("<HTML>A<br>B<br>C<br>D<br>E<br>F<br>G<br>H<br></HTML>"), gbc);
try {
int noOfRows = 100;
for (int row = 0; row < noOfRows; row++, row++) {
int p = (int) ((Math.random() * 2) + 1);
System.out.println(p);
if (p == 1) {
JCheckBox cb = new JCheckBox("CheckBox");
checkboxes.add(cb, gbc);
} else if (p == 2) {
JCheckBox cb1 = new JCheckBox("CheckBox ");
JPanel stuff = new JPanel();
stuff.add(cb1);
stuff.add(new JTextField(10));
checkboxes.add(stuff, gbc);
}
}
} catch (Exception e) {
System.out.println("Exception is " + e);
}
checkboxes.revalidate();
checkboxes.repaint();
}
});
//JOptionPane.showMessageDialog(null, "Done", "Alert", WIDTH);
JCheckBox a = new JCheckBox("A");
JCheckBox b = new JCheckBox("B");
JLabel label = new JLabel("Option");
JPanel stuff = new JPanel();
stuff.add(label);
stuff.add(a);
stuff.add(b);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.anchor = GridBagConstraints.WEST;
gbc.weightx = 1;
checkboxes.add(stuff, gbc);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
現在,我不確定100%,但是您可能會發現使用JTable
會產生您所追求的外觀。 如何使用表格
您不能將復選框直接添加到滾動窗格,需要創建列表模型。 您將需要滾動窗格,列表渲染器,模型和自定義JCheckboxList類。 請參閱以下問題:
另外,您可能會在JScrollPane的視口所在的位置遇到一個問題:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.