MouseListener takes ages - java

I've created a Table that is basically a JPanel with other panels as rows containing JTextArea components to display data. I didn't want to use a JTable to have full control by myself.
When a row is clicked on I want it to be highlighted through changing its border. I implemented a Mouselistener that does the job for every text area on mouse pressed. It works. It works with 1000 entries, but when I reach more, i.e. 5000 it takes ages for the listener to do his work. Obviously it gets worse the more entries I have. But this doesn't make sense to me, as I only change one rows border, no matter whether there is 1 or 10000 others.
Can anyone point me to the reason for this? Even if I put a System.out right after "mouse pressed" it takes ages for the output. So I guess its not the executed methods (that are not time consuming at all) themselves.

Related

How can I let user rearrange Jbuttons or Jtexfields with drag and drop?

I've made an equation builder program. it displays the terms with textboxes and buttons in a single line. each control is a term 3 * 3 is 2 textboxes for the numbers and a button for the *. I would like drag and drop to be implemented for changing the order of these. I'm just missing on crucial part I need to know how to detect where the control is dropped, either on the jpanel they are in or if dropped on a jbutton which physical part of the jbutton. Basically I just need to know whether to put it on the left or the right.
I currently have it kind of working where when you start to drag I make little catcher buttons appear in between each control but honestly it looks like crap and doesn't work that well.

appear and disappear for a scroll depending on a conditional

everyone, excuse my language I speak Spanish and I use google translator
I have a question I can appear and disappear as one scroll lacelda depending on size or table for example when the cell arrives at a height of "300" scrolling appears visible, if not reach 300 not appear
How I can do that?
regards
There is a Swing class called JScrollPane. There are two functions called:
scrollBar.getHorizontalScrollBar().getValue();
scrollBar.getVerticalScrollBar().getValue();
You'll either need an event listener or a loop in a separate thread to check if the scroll bar is past a certain point.
The code to make it disappear is just:
scrollBar.setVisible(false)
The code to make it reappear:
scrollBar.setVisible(true);
scrollBar.validate();
scrollBar.repaint();

Java JComboBox control scrollbar position?

I'm working on a simple java program (using NetBeans' swing GUI builder). You're supposed to select a randomly placed item from the JComboBox as fast as you can, then it will tell you your time and save it (keeping statistics and whatnot).
Problem is, once you select an item and go back to find a different item (in a new random position in the JComboBox), the scrollbar seems to have remembered it's previous position.
Since this game is supposed to be competitive, it wouldn't really be fair for the scrollbar to be in the previous player's position.
Is there any way to set the position of the scrollbar in a JComboBox?
P.S. it's a vertical scrollbar. Also, I did try to find the answer online, but couldn't find it after over two hours of searching. Any help appreciated!
Select the first item so the scrollbar goes back to the top, then set the selection to -1.
comboBox.setSelectedIndex(0);
comboBox.setSelectedIndex(-1);

Listeners and validation/repainting

So I have this problem. I have a program that creates and load files.
When I load a file into my program, I rely on a component listener that tells me when is the specific component "full", and then moves all of the components according to that.
I add each component on the top, and then listener register when parent component is "filled" and moves the bottom compoennt to a new parent it also creates. Think of it as adding a new line of text to the begining of a houndred page MSWord document.
That listener also relys on the GUI setup - if everything in memory is not painted to the screen, it grabs wrong component heights (usually being 0) and uses them in it's calculations which then come out wrong, and everything gets messed up.
Here is a flow chart of that part of my program:
(everytning happens on a JPanel in a JFrame)
trigger opening method:
{
repeat this x (lets say e.g. 100) times:
{
trigger addComponent method
{
add component
{
adding component triggers the component listner 8if there is no more room in parent)
{
move all of the components one place down, move the ones out of bounds to next "page"
repaint and revalidate whole JFrame (inside listener)
}
}
repaint and revalidate whole JFrame (part of addComponent method)
}
repaint and revalidate whole JFrame(part of opening methid, after component addition)
}
repaint and revalidate whole JFrame (as a part of opening method, final repaint/validate)
}
The reason for this may repaint/validate is that addComponent method, as well as the listener has other functions and is called in other places where that is the only (optimal) place for repainting/validating.
The problem is that JFrame doesn't get repainted until the very last(final) validate/repaint call in opeoning method. I tried adding Thread.sleep(1000) to several places in the code (after validate/repaint) to proved this.
Also, as far a I know, when a component listener is triggered, it stops at the line which triggerd it, then executes itself, and then continues from that line, right?
How do I fix this? How do I force my program to repaint/validate after each new component was added ant then again after the listener did it's job?
Reply to the first two comments:
Firstly, Thread.sleep(1000) was just to diagnose the problem. Right before Thread.sleep(1000) method was repaint/validate method, so I thought if I pause the program right after reapint() was called, after every pause, GUI would be repainted ad I would see new element added, which was not the case.
Secondly, regarding to the lengthy calculations, those calculations aren't that long (executing this with 20 components gets momentary results, though not ones that are desired). Also, that calculations require removing and adding components to the GUI quite often (every 10-20 lines), so Incorporating SwingWorker in that is close to impossible, and not needed.
Thirdly, I think you missed the whole point. Length of execution is not the real problem here, nor the freezing of the GUI (whic doesn't really occurr, not long enough to be noticable, anyway). The problem is that repaint/validate was called in the loop for total of 3-4 times for every component (cca. 60-80 times if I open a file with 20 components), and the only time I saw it being executed is when the last time it was called, after the loop...
I even put System.out.println("something") method right before and right after the repaint/validate. It printed out "something" twice, but repaint/validate never happened.
I can see any issue with add / remove / modify JComponents in the container (JFrame --> JPanel e.i.)
depends if container (JPanel e.i.) is placed in JScrollPane
depends if you need to call pack() after add / remove / modify JComponents in the container, and then to change size of JFrame (for example) on the screen

Java JPanel redraw issues

I have a Java swing application with a panel that contains three JComboBoxes that do not draw properly.
The combox boxes just show up as the down arrow on the right side, but without the label of the currently selected value.
The boxes will redraw correctly if the window is resized either bigger or smaller by even one pixel.
All of my googling has pointed to calling revalidate() on the JPanel to fix this, but that hasn't worked for me.
Calling updateUI() on the JPanel has changed it from always displaying incorrectly to displaying incorrectly half of the time.
Has anyone else seen this and found a different way to force a redraw of the combo boxes?
Can you give us some more information on how you add the combo boxes to the JPanel? This is a pretty common thing to do in Swing so I doubt that it's a JVM issue but I guess anything is possible.
Specifically, I would double check to make sure you're not accessing the GUI from any background threads. In this case, maybe you're reading the choices from a DB or something and updating the JComboBox from a background thread, which is a big no-no in Swing. See SwingUtils.invokeLater().

Categories