How to avoid components overlapping and make them smaller - java

I am designing a window with GridBagLayout but when I run it the components of the window are quite big when I have a ScrollPane. When I don't have scroll and window is smaller the components overlap.
This is the code I have:
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{922, 0};
gridBagLayout.rowHeights = new int[]{148, 0};
gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
setLayout(gridBagLayout);
JPanel panel_2 = new JPanel();
GridBagConstraints gbc_panel_2 = new GridBagConstraints();
gbc_panel_2.fill = GridBagConstraints.BOTH;
gbc_panel_2.gridx = 0;
gbc_panel_2.gridy = 0;
add(panel_2, gbc_panel_2);
GridBagLayout gbl_panel_2 = new GridBagLayout();
gbl_panel_2.columnWidths = new int[] {0};
gbl_panel_2.rowHeights = new int[] {0, 0};
gbl_panel_2.columnWeights = new double[]{1.0, 1.0};
gbl_panel_2.rowWeights = new double[]{1.0, 1.0};
panel_2.setLayout(gbl_panel_2);
JPanel panel_11 = new JPanel();
GridBagConstraints gbc_panel_11 = new GridBagConstraints();
gbc_panel_11.weightx = 1.0;
gbc_panel_11.weighty = 1.0;
gbc_panel_11.gridwidth = 2;
gbc_panel_11.fill = GridBagConstraints.BOTH;
gbc_panel_11.gridx = 0;
gbc_panel_11.gridy = 1;
panel_2.add(panel_11, gbc_panel_11);
GridBagLayout gbl_panel_11 = new GridBagLayout();
gbl_panel_11.columnWidths = new int[] {0};
gbl_panel_11.rowHeights = new int[] {0};
gbl_panel_11.columnWeights = new double[]{1.0};
gbl_panel_11.rowWeights = new double[]{1.0};
panel_11.setLayout(gbl_panel_11);
JPanel panel_8 = new JPanel();
panel_8.setBorder(new LineBorder(new Color(0, 0, 0)));
GridBagConstraints gbc_panel_8 = new GridBagConstraints();
gbc_panel_8.weightx = 1.0;
gbc_panel_8.weighty = 1.0;
gbc_panel_8.fill = GridBagConstraints.BOTH;
gbc_panel_8.gridx = 0;
gbc_panel_8.gridy = 0;
panel_11.add(panel_8, gbc_panel_8);
GridBagLayout gbl_panel_8 = new GridBagLayout();
gbl_panel_8.columnWidths = new int[] {0};
gbl_panel_8.rowHeights = new int[] {0, 0};
gbl_panel_8.columnWeights = new double[]{1.0};
gbl_panel_8.rowWeights = new double[]{1.0, 1.0};
panel_8.setLayout(gbl_panel_8);
JPanel panel_14 = new JPanel();
GridBagConstraints gbc_panel_14 = new GridBagConstraints();
gbc_panel_14.weighty = 0.2;
gbc_panel_14.fill = GridBagConstraints.BOTH;
gbc_panel_14.gridx = 0;
gbc_panel_14.gridy = 0;
panel_8.add(panel_14, gbc_panel_14);
GridBagLayout gbl_panel_14 = new GridBagLayout();
gbl_panel_14.columnWidths = new int[] {0, 0, 0, 0, 0};
gbl_panel_14.rowHeights = new int[]{0, 0, 0};
gbl_panel_14.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
gbl_panel_14.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
panel_14.setLayout(gbl_panel_14);
Label label_7 = new Label("Family");
GridBagConstraints gbc_label_7 = new GridBagConstraints();
gbc_label_7.insets = new Insets(0, 40, 0, 5);
gbc_label_7.gridx = 0;
gbc_label_7.gridy = 1;
panel_14.add(label_7, gbc_label_7);
JPanel panel_16 = new JPanel();
GridBagConstraints gbc_panel_16 = new GridBagConstraints();
gbc_panel_16.weighty = 0.8;
gbc_panel_16.fill = GridBagConstraints.BOTH;
gbc_panel_16.gridx = 0;
gbc_panel_16.gridy = 1;
panel_8.add(panel_16, gbc_panel_16);
GridBagLayout gbl_panel_16 = new GridBagLayout();
gbl_panel_16.columnWidths = new int[] {0, 0, 0, 0};
gbl_panel_16.rowHeights = new int[] {0, 0};
gbl_panel_16.columnWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
gbl_panel_16.rowWeights = new double[]{0.0, Double.MIN_VALUE};
panel_16.setLayout(gbl_panel_16);
TextArea textArea_2 = new TextArea();
GridBagConstraints gbc_textArea_2 = new GridBagConstraints();
gbc_textArea_2.weighty = 0.5;
gbc_textArea_2.weightx = 0.5;
gbc_textArea_2.fill = GridBagConstraints.BOTH;
gbc_textArea_2.insets = new Insets(0, 0, 0, 5);
gbc_textArea_2.gridx = 0;
gbc_textArea_2.gridy = 0;
panel_16.add(textArea_2, gbc_textArea_2);
textArea_2.setText("TEXT...");
Label label_9 = new Label("Language");
GridBagConstraints gbc_label_9 = new GridBagConstraints();
gbc_label_9.weightx = 0.25;
gbc_label_9.fill = GridBagConstraints.BOTH;
gbc_label_9.insets = new Insets(0, 0, 0, 5);
gbc_label_9.gridx = 1;
gbc_label_9.gridy = 0;
panel_16.add(label_9, gbc_label_9);
Choice choice_13 = new Choice();
GridBagConstraints gbc_choice_13 = new GridBagConstraints();
gbc_choice_13.weightx = 0.25;
gbc_choice_13.fill = GridBagConstraints.HORIZONTAL;
gbc_choice_13.gridx = 2;
gbc_choice_13.gridy = 0;
panel_16.add(choice_13, gbc_choice_13);
And the result is:
Overlapping components
What can I do to stop the components overlapping?
How can I make the components smaller?

Related

Program runs good in IDE but not as an exported file [duplicate]

