Jtable dynamical fixed columns problems - java

I created a class that can dynamically to lock and unlock columns .
In my program i create two tables with the same tablemodel.
One is in the Jviewport of the scrollpane, the other in the RowHeaderView.
The problem is when you unlock all the locked columns
and you want to start to lock again, doesn't work. There are no errors but it's like the event doesn't answer.
Steps to produce the problem:
Try the code,
put all the columns in the fixed table,
then unlock with right double click,
then start again to lock, and unlock
Do this procedure and you can see that the mouse event doesnt answer anymore
public class Prova extends JFrame{
private JTable mainTable,fixedTable;
private JScrollPane scrollPane;
private JTableHeader mainTableHeader;
private TableColumnModel originalColumnModel,mainColumnModel,fixedColumnModel;
private TableColumn[] columns;
private int ncols,counter;
public Prova(){
counter = 0;
TableModel mainTableModel = new DefaultTableModel(5, 10);
scrollPane = new JScrollPane();
mainTable = new JTable(mainTableModel);
mainColumnModel = mainTable.getColumnModel();
fixedTable = new JTable();
fixedTable.setAutoCreateColumnsFromModel(false);
fixedTable.setModel(mainTable.getModel() );
ncols = mainTableModel.getColumnCount();
columns = new TableColumn[ncols];
for (int i=0;i<ncols;i++){
columns[i] = mainColumnModel.getColumn(i);
}
mainColumnModel = mainTable.getColumnModel();
fixedColumnModel = fixedTable.getColumnModel();
mainTableHeader = mainTable.getTableHeader();
mainTableHeader.addMouseListener( new MouseAdapter(){
#Override
public void mouseClicked(MouseEvent me){
if (SwingUtilities.isRightMouseButton(me)){
if (ncols - counter>1){
counter ++;
int col = mainTable.columnAtPoint(me.getPoint());
TableColumn column = mainColumnModel.getColumn(col);
mainColumnModel.removeColumn(column);
fixedTable.getColumnModel().addColumn(column);
scrollPane.setRowHeaderView(fixedTable);
scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, fixedTable.getTableHeader());
}
}
}
});
fixedTable.getTableHeader().addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent me){
if (SwingUtilities.isRightMouseButton(me) && me.getClickCount()== 2 ){
while (mainColumnModel.getColumnCount() > 0){
mainColumnModel.removeColumn(mainColumnModel.getColumn(0));
}
while (fixedColumnModel.getColumnCount() > 0){
fixedColumnModel.removeColumn(fixedColumnModel.getColumn(0));
}
for(int i=0;i<ncols;i++){
mainColumnModel.addColumn(columns[i]);
}
scrollPane.setRowHeaderView(null);
}
}
});
scrollPane.setViewportView(mainTable);
add(scrollPane, BorderLayout.CENTER);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Prova().setVisible(true);
}
});
}
}

A few pointers when posting a SSCCE:
for (int i=0;i<ncols;i++){
Don't be afraid to use whitespace in you code to make it more readable be separating the 3 statements of the for statement.
for (int i = 0; i < ncols; i++){
Keep the code simple and directly related to the problem:
TableModel mainTableModel = new EmployeeTableModel(listEmployees);
You question is about "moving columns", not about the data in the table so there is no need for a special TableModel and the Employee class. Just use the DefaultTableModel:
TableModel mainTableModel = new DefaultTableModel(5, 10);
Your current code won't compile because you didn't include the Employee class. By using JDK classes the code is smaller and easier to read.
The problem is when you unlock all the locked columns and you want to start to lock again, doesnt work
Your looping code is wrong. I didn't bother to figure out what was wrong. Instead I made the code simpler:
//for(int i=0;i<(ncols-counter);i++){
while (mainColumnModel.getColumnCount() > 0)
{
mainColumnModel.removeColumn(mainColumnModel.getColumn(0));
}
//for(int i=0;i<counter;i++){
while (fixedColumnModel.getColumnCount() > 0)
{
fixedColumnModel.removeColumn(fixedColumnModel.getColumn(0));
}
Another problem is your fixed table doesn't have a header so you don't know what the columns are. This is fixed by using:
scrollPane.setRowHeaderView(fixedTable);
scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, fixedTable.getTableHeader());
Now that you have a header you need to add the MouseListener to the header, not the scrollpane:
//scrollPane.addMouseListener(new MouseAdapter() {
fixedTable.getTableHeader().addMouseListener(new MouseAdapter() {
Edit:
You have a similar problem to what I fixed above. That is don't keep using variable to track values when you can use the component itself.
if (ncols - counter>1){
You never reset the value of the counter so the if condition won't be true the second time.
As I did above just use the value from the column model:
//if (ncols - counter>1){
if (mainColumnModel.getColumnCount() > 1) {
This is just basic problem solving. Put a display statement in the block of code to see if it executes when you have problems.

Related

How to add MouseListener to a table model

I have a JTable. When a user clicks on a cell another JTable is created that shows the data for the whole row of that cell, in a column format (ie the row is converted to a column).
This happens when the user clicks but its a bit irritating to happen every time so I want to make it only on a double click.
The problem is that the getSelection method of the table only takes a addListSelectionListener method and not a MouseListener. How can I do what I want?
Here is the code:
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
int selectedRow = table.getSelectedRow();
DefaultTableModel newModel = new DefaultTableModel();
String rowName = "Row: " + selectedRow;
newModel.setColumnIdentifiers(new Object[]{rowName});
for (int i = 0; i < table.getModel().getColumnCount(); i++) {
newModel.addRow(new Object[]{table.getModel().getValueAt(selectedRow, i)});
}
JTable newTable = new JTable(newModel) {
/**
*
*/
private static final long serialVersionUID = 1L;
#Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(140, 240);
}
};
// Apply any custom renderers and editors
JOptionPane.showMessageDialog(frame, new JScrollPane(newTable),
rowName, JOptionPane.PLAIN_MESSAGE);
}
}
});
This happens when the user clicks but its a bit irritating to happen every time so I want to make it only on a double click
You use a MouseListener, not a ListSelectionListener. You would check the Mouse event for a click count of 2.
Read the section from the Swing tutorial on How to Write a MouseLister for more information and working examples.
Also, a double click will start the editor by default so you want to make sure the cell is not editable. So you may need to override the isCellEditable(...) method of the table.

