Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
167 views
in Technique[技术] by (71.8m points)

java - Make JButton invisible but respect its original space

I am using a GridBagLayout and I want to set some buttons as invisible (i.e. they cannot be seen nor clicked), but when I do so using wdButton.setVisible(false) the rest of the buttons move. I would like to leave the space originally occupied by the invisibilized button blank and keep the rest unaltered (similar problem to the one presented here). The solutions proposed there are not useful to me, since I do not want to change the layout.

For the moment, I have been able to do so by using the following lines of code:

wdButton.setText("");
wdButton.setOpaque(false);
wdButton.setContentAreaFilled(false);
wdButton.setBorderPainted(false);
wdButton.setEnabled(false);

Is there a shorter way of achieving this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Is there a shorter way of achieving this?

Create a method and pass in the button as a parameter.

The solutions proposed there are not useful to me, since I do not want to change the layout.

The solution did not involve changing the layout manager for then entire panel. It involved adding a panel using a CardLayout containing your button and an empty panel to your main panel.

I want to set some buttons as invisible

You could replace the button with another component:

GridBagConstraints gbc = layout.getConstraints( button );
panel.remove( button );
panel.add( new JLabel(" ");
panel.revalidate();

So as you can see whatever you do it will require multiple lines of code, so pick the easiest solution and just move the code to a method so that you only need to use a single statement for each component you want to hide.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...