This question already has answers here:
I can run .jar files through cmd, but I cannot double click them [closed]
(7 answers)
Closed 2 years ago.
My MineSweeper project works perfectly in the IDE, but it doesn't work when I export is as "Runnable Jar File" in Eclipse and executing it afterwards.
My MANIFEST.MF
I also tried to navigate via cmd there and do javac Minesweeper.jar.
Console says it cant find the command "javac".
If i cd into my folder and just do java Minesweeper.jar it says: "Cant find or load Mainclass Minesweeper.jar"
This is my code in which the main method is located:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JSpinner;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Font;
import javax.swing.ImageIcon;
#SuppressWarnings("serial")
public class Anzeigen extends JFrame {
private JPanel contentPane;
int spaltenAnzahl,bombenAnzahl,reihenAnzahl;
Logik logik;
public Anzeigen() {
logik = new Logik();
reihenAnzahl = 2;
spaltenAnzahl = 2;
bombenAnzahl = 1;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(800, 300, 700, 500);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);
JLabel lblReihenanzahl = new JLabel("ReihenAnzahl");
lblReihenanzahl.setFont(new Font("Tahoma", Font.BOLD, 15));
GridBagConstraints gbc_lblReihenanzahl = new GridBagConstraints();
gbc_lblReihenanzahl.insets = new Insets(0, 0, 5, 5);
gbc_lblReihenanzahl.gridx = 4;
gbc_lblReihenanzahl.gridy = 0;
contentPane.add(lblReihenanzahl, gbc_lblReihenanzahl);
JLabel lblSpaltenanzahl = new JLabel("SpaltenAnzahl");
lblSpaltenanzahl.setFont(new Font("Tahoma", Font.BOLD, 15));
GridBagConstraints gbc_lblSpaltenanzahl = new GridBagConstraints();
gbc_lblSpaltenanzahl.insets = new Insets(0, 0, 5, 5);
gbc_lblSpaltenanzahl.gridx = 7;
gbc_lblSpaltenanzahl.gridy = 0;
contentPane.add(lblSpaltenanzahl, gbc_lblSpaltenanzahl);
JLabel lblAnzahlminen = new JLabel("AnzahlMinen");
lblAnzahlminen.setFont(new Font("Tahoma", Font.BOLD, 15));
GridBagConstraints gbc_lblAnzahlminen = new GridBagConstraints();
gbc_lblAnzahlminen.insets = new Insets(0, 0, 5, 0);
gbc_lblAnzahlminen.gridx = 9;
gbc_lblAnzahlminen.gridy = 0;
contentPane.add(lblAnzahlminen, gbc_lblAnzahlminen);
JSpinner spinner_1 = new JSpinner();
spinner_1.setFont(new Font("Tahoma", Font.PLAIN, 20));
GridBagConstraints gbc_spinner_1 = new GridBagConstraints();
gbc_spinner_1.gridheight = 4;
gbc_spinner_1.fill = GridBagConstraints.BOTH;
gbc_spinner_1.insets = new Insets(0, 0, 5, 5);
gbc_spinner_1.gridx = 7;
gbc_spinner_1.gridy = 1;
contentPane.add(spinner_1, gbc_spinner_1);
JSpinner spinner_2 = new JSpinner();
spinner_2.setFont(new Font("Tahoma", Font.PLAIN, 20));
GridBagConstraints gbc_spinner_2 = new GridBagConstraints();
gbc_spinner_2.fill = GridBagConstraints.BOTH;
gbc_spinner_2.gridheight = 4;
gbc_spinner_2.insets = new Insets(0, 0, 5, 0);
gbc_spinner_2.gridx = 9;
gbc_spinner_2.gridy = 1;
contentPane.add(spinner_2, gbc_spinner_2);
JLabel label = new JLabel("");
label.setIcon(new ImageIcon(Anzeigen.class.getResource("/javax/swing/plaf/metal/icons/Warn.gif")));
GridBagConstraints gbc_label = new GridBagConstraints();
gbc_label.fill = GridBagConstraints.BOTH;
gbc_label.insets = new Insets(0, 0, 0, 5);
gbc_label.gridx = 6;
gbc_label.gridy = 9;
label.setVisible(false);
JSpinner spinner = new JSpinner();
spinner.setFont(new Font("Tahoma", Font.PLAIN, 20));
GridBagConstraints gbc_spinner = new GridBagConstraints();
gbc_spinner.fill = GridBagConstraints.BOTH;
gbc_spinner.gridheight = 4;
gbc_spinner.insets = new Insets(0, 0, 5, 5);
gbc_spinner.gridx = 4;
gbc_spinner.gridy = 1;
contentPane.add(spinner, gbc_spinner);
spinner.setValue(2);
JLabel lblNewLabel = new JLabel("");
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 15));
GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
gbc_lblNewLabel.fill = GridBagConstraints.BOTH;
gbc_lblNewLabel.insets = new Insets(0, 0, 0, 5);
gbc_lblNewLabel.gridx = 7;
gbc_lblNewLabel.gridy = 9;
lblNewLabel.setVisible(false);
contentPane.add(lblNewLabel, gbc_lblNewLabel);
spinner_1.setValue(2);
spinner_2.setValue(1);
JButton btnStart = new JButton("Start");
btnStart.setFont(new Font("Tahoma", Font.PLAIN, 20));
btnStart.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent arg0) {
lblNewLabel.setVisible(false);
spaltenAnzahl = (int) spinner_1.getValue();
reihenAnzahl = (int) spinner.getValue();
bombenAnzahl = (int) spinner_2.getValue();
System.out.println("SpaltenAnzahl: "+spaltenAnzahl);
System.out.println("ReihenAnzahl: "+reihenAnzahl);
System.out.println("BombenAnzahl: "+bombenAnzahl);
if(bombenAnzahl > spaltenAnzahl * reihenAnzahl) {
label.setVisible(true);
lblNewLabel.setVisible(true);
lblNewLabel.setText("Mehr Minen oder mehr Reihen/Spalten");
}
else if(reihenAnzahl * spaltenAnzahl >= 2500) {
lblNewLabel.setText("Zu Groß!");
lblNewLabel.setVisible(true);
}
else {
logik.inizialisieren(reihenAnzahl,spaltenAnzahl,bombenAnzahl);
setVisible(false);
}
}
});
JLabel lblDirektStarten = new JLabel("Direkt Starten:");
lblDirektStarten.setFont(new Font("Tahoma", Font.BOLD, 15));
GridBagConstraints gbc_lblDirektStarten = new GridBagConstraints();
gbc_lblDirektStarten.insets = new Insets(0, 0, 5, 5);
gbc_lblDirektStarten.gridx = 1;
gbc_lblDirektStarten.gridy = 7;
contentPane.add(lblDirektStarten, gbc_lblDirektStarten);
GridBagConstraints gbc_btnStart = new GridBagConstraints();
gbc_btnStart.insets = new Insets(0, 0, 5, 0);
gbc_btnStart.gridx = 9;
gbc_btnStart.gridy = 7;
contentPane.add(btnStart, gbc_btnStart);
JButton btnNewButton = new JButton("Einfach");
btnNewButton.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
logik.inizialisieren(9, 9, 10);
setVisible(false);
}
});
GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
gbc_btnNewButton.fill = GridBagConstraints.VERTICAL;
gbc_btnNewButton.insets = new Insets(0, 0, 5, 5);
gbc_btnNewButton.gridx = 0;
gbc_btnNewButton.gridy = 8;
contentPane.add(btnNewButton, gbc_btnNewButton);
JButton btnNewButton_1 = new JButton("Mittel");
btnNewButton_1.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
setVisible(false);
logik.inizialisieren(16, 16, 40);
}
});
GridBagConstraints gbc_btnNewButton_1 = new GridBagConstraints();
gbc_btnNewButton_1.fill = GridBagConstraints.VERTICAL;
gbc_btnNewButton_1.insets = new Insets(0, 0, 5, 5);
gbc_btnNewButton_1.gridx = 1;
gbc_btnNewButton_1.gridy = 8;
contentPane.add(btnNewButton_1, gbc_btnNewButton_1);
JButton btnNewButton_2 = new JButton("Schwer");
btnNewButton_2.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
setVisible(false);
logik.inizialisieren(16, 30, 99);
}
});
GridBagConstraints gbc_btnNewButton_2 = new GridBagConstraints();
gbc_btnNewButton_2.fill = GridBagConstraints.BOTH;
gbc_btnNewButton_2.insets = new Insets(0, 0, 5, 5);
gbc_btnNewButton_2.gridx = 2;
gbc_btnNewButton_2.gridy = 8;
contentPane.add(btnNewButton_2, gbc_btnNewButton_2);
contentPane.add(label, gbc_label);
}
public static void main(String[] args) {
Anzeigen frame = new Anzeigen();
frame.setVisible(true);
}
}
The way to run a runnable jar is with the -jar parameter, e.g. java -jar Minesweeper.jar . Otherwise it assumes you mean a class name on the CLASSPATH environment variable, which you shouldn't rely on.
Figured it out i had to change stuff in my registry
I can run .jar files through cmd, but I cannot double click them
sry for asking this another time when there is an answer already..