Removing JTextFields after dynamically adding it

My goal is to delete two JTextFields at the same time by clicking a JLabel.
I've created the textfields like this:
public void mouseClicked(MouseEvent e) {
inc++;
txtName= new JTextField();
txtNumber = new JTextField();
txtName.setName("txtName"+inc);
txtNumber.setName("txtNumber" + inc);
pnlPanel.add(txtName);
pnlPanel.add(txtNumber);
if(count>0){
x+=50;
y+=50;
txtName.setBounds(225,6+y, 182, 27);
txtNumber.setBounds(35, 6+y, 182, 27);
txtName.setName(tempBox+count);
if(pnlTxtText.getComponentCount() >9){
pnlPanel.setPreferredSize(new Dimension(450+y,50+y));
pnlPanel.add(txtStudName);
pnlPanel.add(txtStudentNumber);
frmFrame.repaint();
scrpPanel.revalidate();
}
}
frmFrame.repaint();
}
});
And this is my code for removing the textfields:
public void mouseClicked(MouseEvent e) {
int countPlace= pnlPanel.getComponentCount();
int countOfRemaining =countPlace;
pnlPanel.remove(--countOfRemaining);
frmFrame.revalidate();
pnlPanel.remove(--countOfRemaining);
frmFrame.revalidate();
}
});
Instead of deleting the txtfields on the same row, it deletes it one by one, i dont want that. Please help me. Thank you.
Call pnlPanel.revalidate() before repaint()
And don't use setBounds(). Define porper LayoutManager instead.
Instead of this,
public void mouseClicked(MouseEvent e) {
int countPlace= pnlPanel.getComponentCount();
int countOfRemaining =countPlace;
pnlPanel.remove(countOfRemaining-1);
frmFrame.repaint();
pnlPanel.remove(countOfRemaining-1);
frmFrame.repaint();
}
});
Use this
public void mouseClicked(MouseEvent e) {
int countPlace= pnlPanel.getComponentCount();
int countOfRemaining =countPlace;
pnlPanel.remove(--countOfRemaining);
frmFrame.revalidate();
pnlPanel.remove(--countOfRemaining);
frmFrame.revalidate();
}
});
The above one throws an ArrayIndeOutOfBounds Exception because the count remaining variable is not being decremented after you remove a component. Hence the index goes out of bound when you try to remove the second time.
I recommend that you add all of your JPanels to a Map (I use HashMap) in the form of <Integer, JPanel>. Name them all in order, and then just do Map.remove(Map.size() - 1) and Map.remove(Map.size() - 2).
You can also get the current set of Integers (the keys) by doing a Map.keySet();
Map<Integer, JPanel> temp = new HashMap<Integer, JPanel>();
temp.put(0, new JPanel());
temp.put(1, new JPanel());
temp.put(2, new JPanel());
temp.remove(temp.size() - 1);
Makes it much easier to maintain numerous sets of panels. In my applications I don't control them numerically, but with short names instead.

NullPointerException in Clicking a row in JTable

