How can I set a size for my label? - java

In this code I try to set a size for my label in line 70:
label1.setMinimumSize(new Dimension(150, 100));
But I don't know why the compiler ignores that code so help me to re size my labels in "box layout" and other layouts
This is my code!
package gui1.pkg2;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
/**
* #author Danial
*/
public class Frame extends JFrame {
public Frame() {
setPreferredSize(new Dimension(600, 500));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBackground(Color.red); //---------------------------------------------------------------------------------->> karnemikone.
//-------Strat--- ADD sub Classes-------------------
//-------DownButtons Class----------
DownButtons dB = new DownButtons();
add(dB, BorderLayout.SOUTH);
//-------BlackAndWite Class----------
BlackAndWite bW = new BlackAndWite();
add(bW, BorderLayout.WEST);
//-------RadioButtons Class----------
radioButtons rB = new radioButtons();
add(rB, BorderLayout.EAST);
//-------End----ADD subClasses----------------------
setVisible(true);
pack();
//-----------------------------
}
//-------- a class for buttons.--------------
class DownButtons extends JPanel {
public DownButtons() {
setLayout(new FlowLayout());
JButton save = new JButton("Save");
save.setBackground(new java.awt.Color(150, 231, 19));
JButton exit = new JButton("Exit");
exit.setBackground(new java.awt.Color(150, 231, 19));
JButton cancel = new JButton("cancel");
cancel.setBackground(new java.awt.Color(150, 231, 19));
add(save);
add(exit);
add(cancel);
pack();
}
}
class BlackAndWite extends JPanel {
public BlackAndWite() {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
JLabel label1 = new JLabel(" ");
label1.setOpaque(true);
label1.setMinimumSize(new Dimension(150, 100));
label1.setBackground(Color.red);
add(label1);
}
}
class radioButtons extends JPanel {
public radioButtons() {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
class Inner extends JPanel {
public Inner(String labl, String r1, String r2) {
setLayout(new FlowLayout());
JLabel label = new JLabel(labl);
label.setOpaque(true);
label.setBackground(Color.YELLOW);
add(label);
JRadioButton radio1 = new JRadioButton(r1 + "");
JRadioButton radio2 = new JRadioButton(r2);
add(radio1);
add(radio2);
}
}
Inner in = new Inner(gender, male , Female);
///add(in);
}
}
}

Related

Having a JTextField popup at a certain location after clicking a JButton