JLabel's setText() is not working when called outside of the class that created it

Can a setText() operation of a JLabel be called outside a JPanel ou the class that created it? Because i have to change my JLabel on a Singleton i created. When i use the setText() function, the text attribute of the JLabel is changed(i can see it from debug), but visually it is still the same old text.
But when i change the text from inside the JPanel that created the JLabel, it works... But i don't want this. I have to make setText() work outside of a JPanel. I am calling it from a Singleton which is created inside the JPanel construction.
The class that contains my JLabel is TelaPrincipal. And on it´s constructor, i change the text of the JLabel via a "setText()" and it workds just fine:
public TelaPrincipal() {
setTitle("codefont2file");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 650, 400);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{12, 92, 42, 42, 92};
gbl_contentPane.rowHeights = new int[]{19, 19, 19, 19, 19, 19, 19, 19};
gbl_contentPane.columnWeights = new double[]{0.2, 1.0, 0.1, 0.1, 0.6};
gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE, 0.0};
contentPane.setLayout(gbl_contentPane);
JLabel TituloTela = DefaultComponentFactory.getInstance().createTitle("CodeFont 2 File");
TituloTela.setFont(new Font("Tahoma", Font.BOLD, 15));
TituloTela.setHorizontalAlignment(SwingConstants.CENTER);
GridBagConstraints gbc_TituloTela = new GridBagConstraints();
gbc_TituloTela.gridwidth = 7;
gbc_TituloTela.gridheight = 1;
gbc_TituloTela.insets = new Insets(0, 0, 5, 5);
gbc_TituloTela.anchor = GridBagConstraints.NORTH;
gbc_TituloTela.fill = GridBagConstraints.HORIZONTAL;
gbc_TituloTela.gridx = 0;
gbc_TituloTela.gridy = 0;
contentPane.add(TituloTela, gbc_TituloTela);
JLabel descricao_software = DefaultComponentFactory.getInstance().createLabel("Bem vindo ao Codefont2File! Escolha seu projeto que converteremos");
GridBagConstraints gbc_descricao_software = new GridBagConstraints();
gbc_descricao_software.insets = new Insets(0, 0, 5, 0);
gbc_descricao_software.gridx = 0;
gbc_descricao_software.gridy = 1;
gbc_descricao_software.gridwidth = 8;
gbc_descricao_software.gridheight = 1;
contentPane.add(descricao_software, gbc_descricao_software);
JLabel descricao_software2 = DefaultComponentFactory.getInstance().createLabel("o c\u00F3digo fonte para um \u00FAnico PDF!");
descricao_software2.setHorizontalAlignment(SwingConstants.CENTER);
GridBagConstraints gbc_descricao_software2 = new GridBagConstraints();
gbc_descricao_software2.gridwidth = 8;
gbc_descricao_software2.gridheight = 1;
gbc_descricao_software2.insets = new Insets(0, 0, 5, 0);
gbc_descricao_software2.gridx = 0;
gbc_descricao_software2.gridy = 2;
contentPane.add(descricao_software2, gbc_descricao_software2);
JLabel lblTtulo = DefaultComponentFactory.getInstance().createLabel("T\u00EDtulo:");
GridBagConstraints gbc_lblTtulo = new GridBagConstraints();
gbc_lblTtulo.anchor = GridBagConstraints.EAST;
gbc_lblTtulo.insets = new Insets(0, 0, 5, 5);
gbc_lblTtulo.gridx = 0;
gbc_lblTtulo.gridy = 3;
contentPane.add(lblTtulo, gbc_lblTtulo);
campo_nome_projeto = new JTextField();
GridBagConstraints gbc_campo_nome_projeto = new GridBagConstraints();
gbc_campo_nome_projeto.insets = new Insets(0, 0, 5, 5);
gbc_campo_nome_projeto.fill = GridBagConstraints.HORIZONTAL;
gbc_campo_nome_projeto.gridx = 1;
gbc_campo_nome_projeto.gridy = 3;
contentPane.add(campo_nome_projeto, gbc_campo_nome_projeto);
campo_nome_projeto.setColumns(10);
JButton botaoHintNomeProjeto = new JButton("?");
botaoHintNomeProjeto.setToolTipText("Nome do seu projeto");
GridBagConstraints gbc_botaoHintNomeProjeto = new GridBagConstraints();
gbc_botaoHintNomeProjeto.insets = new Insets(0, 0, 5, 5);
gbc_botaoHintNomeProjeto.gridx = 2;
gbc_botaoHintNomeProjeto.gridy = 3;
contentPane.add(botaoHintNomeProjeto, gbc_botaoHintNomeProjeto);
JLabel label_diretorio = DefaultComponentFactory.getInstance().createLabel("Diret\u00F3rio:");
GridBagConstraints gbc_label_diretorio = new GridBagConstraints();
gbc_label_diretorio.anchor = GridBagConstraints.EAST;
gbc_label_diretorio.insets = new Insets(0, 0, 5, 5);
gbc_label_diretorio.gridx = 0;
gbc_label_diretorio.gridy = 4;
gbc_label_diretorio.gridwidth = 1;
gbc_label_diretorio.gridheight = 1;
contentPane.add(label_diretorio, gbc_label_diretorio);
campo_preencher_diretorio = new JTextField();
GridBagConstraints gbc_campo_preencher_diretorio = new GridBagConstraints();
gbc_campo_preencher_diretorio.insets = new Insets(0, 0, 5, 5);
gbc_campo_preencher_diretorio.fill = GridBagConstraints.HORIZONTAL;
gbc_campo_preencher_diretorio.gridx = 1;
gbc_campo_preencher_diretorio.gridy = 4;
gbc_campo_preencher_diretorio.gridheight = 1;
gbc_descricao_software.gridwidth = 1;
contentPane.add(campo_preencher_diretorio, gbc_campo_preencher_diretorio);
campo_preencher_diretorio.setColumns(20);
JButton botao_selecionar_pasta_projeto = new JButton("...");
botao_selecionar_pasta_projeto.setAction(acaoSelecionarPastaProjeto);
GridBagConstraints gbc_botao_selecionar_pasta_projeto = new GridBagConstraints();
gbc_botao_selecionar_pasta_projeto.insets = new Insets(0, 0, 5, 5);
gbc_botao_selecionar_pasta_projeto.gridx = 2;
gbc_botao_selecionar_pasta_projeto.gridy = 4;
gbc_botao_selecionar_pasta_projeto.gridheight = 1;
contentPane.add(botao_selecionar_pasta_projeto, gbc_botao_selecionar_pasta_projeto);
JButton botao_explicacao_selecione_diretorio = new JButton("?");
botao_explicacao_selecione_diretorio.setHorizontalAlignment(SwingConstants.LEFT);
botao_explicacao_selecione_diretorio.setToolTipText("escolha a pasta raiz do seu projeto");
GridBagConstraints gbc_botao_explicacao_selecione_diretorio = new GridBagConstraints();
gbc_botao_explicacao_selecione_diretorio.insets = new Insets(0, 0, 5, 5);
gbc_botao_explicacao_selecione_diretorio.gridx = 3;
gbc_botao_explicacao_selecione_diretorio.gridy = 4;
gbc_botao_explicacao_selecione_diretorio.gridheight = 1;
contentPane.add(botao_explicacao_selecione_diretorio, gbc_botao_explicacao_selecione_diretorio);
//PARTE REFERENTE A ADICIONAR EXTENSÃO(ANDREWS)
JPanel painel_adicionar_extensao = new JPanel();
TitledBorder tituloPainelExtensoes;
tituloPainelExtensoes = BorderFactory.createTitledBorder("Extensões");
painel_adicionar_extensao.setBorder(tituloPainelExtensoes);
GridBagConstraints gbc_painel_adicionar_extensao = new GridBagConstraints();
gbc_painel_adicionar_extensao.insets = new Insets(0, 0, 5, 0);
gbc_painel_adicionar_extensao.anchor = GridBagConstraints.NORTH;
gbc_painel_adicionar_extensao.gridheight = 7;
gbc_painel_adicionar_extensao.gridx = 4;
gbc_painel_adicionar_extensao.gridy = 3;
gbc_painel_adicionar_extensao.gridwidth = 4;
contentPane.add(painel_adicionar_extensao, gbc_painel_adicionar_extensao);
GridBagLayout gbl_painel_adicionar_extensao = new GridBagLayout();
gbl_painel_adicionar_extensao.columnWidths = new int[]{20, 20, 20, 20};
gbl_painel_adicionar_extensao.rowHeights = new int[]{20, 20, 20, 20};
gbl_painel_adicionar_extensao.columnWeights = new double[]{0.4, 0.4, 0.4, 0.4};
gbl_painel_adicionar_extensao.rowWeights = new double[]{0.4, 0.4, 0.4, 0.4};
painel_adicionar_extensao.setLayout(gbl_painel_adicionar_extensao);
textFieldAdicionarExtensoes = new JTextField();
GridBagConstraints gbc_textFieldAdicionarExtensoes = new GridBagConstraints();
gbc_textFieldAdicionarExtensoes.gridwidth = 3;
gbc_textFieldAdicionarExtensoes.insets = new Insets(0, 0, 0, 5);
gbc_textFieldAdicionarExtensoes.fill = GridBagConstraints.HORIZONTAL;
gbc_textFieldAdicionarExtensoes.gridx = 0;
gbc_textFieldAdicionarExtensoes.gridy = 3;
painel_adicionar_extensao.add(textFieldAdicionarExtensoes, gbc_textFieldAdicionarExtensoes);
textFieldAdicionarExtensoes.setColumns(10);
buttonRemoverExtensoes = new JButton("-");
GridBagConstraints gbc_buttonRemoverExtensoes = new GridBagConstraints();
gbc_buttonRemoverExtensoes.insets = new Insets(0, 0, 5, 0);
gbc_buttonRemoverExtensoes.gridx = 3;
gbc_buttonRemoverExtensoes.gridy = 0;
painel_adicionar_extensao.add(buttonRemoverExtensoes, gbc_buttonRemoverExtensoes);
buttonRemoverExtensoes.addActionListener(this);
this.listModel = new DefaultListModel<String>();
this.listaExtensoes = new JList<String>(listModel);
GridBagConstraints gbc_listaExtensoes = new GridBagConstraints();
gbc_listaExtensoes.gridheight = 3;
gbc_listaExtensoes.gridwidth = 4;
gbc_listaExtensoes.insets = new Insets(0, 0, 5, 25);
gbc_listaExtensoes.gridx = 0;
gbc_listaExtensoes.gridy = 0;
listaExtensoes.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
listaExtensoes.setLayoutOrientation(JList.VERTICAL);
listaExtensoes.setVisibleRowCount(-1);
JScrollPane scrollPane = new JScrollPane(listaExtensoes);
scrollPane.setPreferredSize(new Dimension(80, 120));
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setHorizontalScrollBarPolicy(
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
ListSelectionModel listSelectionModel = listaExtensoes.getSelectionModel();
listSelectionModel.addListSelectionListener(
new ListenerListaExtensoes(buttonRemoverExtensoes,listaExtensoes));
painel_adicionar_extensao.add(scrollPane, gbc_listaExtensoes);
buttonAdicionarExtensoes = new JButton("+");
GridBagConstraints gbc_buttonAdicionarExtensoes = new GridBagConstraints();
gbc_buttonAdicionarExtensoes.gridx = 3;
gbc_buttonAdicionarExtensoes.gridy = 3;
painel_adicionar_extensao.add(buttonAdicionarExtensoes, gbc_buttonAdicionarExtensoes);
buttonAdicionarExtensoes.addActionListener(this);
extensoes= new LinkedList<String>();
JLabel label_autor = DefaultComponentFactory.getInstance().createLabel("Autor:");
GridBagConstraints gbc_label_autor = new GridBagConstraints();
gbc_label_autor.anchor = GridBagConstraints.EAST;
gbc_label_autor.insets = new Insets(0, 0, 5, 5);
gbc_label_autor.gridx = 0;
gbc_label_autor.gridy = 5;
gbc_label_autor.gridheight = 1;
contentPane.add(label_autor, gbc_label_autor);
campo_preencher_autor = new JTextField();
GridBagConstraints gbc_campo_preencher_autor = new GridBagConstraints();
gbc_campo_preencher_autor.insets = new Insets(0, 0, 5, 5);
gbc_campo_preencher_autor.fill = GridBagConstraints.HORIZONTAL;
gbc_campo_preencher_autor.gridx = 1;
gbc_campo_preencher_autor.gridy = 5;
gbc_campo_preencher_autor.gridheight = 1;
contentPane.add(campo_preencher_autor, gbc_campo_preencher_autor);
campo_preencher_autor.setColumns(20);
JLabel label_versao = DefaultComponentFactory.getInstance().createLabel("Vers\u00E3o:");
GridBagConstraints gbc_label_versao = new GridBagConstraints();
gbc_label_versao.anchor = GridBagConstraints.EAST;
gbc_label_versao.insets = new Insets(0, 0, 5, 5);
gbc_label_versao.gridx = 0;
gbc_label_versao.gridy = 6;
gbc_label_versao.gridheight = 1;
contentPane.add(label_versao, gbc_label_versao);
campo_preencher_versao = new JTextField();
GridBagConstraints gbc_campo_preencher_versao = new GridBagConstraints();
gbc_campo_preencher_versao.insets = new Insets(0, 0, 5, 5);
gbc_campo_preencher_versao.fill = GridBagConstraints.HORIZONTAL;
gbc_campo_preencher_versao.gridx = 1;
gbc_campo_preencher_versao.gridy = 6;
contentPane.add(campo_preencher_versao, gbc_campo_preencher_versao);
gbc_campo_preencher_versao.gridheight = 1;
campo_preencher_versao.setColumns(10);
JLabel label_output = DefaultComponentFactory.getInstance().createLabel("Output:");
GridBagConstraints gbc_label_output = new GridBagConstraints();
gbc_label_output.anchor = GridBagConstraints.EAST;
gbc_label_output.insets = new Insets(0, 0, 5, 5);
gbc_label_output.gridx = 0;
gbc_label_output.gridy = 7;
gbc_label_output.gridheight = 1;
contentPane.add(label_output, gbc_label_output);
campo_preencher_output = new JTextField();
GridBagConstraints gbc_campo_preencher_output = new GridBagConstraints();
gbc_campo_preencher_output.insets = new Insets(0, 0, 5, 5);
gbc_campo_preencher_output.fill = GridBagConstraints.HORIZONTAL;
gbc_campo_preencher_output.gridx = 1;
gbc_campo_preencher_output.gridy = 7;
gbc_campo_preencher_output.gridheight = 1;
contentPane.add(campo_preencher_output, gbc_campo_preencher_output);
campo_preencher_output.setColumns(10);
JButton botao_especificar_arquivo_output = new JButton("...");
botao_especificar_arquivo_output.setAction(acaoEspecificarOutput);
GridBagConstraints gbc_botao_especificar_arquivo_output = new GridBagConstraints();
gbc_botao_especificar_arquivo_output.insets = new Insets(0, 0, 5, 5);
gbc_botao_especificar_arquivo_output.gridx = 2;
gbc_botao_especificar_arquivo_output.gridy = 7;
gbc_botao_especificar_arquivo_output.gridheight = 1;
contentPane.add(botao_especificar_arquivo_output, gbc_botao_especificar_arquivo_output);
JButton botaoEspecificarPastas = new JButton("Avançado...");
botaoEspecificarPastas.setAction(acaoBotaoEspecificarPastasArquivosProjeto);
GridBagConstraints gbc_botaoEspecificarPastas = new GridBagConstraints();
gbc_botaoEspecificarPastas.gridwidth = 2;
gbc_botaoEspecificarPastas.insets = new Insets(5, 0, 5, 5);
gbc_botaoEspecificarPastas.gridx = 1;
gbc_botaoEspecificarPastas.gridy = 8;
contentPane.add(botaoEspecificarPastas, gbc_botaoEspecificarPastas);
JButton botao_explicacao_especificar_pastas = new JButton("?");
botao_explicacao_especificar_pastas.setToolTipText("especificar que pastas/arquivos de seu projeto você quer no PDF");
GridBagConstraints gbc_botao_explicacao_especificar_pastas = new GridBagConstraints();
gbc_botao_explicacao_especificar_pastas.insets = new Insets(5, 0, 5, 5);
gbc_botao_explicacao_especificar_pastas.gridx = 2;
gbc_botao_explicacao_especificar_pastas.gridy = 8;
contentPane.add(botao_explicacao_especificar_pastas, gbc_botao_explicacao_especificar_pastas);
JButton botaoGerarPDF = new JButton("Gerar PDF");
botaoGerarPDF.setAction(acaoGerarPdf);
GridBagConstraints gbc_botaoGerarPDF = new GridBagConstraints();
gbc_botaoGerarPDF.gridheight = 3;
gbc_botaoGerarPDF.gridwidth = 2;
//gbc_botaoGerarPDF.insets = new Insets(0, 0, 0, 5);
gbc_botaoGerarPDF.gridx = 1;
gbc_botaoGerarPDF.gridy = 9;
contentPane.add(botaoGerarPDF, gbc_botaoGerarPDF);
JProgressBar barraDeProgresso = new JProgressBar();
GridBagConstraints gbc_JProgressBarUsuarioClicouNoBotaoAvancado = new GridBagConstraints();
gbc_JProgressBarUsuarioClicouNoBotaoAvancado.gridheight = 1;
gbc_JProgressBarUsuarioClicouNoBotaoAvancado.gridwidth = 2;
gbc_JProgressBarUsuarioClicouNoBotaoAvancado.gridx = 1;
gbc_JProgressBarUsuarioClicouNoBotaoAvancado.gridy = 10;
contentPane.add(barraDeProgresso, gbc_JProgressBarUsuarioClicouNoBotaoAvancado);
SingletonBarraDeProgresso.getInstance().setBarraDeProgresso(barraDeProgresso);
textoBarraDeProgresso = new JLabel("oi meu nome é andrews e eu sou uma pessoa legal como vocês são e eu falo muito não é vamos falar mais um pouco");
GridBagConstraints gbc_textoBarraDeProgresso = new GridBagConstraints();
gbc_textoBarraDeProgresso.gridheight = 1;
gbc_textoBarraDeProgresso.gridwidth = 2;
gbc_textoBarraDeProgresso.gridx = 1;
gbc_textoBarraDeProgresso.gridy = 11;
contentPane.add(textoBarraDeProgresso, gbc_textoBarraDeProgresso);
textoBarraDeProgresso.setText("oi meu nome é andrews");
textoBarraDeProgresso.setVisible(true);
SingletonBarraDeProgresso.getInstance().setTextoBarraDeProgresso(textoBarraDeProgresso);
SingletonBarraDeProgresso.getInstance().setTelaPrincipal(this);
extensoes= new LinkedList<String>();
//vamos verificar se n jah existem extensoes no arquivo .txt que podemos usar
this.verificarSeJaExistemExtensoesNoTxtParaJaPovoarAGuiComEstasExtensoes();
}
But when i try to change it via my Singleton on this method:
public void inicializarBarraDeProgresso(int valorMaximo, String textoBarra)
{
if(barraDeProgresso != null)
{
barraDeProgresso.setVisible(true);
barraDeProgresso.setStringPainted(true);
barraDeProgresso.setValue(0);
this.barraDeProgresso.setMaximum(valorMaximo);
this.barraDeProgresso.setMinimum(0);
this.textoBarraDeProgresso.setVisible(true);
this.textoBarraDeProgresso.setText(textoBarra);
}
}
Which is called inside many classes(TelaPrincipal is one of them, but there are a lot of other classes), the text stays the same: "oi meu nome é andrews"
I have also tried myJLabel.update(myJLabel.getGraphics()); after the setText(), but it bugs by text(it´s like it creates a loop which puts a lot of text over each other).
This post is the real lifesaver:
Why isn't setText updating the JLabel?
Basically, all you have to do is:
label.paintImmediately(label.getVisibleRect());
Right after the settext().
Java swing is confusing and annoying. I really do like working with android better ^^

