How can I add multiple checkboxes to jScrollPane? - java

I have a Java connection with MySQL database. I inserted 30 variable to a MySQL table. I would like to create a graphical interface with Jframe where I can select the variables from the table using a checkbox.
I plan to do something similar as Java Swing Group of checkbox multiple selection handler
But I do not know how I can use it with NetBeans? I tried to add a newJFrame where I add a jScrollPane and I wanted to add multiple jCheckBoxes but I can add only one to jScrollPane.
Could someone let me know how I should add a group of checkboxes to jScrollPane?

You have to add a JPanel to your JScrollPane, then add checkboxes to that JPanel.
JPanel panel = new JPanel(layout);
panel.add(checkbox1);
panel.add(checkbox2);
JScrollPane scrollPane = new JScrollPane(panel);

Related

Set MouseListener to JLabel inside JTable

I have one JTable which contains some columns with String and two columns for Edit and Delete. I have added ImageIcon on JLabel, and set that label in JTable for all rows. Now I want to set MouseListener for that JLabel(as there is no way to set MouseListener for ImageIcon).
How can I set MouseListener to JLabel inside JTable?
I can get which cell clicked by user, but I want to set Listener only.
Please suggest me....
As I found there is no way to add direct listner to components inside JTable. You have to go with JTable's listener only.

Java AbstractTableModel repaint();

I have a problem in my table model to update the data I print in it.
I have the class AgendaTableModel who is the table model, and the class Screen that is the main UI, when I press the button 'Listar Contatos' the table model should appear on JScrollPane in the center of the JFrame, but it continues blank.
What should be the problem ?
You should really post the code or better an SSCCE.
Here's the Oracle's tutorial on JTable.
I'll give you some hints:
Each JTable has a TableModel associated
You don't display the TableModel but the JTable, that is a view of your model
When you add components dynamically you should revalidate the parent component, so if you are adding a JTable somewhere, try to revalidate its container.
If you are trying to add the JTable to an already existing JScrollPane (empty or containing something else), consider to instantiate a new JScrollPane rather than updating its content.

Active Elements in swing JTree, JTable or JList

I need a Container with similar JPanels lined up one below the other which can be selected. I could:
Use a JList with a custom renderer but those JPanels would be passive elements, that's not what I want.
Implement my own Container with 'active' JPanels but those would not be selectable. Or could they made selectable?
Maybe a MouseListener and access to the system default selected-background-colors could be a way but it seems a bit too much effort
Use a JTable or JTree with custom cell editors rendering the 'active' JPanel. But these active parts would only react at the 'second' click, first to activate the editor, second to perform the real action of the JPanel. this is also not acceptable.
To get a more visual impression, here is an example of what this could mean:
A JList containing list items which have each two functional JButtons.
As you've discovered, simply putting a JPanel inside a JList doesn't quite work as you'd like. The JPanel will be passive and won't receive events - essentially all that is happening is your JPanel is simply being drawn, it's not a living component.
Instead of using a JList to put your panels in a list, use a list-like layout manager, such as BoxLayout or GridLayout. If you want all your panels to be the same size, use GridLayout with only a single column.
How to Use GridLayout
How to use BoxLayout
I'm not sure I understand your "example". If you want two functional buttons, then use a JTable where the functional buttons are contained is separate columns. Then your data would be displayed in other columns.
Table Button Column shows how you can do this.
your question(s) isn't clear for me, maybe here
there is JTable, with one TableColumn but without TableHeader, contains JPanel with active JComponents inside (you can implements TableCellEditor for all JComponents) as JComboBox, JButton and JTextField

How to add list of checkboxes [combo boxes] to JScrollPane in Java Netbeans Swing application

I have a list of names and associated values to create checkboxes like
["SAMABULA","CARPETS INTERNATIONAL","HRM/TRAINING"]
["091","094","003"]
How to create checkboxes with these values add these checkboxes to JScrollPane in Netbeans IDE.
This is a Swing application. I created a window from palette and added jscrollpane to that window. But I don't know how to add list of checkboxes to that scrollpane.
# Srikanth Dyapa
there are two areas
you have already JList that's contains JCheckBox(es)
then you just declare
JScrollPane myScrollPane = new JScrollPane(myList);
or you have to put your JCheckBox to JList or JPanel but for JPanel with correct LayoutManager for example GridLaoyut
thenarter add your JList or JPanel to the JScrollPane as is above mentioned
maybe ButtonGroup Component can help you with that
Not sure how to add them in NetBeans as i use Eclipse, but in code would be something like:
JCheckBox chk = new JCheckBox("[LABEL STRING]");
// add event listeners to chk
myScrollPane.add(chk);
see here for more detail and examples: http://download.oracle.com/javase/tutorial/uiswing/components/scrollpane.html
In Netbeans you just drag and drop the Check Boxes where you need them. In an Action Listener you use jCheckBox1.isSelected() in order to check if the user has checked the box or not.

JScrollPane layout

I want to add table2 into the scrollpanel (called feedback) which already has table1 in there. But only one table shows up. If I use feedback.add(table2), only the 1st table shows (I guess the 2nd table is behind the first one, but I don't know how to make the second one below the first one). if I use feedback.getViewport().add(table2, null), only the 2nd table shows. Do I need to use some layout manager here? i tried to search online about scrollpanel layout but didn't get any solutions. Could anyone tell me what's the problem or give me some related example links? Thanks a lot. The relative code are:
content = getContentPane();
content.setLayout(new FlowLayout());
scrollPane = new JScrollPane(tree);
feedback = new JScrollPane(table1);
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,scrollPane, feedback);
content.add(splitPane);
.
.
.
.
feedback.add(table2);
//i add this, but still doesn't work
content.add(table2);
Only a single component can be added to the "viewport" of a JScrollPane. This is done by using:
JScrollPane scrollPane = new JScrollPane( table );
or
scrollPane.setViewportView( table );
If you want multiple component to appear in a scrollPane then add the component to a panel first and add the panel to the viewport.
Read the JScrollPane API for more information and follow the link to the Swing tutorial as well for examples.

Categories

Resources