Скрыть и изменить размер панели в GridBagLayout во время выполненияJAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 Скрыть и изменить размер панели в GridBagLayout во время выполнения

Сообщение Anonymous »

У меня есть JPanel PanelOpslog с GridBagLayout.
На этой панели есть 2 Panel. 1 сверху и один снизу.
panelSearchOpsLog
---------------------
panelShiftTeamLogging

panelSearchOpsLog настроен на рост вниз и вправо
panelOpslog = new JPanel();
tabbedPane.addTab("Opslog", null, panelOpslog, null);
GridBagLayout gbl_panelOpslog = new GridBagLayout();
gbl_panelOpslog.columnWidths = new int[]{1155, 0};
gbl_panelOpslog.rowHeights = new int[]{212, 401, 0};
gbl_panelOpslog.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gbl_panelOpslog.rowWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
panelOpslog.setLayout(gbl_panelOpslog);

....

panelShiftTeamLogging = new JPanel();
GridBagConstraints gbc_panelShiftTeamLogging = new GridBagConstraints();
gbc_panelShiftTeamLogging.fill = GridBagConstraints.BOTH;
gbc_panelShiftTeamLogging.gridx = 0;
gbc_panelShiftTeamLogging.gridy = 1;
panelOpslog.add(panelShiftTeamLogging, gbc_panelShiftTeamLogging);
panelShiftTeamLogging.setLayout(null);

....

panelSearchOpsLog = new JPanel();
panelSearchOpsLog.setFont(new Font("Tahoma", Font.PLAIN, 10));
panelSearchOpsLog.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, new Color(255, 255, 255), new Color(160, 160, 160)), "Search Opslog", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
GridBagConstraints gbc_panelSearchOpsLog = new GridBagConstraints();
gbc_panelSearchOpsLog.fill = GridBagConstraints.BOTH;
gbc_panelSearchOpsLog.insets = new Insets(0, 0, 5, 0);
gbc_panelSearchOpsLog.gridx = 0;
gbc_panelSearchOpsLog.gridy = 0;
panelOpslog.add(panelSearchOpsLog, gbc_panelSearchOpsLog);
GridBagLayout gbl_panelSearchOpsLog = new GridBagLayout();
gbl_panelSearchOpsLog.columnWidths = new int[]{129, 0, 0};
gbl_panelSearchOpsLog.rowHeights = new int[]{30, 160, 0};
gbl_panelSearchOpsLog.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
gbl_panelSearchOpsLog.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
panelSearchOpsLog.setLayout(gbl_panelSearchOpsLog);

Теперь мне нужна возможность сделать PanelShiftTeamLogging невидимым, чтобы Панель 1 могла использовать все пространство.
Я поместил этот код в прослушиватель действий с кнопки. Он скрывает PanelShiftTeamLogging, но размер PanelSearchOpsLog не изменяется
btnHideShow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
boolean isVisible = panelShiftTeamLogging.isVisible();
panelShiftTeamLogging.setVisible(!isVisible);

panelOpslog.revalidate();
panelOpslog.repaint();

panelOpslog.getParent().revalidate();
panelOpslog.getParent().repaint();
}
});

PS: я хочу остаться с GridBagLayout
EDIT: рабочий пример
import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.GridBagLayout;
import javax.swing.JPanel;
import java.awt.GridBagConstraints;
import javax.swing.JButton;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;

public class Test {

private JFrame frame;
private JTabbedPane tabbedPane;
private JPanel panel;
private JPanel panelTop;
private JButton btnHideShow;
private JPanel panelBottom;
private JTextArea textArea;
private JTable table;
private JScrollPane scrollPaneOnToPanel;
private JScrollPane scrollPaneOnBottomPanel;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test window = new Test();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public Test() {
initialize();
}

/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 1002, 533);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{988, 0};
gridBagLayout.rowHeights = new int[]{465, 0};
gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
frame.getContentPane().setLayout(gridBagLayout);

tabbedPane = new JTabbedPane(JTabbedPane.TOP);
GridBagConstraints gbc_tabbedPane = new GridBagConstraints();
gbc_tabbedPane.fill = GridBagConstraints.BOTH;
gbc_tabbedPane.gridx = 0;
gbc_tabbedPane.gridy = 0;
frame.getContentPane().add(tabbedPane, gbc_tabbedPane);

panel = new JPanel();
tabbedPane.addTab("New tab", null, panel, null);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[]{983, 0};
gbl_panel.rowHeights = new int[]{189, 206, 0};
gbl_panel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gbl_panel.rowWeights = new double[]{1.0, 1.0, Double.MIN_VALUE};
panel.setLayout(gbl_panel);

panelTop = new JPanel();
GridBagConstraints gbc_panelTop = new GridBagConstraints();
gbc_panelTop.fill = GridBagConstraints.BOTH;
gbc_panelTop.insets = new Insets(0, 0, 5, 0);
gbc_panelTop.gridx = 0;
gbc_panelTop.gridy = 0;
gbc_panelTop.weighty = 0; // Hovercraft Full Of Eels suggestion
gbc_panelTop.gridheight = 1; // Hovercraft Full Of Eels suggestion
panel.add(panelTop, gbc_panelTop);

GridBagLayout gbl_panelTop = new GridBagLayout();
gbl_panelTop.columnWidths = new int[]{449, 0};
gbl_panelTop.rowHeights = new int[]{21, 0, 0};
gbl_panelTop.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gbl_panelTop.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
panelTop.setLayout(gbl_panelTop);

btnHideShow = new JButton("Hide / Show the lower panel. The Table should also use the space from the lower panel");
btnHideShow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
boolean isVisible = panelBottom.isVisible();
panelBottom.setVisible(!isVisible);

panel.revalidate();
panel.repaint();

panel.getParent().revalidate();
panel.getParent().repaint();
}
});

GridBagConstraints gbc_btnHideShow = new GridBagConstraints();
gbc_btnHideShow.insets = new Insets(0, 0, 5, 0);
gbc_btnHideShow.anchor = GridBagConstraints.NORTHWEST;
gbc_btnHideShow.gridx = 0;
gbc_btnHideShow.gridy = 0;
panelTop.add(btnHideShow, gbc_btnHideShow);

scrollPaneOnToPanel = new JScrollPane();
GridBagConstraints gbc_scrollPaneOnToPanel = new GridBagConstraints();
gbc_scrollPaneOnToPanel.fill = GridBagConstraints.BOTH;
gbc_scrollPaneOnToPanel.gridx = 0;
gbc_scrollPaneOnToPanel.gridy = 1;
panelTop.add(scrollPaneOnToPanel, gbc_scrollPaneOnToPanel);

table = new JTable();
String[] columns = {"Column A", "Column B"};
table.setModel(new DefaultTableModel(columns, 60));
scrollPaneOnToPanel.setViewportView(table);

panelBottom = new JPanel();
panelBottom.setLayout(null);
GridBagConstraints gbc_panelBottom = new GridBagConstraints();
gbc_panelBottom.fill = GridBagConstraints.BOTH;
gbc_panelBottom.gridx = 0;
gbc_panelBottom.gridy = 1;
gbc_panelBottom.weighty = 1.0; // Hovercraft Full Of Eels suggestion
panel.add(panelBottom, gbc_panelBottom);

scrollPaneOnBottomPanel = new JScrollPane();
scrollPaneOnBottomPanel.setBounds(0, 0, 973, 233);
textArea = new JTextArea();
scrollPaneOnBottomPanel.setViewportView(textArea);
panelBottom.add(scrollPaneOnBottomPanel);
}
}



Подробнее здесь: https://stackoverflow.com/questions/789 ... ng-runtime
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»