I have my three JButtons located where I want them (at the top center of the frame), and when the user clicks one, a JTextField pops up in the BoxLayout like wanted.
The problem is, when the JTextField shows up, it is to the left of the buttons, and it moves them.
I tried setting the alignment of the JTextField and using various glues, but the JTextField doesn't move.
If I want to have the JTextField pop up below my JButtons and in the center of the screen, what should I use?
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
public class Library extends JFrame implements ActionListener {
private JFrame jf1;
private JPanel jp1;
private JTextField jtf1;
private JButton jb1;
private JButton jb2;
private JButton jb3;
public Library() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception q) {
q.printStackTrace();
}
JFrame.setDefaultLookAndFeelDecorated(false);
jf1 = new JFrame("Library");
jf1.setVisible(true);
jf1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf1.setSize(1080, 900);
jf1.setResizable(true);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
jf1.setLocation(dim.width/2-jf1.getSize().width/2, dim.height/2-jf1.getSize().height/2);
jp1 = (JPanel) jf1.getContentPane();
jp1.setLayout(new BoxLayout(jp1, BoxLayout.LINE_AXIS));
jb1 = new JButton("Genre");
jb1.addActionListener(this);
jb1.setMinimumSize(new Dimension(140, 60));
jb1.setPreferredSize(new Dimension(150, 60));
jb1.setMaximumSize(new Dimension(150, 60));
jb1.setAlignmentY(-70.0f);
jb2 = new JButton("Author");
jb2.addActionListener(this);
jb2.setMinimumSize(new Dimension(140, 60));
jb2.setPreferredSize(new Dimension(150, 60));
jb2.setMaximumSize(new Dimension(150, 60));
jb2.setAlignmentY(-70.0f);
jb3 = new JButton("Title");
jb3.addActionListener(this);
jb3.setMinimumSize(new Dimension(140, 60));
jb3.setPreferredSize(new Dimension(150, 60));
jb3.setMaximumSize(new Dimension(150, 60));
jb3.setAlignmentY(-70.0f);
jp1.add(Box.createHorizontalGlue());
jp1.add(jb1);
jp1.add(jb2);
jp1.add(jb3);
jp1.add(Box.createHorizontalGlue());
jf1.validate();
}
#Override
public void actionPerformed(ActionEvent e) {
Object code = e.getSource();
if (code == jb1) {
jtf1 = new JTextField("Enter Text");
jtf1.setPreferredSize(new Dimension(200,20));
jtf1.setMaximumSize(new Dimension(200,20));
jtf1.setMinimumSize(new Dimension(10,10));
jp1.add(jtf1);
jp1.validate();
}
else if (code == jb2) {
}
else if (code == jb3) {
}
}
public static void main(String[] args) {
Library shoe = new Library();
}
}
Suggestion: do not add/remove UI elements dynamically. Just add all of those things initially, and simply call setVisible(false) on your text field then.
(instead of adding/removing fields using your action listener)
A good and easy solution may be to use another JPanel to hold your dynamically created text fields. This may be what you wanted:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
public class Library extends JFrame implements ActionListener {
private JFrame jf1;
private JPanel jp1;
private JPanel jp2;
private JTextField jtf1;
private JButton jb1;
private JButton jb2;
private JButton jb3;
public Library() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception q) {
q.printStackTrace();
}
JFrame.setDefaultLookAndFeelDecorated(false);
jf1 = new JFrame("Library");
jf1.setVisible(true);
jf1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf1.setSize(1080, 900);
jf1.setResizable(true);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
jf1.setLocation(dim.width/2-jf1.getSize().width/2, dim.height/2-jf1.getSize().height/2);
jp1 = new JPanel();
jp1.setLayout(new BoxLayout(jp1, BoxLayout.LINE_AXIS));
jp2 = new JPanel();
jp2.setLayout(new BoxLayout(jp2, BoxLayout.LINE_AXIS));
jb1 = new JButton("Genre");
jb1.addActionListener(this);
jb1.setMinimumSize(new Dimension(140, 60));
jb1.setPreferredSize(new Dimension(150, 60));
jb1.setMaximumSize(new Dimension(150, 60));
jb1.setAlignmentY(-70.0f);
jb2 = new JButton("Author");
jb2.addActionListener(this);
jb2.setMinimumSize(new Dimension(140, 60));
jb2.setPreferredSize(new Dimension(150, 60));
jb2.setMaximumSize(new Dimension(150, 60));
jb2.setAlignmentY(-70.0f);
jb3 = new JButton("Title");
jb3.addActionListener(this);
jb3.setMinimumSize(new Dimension(140, 60));
jb3.setPreferredSize(new Dimension(150, 60));
jb3.setMaximumSize(new Dimension(150, 60));
jb3.setAlignmentY(-70.0f);
jp1.add(Box.createHorizontalGlue());
jp1.add(jb1);
jp1.add(jb2);
jp1.add(jb3);
jp1.add(Box.createHorizontalGlue());
jf1.getContentPane().add(jp1);
jp1.setBounds(0, 0, 1080, 900);
jf1.getContentPane().add(jp2);
jp2.setBounds(0, 0, 1080, 900);
validate();
}
public void actionPerformed(ActionEvent e) {
Object code = e.getSource();
if (code == jb1) {
jtf1 = new JTextField("Enter Text");
jtf1.setPreferredSize(new Dimension(200,20));
jtf1.setMaximumSize(new Dimension(200,20));
jtf1.setMinimumSize(new Dimension(10,10));
jp2.add(Box.createHorizontalGlue());
jp2.add(jtf1);
jp2.add(Box.createHorizontalGlue());
jp2.validate();
}
else if (code == jb2) {
}
else if (code == jb3) {
}
}
public static void main(String[] args) {
Library shoe = new Library();
}
}
I gave it a quick try to solve the issue. Hope it helps. Do more tweaking in your code to perfectly match the layout.

Not adding Card Layout in JFrame