How to use multiple layouts, without having blank space inbetween panels?

I'm trying to use multiple layouts (Border and Gridbag) to make a standalone visual application that shows the status of recovery rooms. I want my company logo to be in the top left corner. Directly to the right of the logo I want to have information shown in text format such as available beds, how many total occupants, ect...
Below that I am using the Grid bag layout to place bed icons as well as room numbers. There will be a lot more icons later on, I've only got two added at the moment just to show where they will start on the page.
I've provided an image of what it currently looks like with my source code.
TLDR: I need to beds shown in the image to be in the upper left part of the screen about half an inch from the bottom of the sample logo.
I tried to add an image, but I don't have enough reputation. here is a link to it if you need to see.
http://imgur.com/DViRSuJ
Source code for image above:
public static void showUI() {
int bedCount = getBedCount();
bedCount = bedCount - 5;
Toolkit tk = Toolkit.getDefaultToolkit();
int xSize = ((int) tk.getScreenSize().getWidth());
int ySize = ((int) tk.getScreenSize().getHeight());
ImageIcon maleBed = new ImageIcon("Images/Male.png");
ImageIcon femaleBed = new ImageIcon("Images/Female.png");
ImageIcon emptyBed = new ImageIcon("Images/empty.png");
ImageIcon logo = new ImageIcon("Images/logo-sample.png");
ImageIcon mlogo = new ImageIcon("Images/Medicalistics_Logo.png");
JFrame window = new JFrame("Ft Lauderdale");
window.setVisible(true);
window.setSize(xSize, ySize);
JPanel mainPanel = new JPanel();
mainPanel.setBackground(Color.white);
mainPanel.setBounds(900, 900, 900, 900);
mainPanel.setLayout(new BorderLayout());
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.add(mainPanel);
JPanel logoPane = new JPanel();
logoPane.setLayout(new BorderLayout());
logoPane.setBackground(Color.white);
mainPanel.add(logoPane, BorderLayout.NORTH);
JPanel bedsPane = new JPanel();
bedsPane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JLabel logoLabel = new JLabel(logo);
logoLabel.setHorizontalAlignment(JLabel.LEFT);
logoPane.add(logoLabel, BorderLayout.NORTH);
mainPanel.add(bedsPane, BorderLayout.CENTER);
JLabel bedLabel = new JLabel(emptyBed);
c.gridheight = 5;
c.gridwidth = 5;
c.gridx = 50;
c.gridy = 0;
bedsPane.add(bedLabel, c);
JLabel bedLabel2 = new JLabel(femaleBed);
c.gridx = 0;
c.gridy = 0;
bedsPane.add(bedLabel2, c);
}
Well for the blank around panels you probably need to set the insets
eg: c.insets = new Insets(0, 0, 0, 0);
I am not sure how you want to handle 2 layout but you can do the effect you want with simply GridBagLayout.
You can see my test code here :
package Main;
import java.awt.*;
import javax.swing.*;
public class dummyGUI {
private static JPanel infopane, bedsPane, logopane, mainPanel;
private static JLabel lmalebed, lfemalebed, logoLabel;
public dummyGUI() {
showUI();
}
public static void showUI() {
int bedCount = 7; // getBedCount();
bedCount = bedCount - 5;
Toolkit tk = Toolkit.getDefaultToolkit();
int xSize = ((int) tk.getScreenSize().getWidth());
int ySize = ((int) tk.getScreenSize().getHeight());
ImageIcon maleBed = new ImageIcon("Images/Male.png");
ImageIcon femaleBed = new ImageIcon("Images/Female.png");
ImageIcon emptyBed = new ImageIcon("Images/empty.png");
ImageIcon logo = new ImageIcon("Images/logo-sample.png");
ImageIcon mlogo = new ImageIcon("Images/Medicalistics_Logo.png");
JFrame window = new JFrame("Ft Lauderdale");
window.setVisible(true);
window.setSize(xSize, ySize);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[] { 897, 0 };
gridBagLayout.rowHeights = new int[] { 504, 0, 0 };
gridBagLayout.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
gridBagLayout.rowWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
window.getContentPane().setLayout(gridBagLayout);
mainPanel = new JPanel();
mainPanel.setBackground(Color.white);
GridBagConstraints gbc_mainPanel = new GridBagConstraints();
gbc_mainPanel.insets = new Insets(0, 0, 5, 0);
gbc_mainPanel.fill = GridBagConstraints.BOTH;
gbc_mainPanel.gridx = 0;
gbc_mainPanel.gridy = 0;
window.getContentPane().add(mainPanel, gbc_mainPanel);
GridBagLayout gbl_mainPanel = new GridBagLayout();
gbl_mainPanel.columnWidths = new int[] { 286, 518, 0 };
gbl_mainPanel.rowHeights = new int[] { 101, 367, 0 };
gbl_mainPanel.columnWeights = new double[] { 1.0, 2.0, Double.MIN_VALUE };
gbl_mainPanel.rowWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
mainPanel.setLayout(gbl_mainPanel);
logopane = new JPanel();
logopane.setBackground(Color.WHITE);
GridBagConstraints gbc_logopane = new GridBagConstraints();
gbc_logopane.insets = new Insets(0, 0, 0, 0);
gbc_logopane.fill = GridBagConstraints.BOTH;
gbc_logopane.gridx = 0;
gbc_logopane.gridy = 0;
mainPanel.add(logopane, gbc_logopane);
logopane.setLayout(new BorderLayout());
logoLabel = new JLabel(logo);
logopane.add(logoLabel, BorderLayout.NORTH);
logoLabel.setHorizontalAlignment(JLabel.LEFT);
infopane = new JPanel();
infopane.setBackground(Color.WHITE);
GridBagConstraints gbc_infopane = new GridBagConstraints();
gbc_infopane.insets = new Insets(0, 0, 0, 0);
gbc_infopane.fill = GridBagConstraints.BOTH;
gbc_infopane.gridx = 1;
gbc_infopane.gridy = 0;
mainPanel.add(infopane, gbc_infopane);
infopane.setLayout(new BorderLayout());
bedsPane = new JPanel();
GridBagConstraints gbc_bedsPane = new GridBagConstraints();
gbc_bedsPane.gridwidth = 2;
gbc_bedsPane.fill = GridBagConstraints.BOTH;
gbc_bedsPane.gridx = 0;
gbc_bedsPane.gridy = 1;
mainPanel.add(bedsPane, gbc_bedsPane);
GridBagLayout gbl_bedsPane = new GridBagLayout();
gbl_bedsPane.columnWidths = new int[] { 1, 0, 0, 0, 0 };
gbl_bedsPane.rowHeights = new int[] { 1, 0, 0, 0 };
gbl_bedsPane.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
gbl_bedsPane.rowWeights = new double[] { 0.0, 0.0, 0.0, Double.MIN_VALUE };
bedsPane.setLayout(gbl_bedsPane);
lmalebed = new JLabel(maleBed);
GridBagConstraints gbc_lmalebed = new GridBagConstraints();
gbc_lmalebed.insets = new Insets(0, 0, 0, 0);
gbc_lmalebed.gridx = 1;
gbc_lmalebed.gridy = 1;
bedsPane.add(lmalebed, gbc_lmalebed);
lfemalebed = new JLabel(femaleBed);
GridBagConstraints gbc_lfemalebed = new GridBagConstraints();
gbc_lfemalebed.gridx = 3;
gbc_lfemalebed.gridy = 1;
bedsPane.add(lfemalebed, gbc_lfemalebed);
}
public static void main(String[] args) {
new dummyGUI();
}
}
The part here
gbl_bedsPane.columnWidths = new int[] { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
gbl_bedsPane.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
gbl_bedsPane.rowHeights = new int[] { 1, 0, 0, 0, 0 };
gbl_bedsPane.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
is where you can tell the number of row and column. I did not set enough column that is why you see it appear on the right when you add more.
I also did a function like i said in the comment :
private static JLabel add_bed(String sex, int x, int y){
JLabel bed = null;
if (sex == "female"){
bed = new JLabel(femaleBed);
GridBagConstraints gbc_lfemalebed = new GridBagConstraints();
gbc_lfemalebed.insets = new Insets(0, 0, 0, 0);
gbc_lfemalebed.gridx = x;
gbc_lfemalebed.gridy = y;
bedsPane.add(lfemalebed, gbc_lfemalebed);
}else if (sex == "male"){
lmalebed = new JLabel(maleBed);
GridBagConstraints gbc_lmalebed = new GridBagConstraints();
gbc_lmalebed.insets = new Insets(0, 0, 0, 0);
gbc_lmalebed.gridx = x;
gbc_lmalebed.gridy = y;
bedsPane.add(lmalebed, gbc_lmalebed);
}
return bed;
}
Then i can just do
add_bed("female", 5 , 1);

Repeat WindowBuilder JLabel using Grid Layout

I want to repeat a JLabel "n" no. of times where "n" is given by user.
This implementation, using WindowBuilder's Grid layout, doesn't work, prints nothing at all.
What am I doing wrong?
JLabel lblNewLabel_1[]=new JLabel[20];
GridBagConstraints gbc_lblNewLabel_1[] = new GridBagConstraints[20];
for(int i=0;i<n;i++)
{
lblNewLabel_1[i] = new JLabel("Table #");
gbc_lblNewLabel_1[i].insets = new Insets(0, 0, 5, 5);
gbc_lblNewLabel_1[i].gridx = 0;
gbc_lblNewLabel_1[i].gridy = 4+i;
frame.getContentPane().add(lblNewLabel_1[i], gbc_lblNewLabel_1[i]);
}
Full Method, my content pane is grid layout.
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 507, 432);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{57, 377, 0};
gridBagLayout.rowHeights = new int[]{60, 37, 35, 20, 0, 0, 0, 0};
gridBagLayout.columnWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
frame.getContentPane().setLayout(gridBagLayout);
textField = new JTextField();
textField.setToolTipText("Max: 20");
textField.setText("0");
textField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(arg0.getSource()==textField)
n = Integer.parseInt(arg0.getActionCommand());
}
});
JLabel lblNewLabel = new JLabel("Enter The Number of Tables");
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 30));
GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
gbc_lblNewLabel.insets = new Insets(0, 0, 5, 0);
gbc_lblNewLabel.gridx = 1;
gbc_lblNewLabel.gridy = 1;
frame.getContentPane().add(lblNewLabel, gbc_lblNewLabel);
GridBagConstraints gbc_textField = new GridBagConstraints();
gbc_textField.insets = new Insets(0, 0, 5, 0);
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.gridx = 1;
gbc_textField.gridy = 3;
frame.getContentPane().add(textField, gbc_textField);
textField.setColumns(10);
GridBagConstraints gbc_lblNewLabel_1[] = new GridBagConstraints[20];
for(int i=0;i<n;i++){
lblNewLabel_1[i] = new JLabel("Table #");
gbc_lblNewLabel_1[i].insets = new Insets(0, 0, 5, 5);
gbc_lblNewLabel_1[i].gridx = 0;
gbc_lblNewLabel_1[i].gridy = 4+i;
frame.getContentPane().add(lblNewLabel_1[i], gbc_lblNewLabel_1[i]);
}
}
The default layout for a JFrame is a BorderLayout.
If you want to use a GridBagLayout then you need to set the layout of the content pane to use a GridBagLayout.
I suggest you read the section from the Swing turial on How to Use GridBagLayout for more explanation and working examples.
If you want to use a GridLayout, then you also need to set the layout manager of the content pane to a GridLayout and you don't use any constraints. Again, read the tutorial for examples.

