How to show/hide certain JTable columns based on selection in JComboBox - swing

I have a JTable and a JComboBox. I want certain columns to hide when I select one item in the combobox and the same hidden columns to reappear when I select the other item in the combobox. I write,
jTable1.getColumnModel().getColumn(8).setMinWidth(0)
jTable1.getColumnModel().getColumn(8).setMaxWidth(0)
jTable1.getColumnModel().getColumn(8).setWidth(0)
for hiding the column, but when I again write
jTable1.getColumnModel().getColumn(8).setMinWidth(100)
jTable1.getColumnModel().getColumn(8).setMaxWidth(100)
jTable1.getColumnModel().getColumn(8).setWidth(100)
the hidden columns do not become visible.

Reason is that both setMin/setMax enforce the relation
min <= width <= max
That is the order of method calling matters
// hiding
column.setMinWidth(0);
column.setMaxWidth(0);
// showing
column.setMaxWidth(100);
column.setMinWidth(100);
Note that you need not call setWidth, that's handled internally.
That said: forcing the sizes is .. a hack. Consider using a clean solution, f.i. a framework like SwingX which has (amongst other niceties :-) full-fledged support for column hiding

Use JTable#removeColumn and JTable#addColumn. These operations only affect the view side, not the model side

what's a problem in above code?
In addition to kleopatra's helpful insight, documented here, some L&Fs are more or less cooperative. For example, com.apple.laf.AquaLookAndFeel always leaves enough width to drag after setMinWidth(0), although the column can be forced to zero width manually.

for (int i = 0; i < 2; i++) {
jTable1.getColumnModel().getColumn(8).setMinWidth(100)
jTable1.getColumnModel().getColumn(8).setMaxWidth(100)
jTable1.getColumnModel().getColumn(8).setWidth(100)
}
The columns will become visible.

Related

Find element by its calculated properties (e.g. position and size withing ranges) [duplicate]

This question already has answers here:
Get element with a randomized class name
(2 answers)
Closed 4 years ago.
In an attempt to make web scraping with a headless browser more resilient to site changes, I'd like to combine technical properties of the elements with their visual characteristics.
E.g. when looking for a search bar, I'd like to look for a "big (>50% width), visible (:visible) text input field (<input type="text">) in the upper half of the screen/rendered page." Then, when looking for the submit button, I'd like to find a button located near the aforementioned search bar.
Is there any way to set up this kind of search criterion?
AFAICS, CSS selectors and XPath can only search by predefined parameters (tag, id, class, attributes), not by calculated ones.
The best idea I currently have is to search by predefined parameters, then filter the result further by getting size, position and such for each result and comparing them to the desired ranges. This is rather slow oftentimes since I have to use expressions like *[text()="visible text"] to not rely on technical details that are subject to change without notice.
Here are a few examples of ways to find your wanted element. All below examples are based on the assumption that you have an element that looks a little like this (can be different type and css elsewhere, but basically that you have an element somewhere with some styling and some attribute).
<div mycustomattribute="login" style="width:calc(5cm - 3cm)"></div>
Note that the below examples aren't necessarily all I the ways I can give you, it's just the ones I could think of on the fly, if your problem isn't resolved using these I can probably think of one or two more ways to solve your problem.
Selecting using a custom attribute
You can set any attribute you want on any element you want. For example, if you want <div mycustomattribute="hello"> and then querySelect that, it's totally valid.
var test = document.querySelect("div[mycustomattribute=login]")
The above script will select only the div that has an attribute name with the value login. I think you already know of this method but figured I'd mention it because it's by far the easiest, least hacky way of finding a specific element, if you can set an attribute on your element that is.
Select using position
Lets say you want to select the nearest element that is 50 px to the right of the element you selected.
var base = document.querySelect("div[name=login]")
// Get Y coordinate of base element
var y = base.getBoundingClientRect().top;
// Get X coordinate of base element on its right side, since we're gonna look to the right of it
var x = base.getBoundingClientRect().right;
// Find the element that is 50 pixels to the right of our base element
var element = document.elementFromPoint(x + 50, y);
Select using CSS values
This is more tricky but certainly possible. You are correct in that you can't just run querySelector to find an element based on a CSS value (calculated or otherwise), but you can run the calculation yourself to get the value your desired element should have and then just loop through them to get the one you want.
So, for example:
var divs = document.querySelectorAll('div');
var element = null;
for (i = 0; i < divs.length; ++i) {
/* We assume you know the result of the calculated value, either because it's
a static result (e.g. `5cm - 3cm`), or because you rerun the calculation in
javascript to find out what its result is.
Note that you can use whatever style you want here to find the div, like
"visible" or "display" or whatever you want, just set up the proper if
statements.
*/
if(div.style.width = "2cm") {
element = div;
break;
}
}
References
This is a little side note but try to use mozilla instead of w3schools, mozilla is way better for references. I was hesitant too at first to make the jump to mozillas documentation but it really is way better once you learn how to use it.
https://developer.mozilla.org/en-US/docs/Web/API/Document
https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll
https://developer.mozilla.org/en-US/docs/Web/API/Document/elementFromPoint
https://css-tricks.com/snippets/javascript/loop-queryselectorall-matches/

