Skip to main content

Remove column from column control popup menu

9 replies [Last post]
d_l
Offline
Joined: 2009-10-05

Is it possible to control whether a column should be available in a column control popup menu? I'm using the JXTreeTable and uses the default Column Control (setColumnControlVisible(true)), but there is one column that must always be visible and not changed by the user. Anyone that have any hints?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
1ac0
Offline
Joined: 2013-10-02

d_l wrote:
Is it possible to control whether a column should be available in a column control popup menu? I'm using the JXTreeTable and uses the default Column Control (setColumnControlVisible(true)), but there is one column that must always be visible and not changed by the user. Anyone that have any hints?

at first thank others for their answers. my solution (to be correct, it's just a hack) based on answers from this thread:

first way:

myTable().getColumnExt(_column_number_).setHideable(false);

this works smooth but has one ui drawback: text in menu is gray and thick is black - bad user experience.

so try to fix it, text will be gray and thick won't be here:

public class MyTable extends JXTable
{
public MyTable(AbstractTableModel model)
{
super(model);
//remove items for horizontal scrollbar, pack and packall
this.getActionMap().remove("column.horizontalScroll");
this.getActionMap().remove("column.packAll");
this.getActionMap().remove("column.packSelected");
//first two columns won't be hiddeable
ColumnControlButton controlButton = new ColumnControlButton(this)
{
@Override
protected ColumnControlPopup createColumnControlPopup()
{
return (new NFColumnControlPopup());
}

class NFColumnControlPopup extends DefaultColumnControlPopup
{
@Override
public void addVisibilityActionItems(List<? extends AbstractActionExt> actions)
{
for(int i = 0; i < actions.size(); i++)
{
AbstractActionExt action = actions.get(i);
JCheckBoxMenuItem chk = new JCheckBoxMenuItem(action);
//Disabling unwanted items but they will be still shown for smooth user experience
if(i == 0 || i == 1)
{
chk.setEnabled(false);
chk.setSelected(false);
//chk.setIcon(new ImageIcon(Icons.class.getResource("check.png")));
}
else
{
chk.setSelected(true);
}
chk.addItemListener(action);
super.addItem(chk);
}
}
}
};
this.setColumnControl(controlButton);
}
}
UsmanHussain
Offline
Joined: 2013-08-23

ColumnControlButton controlButton = new ColumnControlButton(table) {
@Override
protected ColumnControlPopup createColumnControlPopup() {
return (new NFColumnControlPopup());
}

class NFColumnControlPopup extends DefaultColumnControlPopup {

@Override
public void addVisibilityActionItems(List<? extends AbstractActionExt> actions) {
for (int i = 0; i < actions.size(); i++) {
AbstractActionExt action = actions.get(i);
//This if statement show that which column you want to remove from JPopMenu you write your code here.
if (i == 6) {
continue; // i will use continue key word because loop currently continue and i want to remove column number six.
}
JCheckBoxMenuItem chk = new JCheckBoxMenuItem(action);
chk.setSelected(true);
chk.addItemListener(action);
super.addItem(chk);
}

}
}
};

Kindly give me a feed back

kleopatra
Offline
Joined: 2003-06-11

what's the problem you want to solve?

BTW, that manual creation of a menuitem is .. wrong, or at least completely against the winds of how the columnControl/popup is working ;-)

Cheers
Jeanette

bbrtki
Offline
Joined: 2009-07-08

You could always hide that control and instead show a popupmenu that's activated on right clicking the header. You can fill this with checkboxmenuitems to display whether a column is visible or not.

You'd be losing some of the default behavior that's available in JXTable for this menu, but you'd gain a highly customizable menu in return. One that, even would allow you to add sub menus and other features without so much of a sweat.

kleopatra
Offline
Joined: 2003-06-11

sure, you could always start from scratch and re-invent the wheel ;-)

Jeanette

bbrtki
Offline
Joined: 2009-07-08

It's the best way to come up with innovations. :)

Who needs wheels when you could fly, right?
Of course, you'll end up crashing a time or two along the way, but eventually you may end up with something bigger and better that everyone might benefit from.

[i]
The airplane is just a bunch of sticks and wires and cloth, a tool for learning about the sky and about what kind of person I am, when I fly. An airplane stands for freedom, for joy, for the power to understand, and to demonstrate that understanding. Those things aren't destructable.

— Richard Bach, 'Nothing by Chance,' 1963[/i]

kleopatra
Offline
Joined: 2003-06-11

not supported yet, we have an open issue (#803-swingx) for such an enhancement: support a hideable property

meanwhile, you can go do it yourself and create that extended columnExt in a custom ColumnFactory, something like (uncompiled code-snippets)

[code]

class MyTableColumnExt extends TableColumnExt {

// add a hideable property, make it true by default
@Override
public boolean isVisible() {
return isHideable() ? super.isVisible() : true;
}

}
class MyColumnFactory extends ColumnFactory {

@Override
void configureTableColumn(TableModel, TableColumnExt) {
super.configure(..);
if ((columnExt instanceof MyTableColumnExt) && (columnExt.getModelIndex() == alwaysVisibleColumn))
columnExt.setHideable(false);
}
}

@Override
MyTableColumnExt createTableColumn(int column) {
return new MyTableColumnExt(column);
}
}

// use
table.setColumnFactory(new MyColumnFactory());
table.setModel(someModel);
[/code]

This issue has some debate about what else might be involved/alternatives.

HTH
Jeanette

d_l
Offline
Joined: 2009-10-05

Thanks :-) I also created an extension of the [b]ColumnControlButton[/b] to override the [b]createColumnVisibilityAction[/b] method in order to determine if the column should be available/togglable in the popupmenu, something like:

.
.
class MuColumnControlButton extends ColumnControlButton
{
protected ColumnVisibilityAction createColumnVisibilityAction(TableColumn column)
{
if (column instanceof MyTableColumnExt
&& ((MyTableColumnExt)column).isHidden)
return null;
return super.createColumnVisibilityAction(column);
}
}
.
.
// usage
table.setColumnControl(new ColumnControlButton());
.
.

kleopatra
Offline
Joined: 2003-06-11

great you made it work!

Would you please comment the issue I mentioned above with a link to this thread? Always good to have a real-world requirement at hand (and Karl will be happy to see the default exclusion in the columnControl as well :-)

Thanks
Jeanette