Why is the tab-order "backwards" when tabbing over Java components inside a JPanel?

function testJavaFocus
% Illustration of tab-order focus problem.
% Run this and start hitting tab. Observe that tab-order goes "backwards"
% when traversing through the components in the JPanel. WHY!!!??!?!?!?
hfig = figure;
components = [];
%%Add JTextField
text = javaObjectEDT('javax.swing.JTextField',6);
text.setFocusable(true);
components{end+1} = text;
%%Add JTextField
text = javaObjectEDT('javax.swing.JTextField',6);
text.setFocusable(true);
components{end+1} = text;
%%Add JPanel - we will add 3 components to this panel to illustrate focus problem
panel = javaObjectEDT('javax.swing.JPanel');
panel.setBackground(java.awt.Color.GREEN);
text = javaObjectEDT('javax.swing.JTextField',6);
text.setFocusable(true);
panel.add(text);
text = javaObjectEDT('javax.swing.JTextField',6);
text.setFocusable(true);
panel.add(text);
text = javaObjectEDT('javax.swing.JTextField',6);
text.setFocusable(true);
panel.add(text);
components{end+1} = panel;
%%Add JTextField
text = javaObjectEDT('javax.swing.JTextField',6);
text.setFocusable(true);
components{end+1} = text;
%%Add JTextField
text = javaObjectEDT('javax.swing.JTextField',6);
text.setFocusable(true);
components{end+1} = text;
%%Add all components to the figure via javacomponent()
x = 10;
padding = 3;
for i = 1:length(components)
prefDim = components{i}.getPreferredSize();
widpos = [x,10,prefDim.width,prefDim.height];
[jh, hg] = javacomponent(components{i},widpos,hfig);
x = x + prefDim.width + padding;
end
end

답변 (1개)

the cyclist
the cyclist 2013년 8월 7일
I don't know much about MATLAB GUIs, much less Java-based ones, but maybe this page will shed some light?

댓글 수: 1

Thanks for the quick response but unfortunately no it doesn't help me in this case. I have written an equivalent snippet using only matlab controls (uicontrol and uipanel) and it tabs as it should. I just need to understand why the FocusTraversalPolicy (of the figure) acts in such a bizarre manner when tabbing over a JPanel. I thought the uipanel was based on a JPanel underneath the hood. Apparently something is different :(
On a side note if I create a gui in the exact same way inside of a JFrame it tabs as it should as well. Its only when I add it to a matlab figure (using javacomponent) that this behavior rears its ugly head.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

질문:

2013년 8월 7일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by