Can anyone see the code ? I want to make a page that has a banner and a pannel in which cards will change on the requirement. I added the Banner in JFrame (That is working fine) but The problem is that " CardLayout Panel is not adding in the JFrame".
Actually, I need this.
When button is pressed only card1 change to card2 but banner will remain same.
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class gui extends JFrame{
private static final long serialVersionUID = 1L;
JPanel
basic_panel,
card_Layout_panel,
banner_panel,
welcome_authenticaion_panel_card1;
CardLayout basic2;
JLabel
logo_label,
name_label;
public gui(){
server_login_gui();
add(basic_panel);
standard_gui();
}
public void server_login_gui(){
basic_panel = new JPanel();
basic_panel.setLayout(null);
basic_panel.setBorder(BorderFactory.createLineBorder(Color.BLUE, 2));
banner_panel = new JPanel();
banner_panel.setLayout(null);
banner_panel.setBorder(BorderFactory.createLineBorder(Color.GREEN, 2));
banner_panel.setSize(680, 200);//(400,100,400,100);
//////Banner inner things//////////////////////////////////////////////////
logo_label = new JLabel("Logo");
logo_label.setBounds(30,40,100,100);
logo_label.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 2));
banner_panel.add(logo_label);
name_label = new JLabel(" Name..... ");
name_label.setFont(new Font("Times new Roman", Font.BOLD | Font.ITALIC,25));
name_label.setBounds(200,80,400,50);
name_label.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 2));
banner_panel.add(name_label);
////////////////////////////////////////////////////////////////////////
// basic_panel.add(banner_panel,BorderLayout.NORTH);
///////// Card Layout//////////////
basic2 = new CardLayout();
card_Layout_panel = new JPanel(basic2);
card_Layout_panel.setBorder(BorderFactory.createLineBorder(Color.WHITE, 5));
basic_panel.add(card_Layout_panel,BorderLayout.CENTER);
welcome_authenticaion_panel_card1 = new JPanel();
welcome_authenticaion_panel_card1.setLayout(null);
welcome_authenticaion_panel_card1.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
welcome_authenticaion_panel_card1.setSize(680, 200);//(400,100,400,100);
welcome_authenticaion_panel_card1.setBounds(0,200,680,460);
card_Layout_panel.add(welcome_authenticaion_panel_card1, "1");
basic_panel.add(card_Layout_panel,BorderLayout.CENTER);
/////////////////////////////////////////////////////////////////////////
}
public void standard_gui(){
setSize(700,700);
setTitle("System");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
}
I want to make a page that has a banner and a pannel in which cards
will change on the requirement.
your component aren't focusable, there is required some event (JButton, Swing Timer) for switching the view by using CardLayout
for more info about CardLayout to read Oracle tutorial, for working code exampes, tons code examples are here
you code works without NullLayout (by set BorderLayout to parent JPanel), default LayoutManager for Jpanel is FlowLayout (accepts only getPreferredSize, childs aren't resizable with its parent/s)
my question is for why reason is there code line basic_panel.add(card_Layout_panel, BorderLayout.CENTER); twice, and another ...
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Gui extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel basic_panel, card_Layout_panel,
banner_panel, welcome_authenticaion_panel_card1;
private CardLayout basic2;
private JLabel logo_label, name_label;
public Gui() {
server_login_gui();
add(basic_panel);
standard_gui();
}
public void server_login_gui() {
basic_panel = new JPanel();
basic_panel.setLayout(new BorderLayout(10, 10));
basic_panel.setBorder(BorderFactory.createLineBorder(Color.BLUE, 2));
banner_panel = new JPanel();
//banner_panel.setLayout(null);
banner_panel.setBorder(BorderFactory.createLineBorder(Color.GREEN, 2));
banner_panel.setSize(680, 200);//(400,100,400,100);
//////Banner inner things//////////////////////////////////////////////////
logo_label = new JLabel("Logo");
//logo_label.setBounds(30, 40, 100, 100);
logo_label.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 2));
banner_panel.add(logo_label);
name_label = new JLabel(" Name..... ");
name_label.setFont(new Font("Times new Roman", Font.BOLD | Font.ITALIC, 25));
//name_label.setBounds(200, 80, 400, 50);
name_label.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 2));
banner_panel.add(name_label);
////////////////////////////////////////////////////////////////////////
basic_panel.add(banner_panel, BorderLayout.NORTH);
///////// Card Layout//////////////
basic2 = new CardLayout();
card_Layout_panel = new JPanel(basic2);
card_Layout_panel.setBorder(BorderFactory.createLineBorder(Color.WHITE, 5));
basic_panel.add(card_Layout_panel, BorderLayout.CENTER);
welcome_authenticaion_panel_card1 = new JPanel();
welcome_authenticaion_panel_card1.setLayout(null);
welcome_authenticaion_panel_card1.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
welcome_authenticaion_panel_card1.setSize(680, 200);//(400,100,400,100);
//welcome_authenticaion_panel_card1.setBounds(0, 200, 680, 460);
card_Layout_panel.add(welcome_authenticaion_panel_card1, "1");
basic_panel.add(card_Layout_panel, BorderLayout.CENTER);
/////////////////////////////////////////////////////////////////////////
}
public void standard_gui() {
setSize(700, 700);
setTitle("System");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Gui();
}
});
}
}
you're doing this basic_panel.add(card_Layout_panel,BorderLayout.CENTER); twice, hence the error. ( check before and after the welcome_authentication_panel_card )

