Monday, October 27, 2014

JScrollPane with JTable Auto Resizing Issue








Hi,








We have Swing window with JTabbedPane (First Tab,Second Tab)







In the First Tab, we have JPanel with GridBagLayout.We have divided the panel into three Columns and several rows.







In the first panel, we added some Checkbox List1 (Column1), Checkbox List2 (Column2) , Checkbox List3 (Column3) in the first Row.








In the second row, we added JScrollPane which contains JTable.User can populate the JTable with the data based on the user selection of Checkbox List1/2/3.








Issue: We are getting horizontal/vertical scroll bars for the scrollpane with tabledata after fetch results based on checkboxs selection. But we need to auto resize the scrollpane with Table data in both horizontally and vertically.








Follwoing is the code.








It would be great help, if you can suggest on this.







Thanks,







Samba.








//////////////////////////////////////////////////////////////////////////////////












XML Code:











public class SOS extends JFrame implements TreeWillExpandListener,

TreeExpansionListener,

JPanel firstPanel = new JPanel(new GridBagLayout());

JPanel secondPanel = new JPanel(new GridBagLayout());



public SOS() throws SQLException, ParserConfigurationException,

SAXException, IOException, XPathExpressionException {

//EHDD Tabbed Pane



nehObjectTypelist = new JList((createObjectsListData()).toArray());

nehObjectTypelist.setFont(font);

nehObjectTypelist.setCellRenderer(new CheckListRenderer());

nehObjectTypelist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

nehObjectTypelist.setBorder(new EmptyBorder(0, 4, 0, 0));

nehObjectTypelist.addMouseListener(new MouseAdapter() {

});

JScrollPane nehObjTypeSP = new JScrollPane(nehObjectTypelist);

nehObjTypeSP.setSize(200, 200);

nehOwmerslist = new JList((createOwnersListData()).toArray());

nehOwmerslist.setFont(font);

nehOwmerslist.setCellRenderer(new CheckListRenderer());

nehOwmerslist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

nehOwmerslist.setBorder(new EmptyBorder(0, 4, 0, 0));

nehOwmerslist.addMouseListener(new MouseAdapter() {

});

JScrollPane sp = new JScrollPane(nehOwmerslist);

sp.setSize(200, 200);



//JTree with checkbox

DefaultMutableTreeNode root = createFirstLevel();

final DefaultTreeModel model = new DefaultTreeModel(root);



nehTree = new JTree(root);

QuestionCellRenderer renderer = new QuestionCellRenderer();

nehTree.setCellRenderer(renderer);

QuestionCellEditor editor = new QuestionCellEditor();

nehTree.setCellEditor(editor);

nehTree.setEditable(true);

nehTree.addTreeWillExpandListener(this);

nehTree.setShowsRootHandles(true);

nehTree.setModel(model);

nehTree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);

JScrollPane scroll = new JScrollPane(nehTree);

scroll.setPreferredSize(new Dimension(200, 180));

scroll.setFont(font);





GridBagConstraints gbc = new GridBagConstraints();

gbc.insets = new Insets(1, 1, 1, 1);

gbc.gridx = 0;

gbc.gridy = 0;

//gbc.gridwidth = 2;

gbc.fill = GridBagConstraints.HORIZONTAL;

//gbc.anchor = GridBagConstraints.LINE_START;

JLabel nehHeading =

new JLabel("Select your criteria and click the Fetch Topics by Criteria button");

nehHeading.setFont(font);

firstPanel.add(nehHeading, gbc);



gbc.insets = new Insets(1, 1, 1, 1);



gbc.fill = GridBagConstraints.HORIZONTAL;





gbc.gridx = 0;

gbc.gridy = 1;

gbc.gridwidth = 1;

JLabel nehDocType = new JLabel("Type");

nehDocType.setFont(font);

firstPanel.add(nehDocType, gbc);



gbc.gridx = 1;

gbc.gridy = 1;

JLabel nehDocOwner = new JLabel("Owner");

nehDocOwner.setFont(font);

firstPanel.add(nehDocOwner, gbc);



gbc.gridx = 2;

gbc.gridy = 1;

JLabel nehProdTree = new JLabel("Tree");

nehProdTree.setFont(font);

firstPanel.add(nehProdTree, gbc);



gbc.weightx = 0;

gbc.weighty = 0;



gbc.gridx = 0;

gbc.gridy = 2;

firstPanel.add(nehObjTypeSP, gbc);



gbc.gridx = 1;

gbc.gridy = 2;

firstPanel.add(sp, gbc);



gbc.gridx = 2;

gbc.gridy = 2;

firstPanel.add(scroll, gbc);

gbc.fill = GridBagConstraints.VERTICAL;

gbc.anchor = GridBagConstraints.FIRST_LINE_START;

gbc.weightx = 0;

gbc.weighty = 0;

gbc.gridheight = 1;

gbc.gridheight = 1;

gbc.gridx = 0;

gbc.gridy = 21;

JButton fetchButton = new JButton("Fetch");

fetchButton.setFont(font);

fetchButton.setSize(new Dimension(175, 35));



firstPanel.add(fetchButton, gbc);

fetchButton.addActionListener(new ActionListener() {



});



gbc.gridx = 0;

gbc.gridy = 22;

JLabel nehOR = new JLabel("- OR -");