Disable/remove close icon on Kendo Grid's default group column

I am using Kendo UI Grid for one of my solutions. I have this one task/requirement where I should be able to give a default grouping for the grid, and the user should not be able to remove this grouping from the UI.
There is one way of achieving this as in this jsFiddle example: http://jsfiddle.net/siva_hari/xzLfgsba/4/
If you look at the following example, the grouping is true but the grouping can be changed by clicking on k-group-delete icon or by dragging the grouped column back into the grid.
http://jsfiddle.net/siva_hari/xzLfgsba
group: [{field: "ShipName"}],
groupable: false
But there is one one problem with this solution, you do not have the group header(because the groupable is set to false) and you cannot sort based on the group it self.
Is there a way to show group header and also disable further changes to grouping of the grid?
Thank you.
Disabling the close handle (X button) is simple, just hide it. I can hide it programmatically but using CSS is much more effective.
span.k-icon.k-group-delete{
display:none;
}
Next step is to get rid of the draggable property of the grouping indicator. To do this, we destroy the draggable container. We need to do this after dataBound because the grid properties are re-applied every time dataBound is called.
dataBound:function(e){
setTimeout(function(){
//get the indicator header
var groupIndicatorHeader = $('.k-group-indicator').parent();
if(!groupIndicatorHeader) return;
//check if it is draggable eneabled
var kendoDraggableObj = $(groupIndicatorHeader).data('kendoDraggable');
if(kendoDraggableObj) kendoDraggableObj.destroy();
},0);
}
setTimeout is necessary to make it run after all dataBound and natural kendo code has finished running. Here is the fiddle for this solution.
You will probably notice that this solution is very hacky, and it is. But sometimes you just gotta roll with it to get the customization you need.

User-friendly, Form input that always totals 100