Adding 2 JLists to a JPanel using BorderLayout

I need to add 2 JLists inside a JPanel for a crossword. The JPanel is located SOUTH and I'm using BorderLayout in the constructor to locate the JPanel.
The problem is, I can't see the 2 JLists inside the JPanel
package crossword;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Main extends JFrame implements MouseListener {
Container container;
//this is the constructor
public Main(){
super("Crossword");
container = getContentPane();
container.setLayout(new BorderLayout(1, 1));
container.setBackground(new Color(91,119,162));
container.add(login(),BorderLayout.NORTH);
container.add(buttons(),BorderLayout.WEST);
container.add(clues(),BorderLayout.SOUTH);
container.add(createMatrix(crosswordPanel()),BorderLayout.CENTER);
container.add(eastPanel(),BorderLayout.EAST);
}
public JPanel login(){
JPanel loginPanel = new JPanel();
loginPanel.setLayout(new FlowLayout(1, 15, 5));
loginPanel.setPreferredSize(new Dimension(8, 40));
loginPanel.setBackground(new Color(47,47,47));
loginPanel.setVisible(true);
JTextField inputUserName = new JTextField(15);
loginPanel.add(inputUserName);
JButton btnEnter = new JButton("Play!");
btnEnter.setPreferredSize(new Dimension(80,25));
loginPanel.add(btnEnter);
return loginPanel;
}
public JPanel buttons(){
JPanel buttonsPanel = new JPanel();
buttonsPanel.setLayout(new FlowLayout(5, 45, 15));
buttonsPanel.setPreferredSize(new Dimension(250, 200));
buttonsPanel.setBackground(new Color(91,119,162));
buttonsPanel.setVisible(true);
JButton newGame = new JButton("New game");
newGame.setPreferredSize(new Dimension(130,27));
newGame.setFont(new Font("Tahoma", Font.PLAIN, 11));
buttonsPanel.add(newGame);
JButton showChar = new JButton("Reveal letter");
showChar.setPreferredSize(new Dimension(130,27));
showChar.setFont(new Font("Tahoma", Font.PLAIN, 11));
buttonsPanel.add(showChar);
JButton showWord = new JButton("Reveal word");
showWord.setPreferredSize(new Dimension(130,27));
showWord.setFont(new Font("Tahoma", Font.PLAIN, 11));
buttonsPanel.add(showWord);
JButton verifyWord = new JButton("Verify");
verifyWord.setPreferredSize(new Dimension(130,27));
verifyWord.setFont(new Font("Tahoma", Font.PLAIN, 11));
buttonsPanel.add(verifyWord);
JButton revealAll = new JButton("Solution");
revealAll.setPreferredSize(new Dimension(130,27));
revealAll.setFont(new Font("Tahoma", Font.PLAIN, 11));
buttonsPanel.add(revealAll);
JLabel labelScore = new JLabel("Score:");
labelScore.setFont(new Font("Arial", Font.BOLD, 13));
buttonsPanel.add(labelScore);
return buttonsPanel;
}
public JPanel clues(){
JPanel clues = new JPanel();
clues.setLayout(new FlowLayout(3, 1, 1));
clues.setPreferredSize(new Dimension(200, 200));
clues.setBackground(new Color(91,119,162));
clues.setVisible(true);
JList horizontalList = new JList();
horizontalList.setBackground(Color.BLUE);
horizontalList.setBorder(BorderFactory.createEtchedBorder(1));
clues.add(horizontalList);
JList verticalList = new JList();
verticalList.setBackground(Color.ORANGE);
verticalList.setBorder(BorderFactory.createEtchedBorder(1));
clues.add(verticalList);
return clues;
}
public JPanel eastPanel(){
JPanel fill = new JPanel();
fill.setLayout(new FlowLayout(5, 25, 15));
fill.setPreferredSize(new Dimension(50, 200));
fill.setBackground(new Color(91,119,162));
fill.setVisible(true);
return fill;
}
public JPanel crosswordPanel(){
JPanel crosswordPanel = new JPanel();
crosswordPanel.setLayout(new GridLayout(10,10,0,0));
crosswordPanel.setBackground(Color.white);
//crosswordPanel.setPreferredSize(new Dimension(200, 200));
crosswordPanel.setBackground(new Color(91,119,162));
crosswordPanel.setVisible(true);
return crosswordPanel;
}
public JPanel createMatrix(JPanel crosswordPanel){
JPanel crosswordMatrix [][] = new JPanel [10][10];
for (int i = 0; i < crosswordMatrix.length ; i++) {
for (int j = 0; j < crosswordMatrix[0].length; j++) {
crosswordMatrix[i][j] = new JPanel();
crosswordMatrix[i][j].setBackground(Color.lightGray);
crosswordMatrix[i][j].setBorder(BorderFactory.createLineBorder(Color.black, 1));
crosswordMatrix[i][j].addMouseListener(this);
//crosswordMatrix[i][j].add(label);
crosswordPanel.add(crosswordMatrix[i][j]);
}
}
return crosswordPanel;
}
public static void main(String[] args) {
Main guiWindow = new Main();
Toolkit screenSize = Toolkit.getDefaultToolkit();
int width = screenSize.getScreenSize().width * 4/5;
int height = screenSize.getScreenSize().height * 4/5;
guiWindow.setSize(width, height);
guiWindow.setLocationRelativeTo(null);
guiWindow.setVisible(true);
guiWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Its not an MCVE, but based upon the limited code you posted: in the clues method a JPanel is constructed, yet nothing is ever added to it. Rather, you add the JLists directly to the object which contains the method (which I presume this extends JFrame). Perhaps try adding the components to the JPanel
public JPanel clues(){
JPanel clues = new JPanel();
...
clues.add(horizontalList);
....
clues.add(verticalList);
}

Can not save state of static checkbox

I have created a small settings menu, which save the state of checboxes by clicking save and quit and of course forget the state by clicking cancel.
I need a reference to the checkboxes, so I have to use static. But when I use it, the save state function won't work correctly anymore...Why? :(
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.util.prefs.Preferences;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
public class SettingsGui extends JPanel {
static Preferences PREFS = Preferences.userNodeForPackage(SettingsGui.class);
JCheckBox testAllPagesHaveSameRotation;
JCheckBox testAllPagesHaveSameSize;
JCheckBox testContent;
JTabbedPane tabbedPane;
ButtonGroup buttonGroup;
public SettingsGui() {
setPreferredSize(new Dimension(500, 500));
setLayout(new BorderLayout());
tabbedPane = new JTabbedPane(SwingConstants.LEFT, JTabbedPane.WRAP_TAB_LAYOUT);
tabbedPane.setFont(new Font("Arial", Font.BOLD, 13));
JPanel pdfCheckOptions = new JPanel(new GridLayout(3, 1));
testAllPagesHaveSameRotation = new JCheckBox("testAllPagesHaveSameRotation");
testAllPagesHaveSameRotation.setFont(new Font("Arial", Font.PLAIN, 15));
testAllPagesHaveSameSize = new JCheckBox("All pages have same size");
testAllPagesHaveSameSize.setFont(new Font("Arial", Font.PLAIN, 15));
testContent = new JCheckBox("Content");
testContent.setFont(new Font("Arial", Font.PLAIN, 15));
tabbedPane.addTab("Check Settings", pdfCheckOptions);
pdfCheckOptions.add(testAllPagesHaveSameRotation);
pdfCheckOptions.add(testAllPagesHaveSameSize);
pdfCheckOptions.add(testContent);
add(tabbedPane);
load();
}
public void store() {
PREFS.putBoolean("test1", testAllPagesHaveSameRotation.isSelected());
PREFS.putBoolean("test2", testAllPagesHaveSameSize.isSelected());
PREFS.putBoolean("test3", testContent.isSelected());
public void load() {
testAllPagesHaveSameRotation.setSelected(PREFS.getBoolean("test1", false));
testAllPagesHaveSameSize.setSelected(PREFS.getBoolean("test2", false));
testContent.setSelected(PREFS.getBoolean("test3", false));
}
class Gui {
JDialog settingsDialog;
SettingsGui content;
JButton saveButton;
JButton cancelButton;
JPanel buttonPanel;
public Gui() {
content = new SettingsGui();
settingsDialog = new JDialog(settingsDialog, "Settings");
settingsDialog.setContentPane(content);
settingsDialog.setLocationRelativeTo(null);
settingsDialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
settingsDialog.pack();
settingsDialog.setVisible(true);
buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
saveButton = new JButton("Save and Quit");
cancelButton = new JButton("Cancel");
buttonPanel.add(saveButton);
buttonPanel.add(cancelButton);
settingsDialog.add(buttonPanel, BorderLayout.SOUTH);
saveButton.addActionListener(new java.awt.event.ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
content.store();
settingsDialog.dispose();
}
});
cancelButton.addActionListener(new java.awt.event.ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
PREFS.putBoolean("test1", testAllPagesHaveSameRotation.isSelected());
PREFS.putBoolean("test2", testAllPagesHaveSameSize.isSelected());
PREFS.putBoolean("test3", testContent.isSelected());
settingsDialog.dispose();
}
});
}
}
public static void main(String[] args) {
new SettingsGui().new Gui();
}
}