Dynamic scaling in nested layout

I want to have arbitrary JPanels containing a JLabel sacling dynamically in a container (JPanel) with a fixed size. So something like this:
__________________________________
| _____________ _____________ |
| | Test Label| | Test Label| |
| |___________| |___________| |
|________________________________|
scaling to this
__________________________________
| ________ ________ ________ |
| |Tes...| |Tes...| |Tes...| |
| |______| |______| |______| |
|________________________________|
I tried to use a BoxLayout (X-Orientation), setting a maximum size for each contained JPanel:
OuterContainer.Width / NumberOfContainedJPanels
without success. The containers around the labels never fall below a specific width.
Further more using a GridBagLayout does not work good, because i want to dynamically add more containers so it wouldn't be a good way to generate the GridBagLayout constraints on each adding.
Is there a good solution for this problem? The following is a SSCCE for my problem:
import javax.swing.JFrame;
import java.awt.GridBagLayout;
import javax.swing.JPanel;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import javax.swing.BoxLayout;
import java.awt.Dimension;
public class DynScalingExample extends JFrame {
public DynScalingExample() {
getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS));
//---Panel 1
JPanel panel = new JPanel();
panel.setMaximumSize(new Dimension(20, 32767));
getContentPane().add(panel);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[]{0, 0};
gbl_panel.rowHeights = new int[]{0, 0};
gbl_panel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gbl_panel.rowWeights = new double[]{1.0, Double.MIN_VALUE};
panel.setLayout(gbl_panel);
JLabel lblNewLabel = new JLabel("Test label");
GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
gbc_lblNewLabel.anchor = GridBagConstraints.WEST;
gbc_lblNewLabel.gridx = 0;
gbc_lblNewLabel.gridy = 0;
panel.add(lblNewLabel, gbc_lblNewLabel);
//---Panel 2
JPanel panel_1 = new JPanel();
getContentPane().add(panel_1);
GridBagLayout gbl_panel_1 = new GridBagLayout();
gbl_panel_1.columnWidths = new int[]{0, 0};
gbl_panel_1.rowHeights = new int[]{0, 0};
gbl_panel_1.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gbl_panel_1.rowWeights = new double[]{1.0, Double.MIN_VALUE};
panel_1.setLayout(gbl_panel_1);
JLabel label = new JLabel("Test label");
GridBagConstraints gbc_label = new GridBagConstraints();
gbc_label.anchor = GridBagConstraints.WEST;
gbc_label.gridx = 0;
gbc_label.gridy = 0;
panel_1.add(label, gbc_label);
//---Panel 3
JPanel panel_2 = new JPanel();
getContentPane().add(panel_2);
GridBagLayout gbl_panel_2 = new GridBagLayout();
gbl_panel_2.columnWidths = new int[]{0, 0};
gbl_panel_2.rowHeights = new int[]{0, 0};
gbl_panel_2.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gbl_panel_2.rowWeights = new double[]{1.0, Double.MIN_VALUE};
panel_2.setLayout(gbl_panel_2);
JLabel label_1 = new JLabel("Test label");
GridBagConstraints gbc_label_1 = new GridBagConstraints();
gbc_label_1.anchor = GridBagConstraints.WEST;
gbc_label_1.gridx = 0;
gbc_label_1.gridy = 0;
panel_2.add(label_1, gbc_label_1);
}
}
As #Andrew Thompson suggested, a GridLayout works perfectly for this purpose. As example, see the following:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class DynScalingExample extends JFrame {
public DynScalingExample() {
getContentPane().setLayout(new GridLayout(1, 0, 0, 0));
//---Panel 1
JPanel panel = new JPanel();
getContentPane().add(panel);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[]{0, 0};
gbl_panel.rowHeights = new int[]{0, 0};
gbl_panel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gbl_panel.rowWeights = new double[]{1.0, Double.MIN_VALUE};
panel.setLayout(gbl_panel);
JLabel lblNewLabel = new JLabel("Test label");
GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
gbc_lblNewLabel.anchor = GridBagConstraints.WEST;
gbc_lblNewLabel.gridx = 0;
gbc_lblNewLabel.gridy = 0;
panel.add(lblNewLabel, gbc_lblNewLabel);
//---Panel 2
JPanel panel_1 = new JPanel();
getContentPane().add(panel_1);
GridBagLayout gbl_panel_1 = new GridBagLayout();
gbl_panel_1.columnWidths = new int[]{0, 0};
gbl_panel_1.rowHeights = new int[]{0, 0};
gbl_panel_1.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gbl_panel_1.rowWeights = new double[]{1.0, Double.MIN_VALUE};
panel_1.setLayout(gbl_panel_1);
JLabel label = new JLabel("Test label");
GridBagConstraints gbc_label = new GridBagConstraints();
gbc_label.anchor = GridBagConstraints.WEST;
gbc_label.gridx = 0;
gbc_label.gridy = 0;
panel_1.add(label, gbc_label);
//---Panel 3
JPanel panel_2 = new JPanel();
getContentPane().add(panel_2);
GridBagLayout gbl_panel_2 = new GridBagLayout();
gbl_panel_2.columnWidths = new int[]{0, 0};
gbl_panel_2.rowHeights = new int[]{0, 0};
gbl_panel_2.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gbl_panel_2.rowWeights = new double[]{1.0, Double.MIN_VALUE};
panel_2.setLayout(gbl_panel_2);
JLabel label_1 = new JLabel("Test label");
GridBagConstraints gbc_label_1 = new GridBagConstraints();
gbc_label_1.anchor = GridBagConstraints.WEST;
gbc_label_1.gridx = 0;
gbc_label_1.gridy = 0;
panel_2.add(label_1, gbc_label_1);
}
}

Categories