Reputation: 1
Reference to the given code..i can access the print statement inside the if statement in for-loop(inside try-catch block) but the checkboxes are not adding. I am using the revalidating and repaint function but still its not working. The for loop inside the try-catch block is running except for the adding jscrollpane. Where am i wrong?
/*
* 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);
}
}
Upvotes: 0
Views: 3714
Reputation: 347214
Start by having a read through Creating a GUI With JFC/Swing, Laying Out Components Within a Container, Concurrency in Swing and How to Use Scroll Panes
Box
is not really a "visible" component, it's meant as a feature to be used with BoxLayout
JScrollPane
s to the single view, which kind of feels weird to meactionPerformed
method existrevaldiate
all the time, leave it until you have finished updating the whole UI, it will provide better performance in the long runStart by creating a JPanel
which you then wrap a JScrollPane
around, add the JScrollPane
to the base UI
As required, add and remove components from this JPanel
, for example...
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);
}
});
}
}
Now, I'm not 100% sure, but you might find using a JTable
produces the look you're after. How to Use Tables
Upvotes: 2
Reputation: 460
You can't add the checkboxes directly to the scroll pane, you need to make a list model. You will need a Scroll Pane, a list renderer, a model, and a custom JCheckboxList class. See the following questions:
How do I make a list with checkboxes in Java Swing?
Also, you might run into a problem that I did where the JScrollPane's viewport:
Components in JList are Hidden by White Square Thing Until Clicked
Upvotes: 0