Resizing jpanel according to the components present in it

In the below example, in the west side of the border layout, there is a parent panel which has a BoxLayout and couple of panels inside. The problem is that the west panel covers the whole area from top to bottom. The FlowLayout used for child panels inside the parent panel consume a lot of area. Is it possible to compress each JPanel according to the components? Also, it should remain the same even when the window is maximized?
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.BoxLayout;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JButton;
public class Sample extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Sample frame = new Sample();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Sample() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.NORTH);
JLabel lblNewLabel = new JLabel("New label");
panel.add(lblNewLabel);
JPanel panel_1 = new JPanel();
contentPane.add(panel_1, BorderLayout.WEST);
panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.Y_AXIS));
JPanel panel_2 = new JPanel();
panel_1.add(panel_2);
textField = new JTextField();
panel_2.add(textField);
textField.setColumns(2);
textField_1 = new JTextField();
panel_2.add(textField_1);
textField_1.setColumns(2);
JPanel panel_3 = new JPanel();
panel_1.add(panel_3);
JCheckBox chckbxNewCheckBox = new JCheckBox("New check box");
panel_3.add(chckbxNewCheckBox);
JPanel panel_4 = new JPanel();
panel_1.add(panel_4);
JButton btnNewButton = new JButton("New");
panel_4.add(btnNewButton);
JButton btnNewButton_1 = new JButton("New");
panel_4.add(btnNewButton_1);
}
}
One approach is to add panel_1 to an enclosing panel. The default FlowLayout conforms itself to the preferred size of the enclosed components when you pack() the enclosing Window. I've added a gray panel to CENTER as a placeholder; resize the frame to see the effect.
JPanel flowPanel = new JPanel();
flowPanel.add(panel_1);
As tested:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.BoxLayout;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JButton;
public class Sample extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
Sample frame = new Sample();
frame.pack();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Sample() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.NORTH);
JLabel lblNewLabel = new JLabel("New label");
panel.add(lblNewLabel);
JPanel panel_1 = new JPanel();
panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.Y_AXIS));
JPanel panel_2 = new JPanel();
panel_1.add(panel_2);
textField = new JTextField();
panel_2.add(textField);
textField.setColumns(2);
textField_1 = new JTextField();
panel_2.add(textField_1);
textField_1.setColumns(2);
JPanel panel_3 = new JPanel();
panel_1.add(panel_3);
JCheckBox chckbxNewCheckBox = new JCheckBox("New check box");
panel_3.add(chckbxNewCheckBox);
JPanel panel_4 = new JPanel();
panel_1.add(panel_4);
JButton btnNewButton = new JButton("New");
panel_4.add(btnNewButton);
JButton btnNewButton_1 = new JButton("New");
panel_4.add(btnNewButton_1);
JPanel flowPanel = new JPanel();
flowPanel.add(panel_1);
contentPane.add(flowPanel, BorderLayout.WEST);
contentPane.add(new JPanel(){
#Override
public Dimension getPreferredSize() {
return new Dimension(320, 240);
}
#Override
public Color getBackground() {
return Color.lightGray;
}
}, BorderLayout.CENTER);
}
}

Categories

Resources