I have a dynamically generated form that needs to gather several numerical values from a user that totals 100 (%). I thought about writing a script/algorithm that adjusts the remaining values of several text fields - so that when the user changes one value, the remaining values dynamically change (so that the values always total 100).
However, instead of text fields, I would really prefer something more user-friendly like sliders that move when one slider is adjusted or some other user-friendly widget (like an adjustable pie chart(?) that always totals 100%).
The script needs to work in late version of Firefox, Chrome and IE. I read somewhere that HTML5 sliders don't work in Firefox.
I am open to different solutions.
Am not getting your question clearly, assuming that you need a value slider which a person will slide and automatically the bar will increment every time by 1, so try using jQuery and Ajax, will suit your requirements, you can check out few over here.
Sliders work fine in FireFox - try http://www.colorpicker.com/
You can use the jQuery UI slider, if you like. You can register a custom function on change event which easily adjusts the other sliders.
So in the end you got something like this (some pseudo-code in it):
$( "#slider1" ).slider({
change: function(event, ui) {
var i = 100 - value_of_slider1 / number_of_sliders_remaining;
$("#slider2).setValue(i);
$("#slider3).setValue(i);
}
});
Of course this can be implemented a lot more sophisticated. Just to give you the basic idea. Depends on your markup.
I have not had problems with sliders in FireFox.
You can have the various sliders/input devices calculate on each other to get the output by division or whatever, then have the last/smallest number be subtracted rather than divided from the total. This way your other calculations will be accurate to the accuracy and the least significant value can make up the rest.

How to divide a large text into multiple chunks, all with the same max-height?

CSS3's column-module allows you to divide your text into a multiple columns. Either by
1) specifying the column-height property (all columns will have the same author-defined height, the column count is dynamic)
or,
2) specifying the column-count property (all columns have the same computer-generated height, the number of columns is defined by the author).
What I would like to have is option 1, but instead of having the columns next to each-other I'd like to have them underneath each other. This way they wouldn't really be columns, but more like rows with a defined height.
This way the text will be divided into pages of all the same height. (Like when you print out a webpage.)
Any ideas on how to achieve this? ( My project only requires webkit-support. )
The column module won't do that. You would be better off declaring a class on a div with the a declared height. If you're looking for dynamic columns, you may need to do some programming via js or php.
By the way, I did play with the following idea which sort of works:
Use a multi-column DIV to split the text into different same-height chunks.
Copy the multi-column DIV X times, where X = number of generated columns.
On each DIV, only show one column. (by using overflow: hidden and a width)
Position the columns so they are underneath each-other.
This works, but is VERY slow as you can imagine.
Perhaps there's a way to show the same multi-column DIV multiple times with different view-ports, without copying the whole DOM tree?
JS seems like the way to go. Check out this jQuery function from the Filament Group. Masonry might be of interest too.
// make columns equal height
function equalHeight(group) {
tallest = 0;
group.each(function() {
thisHeight = $(this).height();
if(thisHeight > tallest) {
tallest = thisHeight;
}
});
group.height(tallest);
}

How can I make the column widths of a list box in MS-Access2007 automatically size themselves correctly?

Background info:
I was handed a "Tool", which was made using MS-Access 2007, and asked to add some things... The tool is basically a collection of options for querying a database. On a form titled CreatedReport there is a listbox that is bound to a table called analyzed which has all of resulting data from the query/queries that ran. The original creator of this tool set the column widths to specific values but with the new collection of possible results, those widths are very far off.
Desired Outcome:
The final result I want to achieve is, of course, to have the columns be the correct widths for the info that is in the columns. As long as that is achieved, I really don't care which route I have to take to get there.
Question:
How can I get the columns in a listbox in MS-Access 2007 to be sized appropriately for each use? Is there an auto-size feature I haven't stumbled across yet or do I need to hard code the set of column widths for each group? This wouldn't be too hard to do since there would only be about 4 or 5 different groups but I would prefer for the process to be automatic if at all possible.
Another approach would be to have the results returned in a sub form datasheet view, then the user can adjust the column widths also to set the widths automatically use code like this:
Example
This example takes effect in Datasheet view of the open Customers form. It sets the column to fit the size of the visible text.
Forms![Customers]![Address].ColumnWidth = -2
You could put this code into the Current Event of the sub form.
I don't think that Robert Harvey's answer is actually responsive to your question.
What you need to do is:
calculate the maximum length of the values in each column,
AND
figure out, based on the font in use, how wide the column should be.
Note that you may not actually want to set it to the maximum width if the value exceeds a certain threshold.
I don't know to do the second taks, but I suspect Stephen Lebans has already done the work on it. You might want to search his website for it.
Last time I checked, you still had to write code for this.
Your best bet is to use a resizer someone has already written. Here is a good one. It's old, but it shoul still work:
http://www.jamiessoftware.tk/resizeform/rf_jump.html
This is a quick solution that should help when you want to set up listview columns of different widths, and you know in advance the widths you want (eg, you know that column X will always be a 2-character State abbreviation, and column Y will always be a city name).
Just supply all the widths as a single semi colon-delimited string. Code each width as a number and a unit, such as 'in' or 'cm'. This worked well for me: Me.lsvPayHist.ColumnWidths = "1.0 in;0.8 in;1.0 in;1.0 in;2.0 in"