I want to activate a button if I click an empty 8th column in my Jtable. But I'm getting this:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at records$1.valueChanged(records.java:57)
Here's my code:
tb_records.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent event) {
int row = tb_records.getSelectedRow();
DefaultTableModel model = (DefaultTableModel)tb_records.getModel();
String hehe = (String) model.getValueAt(row, 7);
if(!hehe.equals("")) {
b_extend.setEnabled(false);
}
else {
b_extend.setEnabled(true);
}
}
});
(not an answer, I'd be talking about simple way)
change ListSelectionMode to SINGLE_...., then only one row can be selected, otherwise you have to loop inside array of selected rows
test if (row > -1) == no row is selected, if passed then you can to
for better help sooner post an SSCCE, short, runnable, compilable, with hardcoded value for JTable
I made few changes to your code, may be help.
.(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e) {
if (! e.getValueIsAdjusting() ) {
ListSelectionModel d = (ListSelectionModel)e.getSource();
if(d.getLeadSelectionIndex() != -1){
String hehe = (String) model.getValueAt(d.getLeadSelectionIndex(), 7);
b_extend.setEnabled("".equals(hehe));
}
}
}
});

JTable row totals color coded label

I have 5 JTables on different forms with arbitrary numbers of rows and I would like to have a label for each one that will show me the total number of rows in that table and also change color for 3 seconds when the row count changes. The color should go green if incrementing and red if decrementing. What would be the best way to implement this such that I do not need to duplicate too much code in each of my forms?
basically, you add a TableModelListener to the JTable's model and on receiving change events, update the corresponding labels as appropriate
some code:
public class TableModelRowStorage
// extends AbstractBean // this is a bean convenience lass of several binding frameworks
// but simple to implement directly
implements TableModelListener {
private int rowCount;
public TableModelRowStorage(TableModel model) {
model.addTableModelListener(this);
this.rowCount = model.getRowCount();
}
#Override
public void tableChanged(TableModelEvent e) {
if (((TableModel) e.getSource()).getRowCount() != rowCount) {
int old = rowCount;
rowCount = ((TableModel) e.getSource()).getRowCount();
doStuff(old, rowCount);
}
}
protected void doStuff(int oldRowCount, int newRowCount) {
// here goes what you want to do - all in pseudo-code
// either directly configuring a label/start timer
label.setText("RowCount: " + newRowCount);
label.setForeground(newRowCount - oldRowCount > 0 ? Color.GREEN : Color.RED);
timer.start();
// or indirectly by firing a propertyChange
firePropertyChange("rowCount", oldRowCount, newRowCount);
}
}

JScrollBar visible

Is there some way to know if a JScrollBar is visible or not inside a JPanel?
I mean, some times, my panel has many rectangles (think of it as buttons) and needs a scrollbar and some times it doesn't need it. I'd like to know if I can know when it is being shown.
If you extend the JPanel and add yourself the JScrollbars (horizontal and/or vertical), then you can control when they must be visible or invisible
(you can check if they are currently visible with the isvisible() function)
You can find two example of such classes that determine the need for visible scrollbar depending on their content:
JGraphPanel (its callback actionPerformed(Event e) will adjust the visibility based on a zoom factor)
Plane (its function adjustComponents() will call setVisible() on the JScrollBar if needed)
Assuming you have a reference to a JScrollPane, you should be able to just call
yourJScrollPane.getHorizontalScrollBar().isVisible()
or
yourJScrollPane.getVerticalScrollBar().isVisible()
If you need also to be notified about visibility changes than you can use a code as follows:
final JScrollPane scroll = new JScrollPane(createMyPanel());
scroll.getVerticalScrollBar().addHierarchyListener(new HierarchyListener() {
#Override
public void hierarchyChanged(HierarchyEvent e) {
if (e.getID() == HierarchyEvent.HIERARCHY_CHANGED &&
(e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0) {
System.out.println(scroll.getVerticalScrollBar().isVisible());
}
}
});
Further to the answers by VonC and Joshua, it's worth noting that isVisible() is a method on the super class Component. Also, the javadoc states:
Determines whether this component should be visible when its parent is visible.
Components are initially visible, with the exception of top level components such as Frame objects.
What this means is that until the JScrollPane is added to a sized frame, calling isVisible() on the JScrollBar will always return true.
Consider the following SSCCE:
public static void main(String[] args) {
// creates a small table in a larger scroll pane
int size = 5;
JTable table = new JTable(makeData(size), makeHeadings(size));
JScrollPane pane = new JScrollPane(table);
pane.setPreferredSize(new Dimension(200, 200));
System.out.println(pane.getVerticalScrollBar().isVisible()); // prints true
JFrame frame = new JFrame("JScrollPane Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(pane);
System.out.println(pane.getVerticalScrollBar().isVisible()); // prints true
frame.pack();
System.out.println(pane.getVerticalScrollBar().isVisible()); // prints false
frame.setVisible(true);
System.out.println(pane.getVerticalScrollBar().isVisible()); // prints false
}
private static Object[] makeHeadings(int size) {
Object[] headings = new Object[size];
for (int i=0; i<size; i++){
headings[i] = i;
}
return headings;
}
private static Object[][] makeData(int size) {
Object[][] data = new Object[size][size];
for (int i=0; i<size; i++){
for (int j=0; j<size; j++){
data[i][j] = i*j;
}
}
return data;
}
Similarly, it's worth adding that if you're adding the JScrollPane to an internal frame, then scrollBar.isVisible() will only work once the internal frame has been added to another component.

Categories