nehOR.setFont(font);

firstPanel.add(nehOR, gbc);



JButton searchButton = new JButton("Fetch by Title");

searchButton.setToolTipText("Enter title search criteria");

searchButton.setFont(font);

gbc.gridheight = 1;

gbc.gridx = 0;

gbc.gridy = 23;

gbc.anchor = GridBagConstraints.LINE_START;

JLabel nehOpenBySVNIdLabel = new JLabel("Open by ID");

nehOpenBySVNIdLabel.setFont(font);

firstPanel.add(nehOpenBySVNIdLabel, gbc);



gbc.gridheight = 1;

gbc.gridx = 1;

gbc.gridy = 23;

gbc.anchor = GridBagConstraints.WEST;

findTextField.setColumns(20);

firstPanel.add(findTextField, gbc);



gbc.gridheight = 1;

gbc.gridx = 2;

gbc.gridy = 23;

gbc.anchor = GridBagConstraints.WEST;

JButton openButton = new JButton("Open");

openButton.setFont(font);

openButton.setPreferredSize(searchButton.getPreferredSize());

System.out.println("Button Size" + openButton.getSize());

firstPanel.add(openButton, gbc);



openButton.addActionListener(new ActionListener() {



});



gbc.gridheight = 1;

gbc.gridx = 0;

gbc.gridy = 24;

gbc.anchor = GridBagConstraints.LINE_START;

JLabel searchLabel =

new JLabel("Search the repository by title word(s)");

searchLabel.setToolTipText("Enter title search criteria");

searchLabel.setFont(font);

firstPanel.add(searchLabel, gbc);



gbc.gridheight = 1;

gbc.gridx = 1;

gbc.gridy = 24;

gbc.anchor = GridBagConstraints.WEST;



searchTextField = new JTextField("", 20);

searchTextField.setColumns(20);

searchTextField.setEditable(true);

firstPanel.add(searchTextField, gbc);



gbc.gridheight = 1;

gbc.gridx = 2;

gbc.gridy = 24;

gbc.anchor = GridBagConstraints.WEST;



firstPanel.add(searchButton, gbc);



searchButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

});



gbc.anchor = GridBagConstraints.FIRST_LINE_START;

gbc.gridwidth = 4;

gbc.gridx = 0;

gbc.gridy = 28;

gbc.fill = GridBagConstraints.HORIZONTAL;



tableScrollPane.setPreferredSize(new Dimension(200, 200));

firstPanel.add(tableScrollPane, gbc);



gbc.fill = GridBagConstraints.NONE;

gbc.gridheight = 1;



JRadioButton reviewButton = new JRadioButton("Review");

reviewButton.setFont(font);

reviewButton.setMnemonic(KeyEvent.VK_B);

reviewButton.setSelected(true);

JRadioButton testButton = new JRadioButton("Test");

testButton.setFont(font);

testButton.setMnemonic(KeyEvent.VK_B);

JRadioButton prodButton = new JRadioButton("Production");

prodButton.setFont(font);

prodButton.setMnemonic(KeyEvent.VK_B);



nehGroup.add(reviewButton);

nehGroup.add(testButton);

nehGroup.add(prodButton);



gbc.gridx = 2;

gbc.gridy = 30;

firstPanel.add(reviewButton, gbc);



gbc.gridx = 2;

gbc.gridy = 31;

firstPanel.add(testButton, gbc);



gbc.gridx = 2;

gbc.gridy = 32;

firstPanel.add(prodButton, gbc);



gbc.gridx = 2;

gbc.gridy = 33;

JButton printButton = new JButton("Print");

printButton.setFont(font);

firstPanel.add(printButton, gbc);

printButton.addActionListener(new ActionListener() {

});





gbc.fill = GridBagConstraints.NORTHEAST;

gbc.gridx = 0;

gbc.gridy = 30;

JButton openTopicsButton = new JButton("Open Selection");

openTopicsButton.setFont(font);

firstPanel.add(openTopicsButton, gbc);

openTopicsButton.addActionListener(new ActionListener() {

});



gbc.gridx = 0;

gbc.gridy = 32;

JButton mapLinks = new JButton("Insert");

mapLinks.setFont(font);

JButton relatedLinks = new JButton("Insert Links");

relatedLinks.setFont(font);

relatedLinks.setPreferredSize(mapLinks.getPreferredSize());

firstPanel.add(relatedLinks, gbc);

relatedLinks.addActionListener(new ActionListener() {

});



gbc.gridx = 0;

gbc.gridy = 33;

firstPanel.add(mapLinks, gbc);

mapLinks.addActionListener(new ActionListener() {

});





tabbedPane.add("NEH", firstPanel);

//NEH Tabbed Pane

tabbedPane.add("EHDD", secondPanel);

add(tabbedPane);

tabbedPane.addChangeListener(new ChangeListener() {

});



}



public static void main(String[] args) throws SQLException,

ParserConfigurationException,

SAXException, IOException,

XPathExpressionException {

SOS sos = new SOS();

sos.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

sos.setTitle("SOS");

sos.setSize(760, 680);

sos.setVisible(true);

sos.toFront();

sos.setMinimumSize(new Dimension(760, 680));

}

}









//////////////////////////////////////////////////////////////////////////////////










No comments:

Post a Comment