I'm using the BeautyEye Laf for a Java Swing application.
I'm setting a JTextField inside a JPopupMenu. The JTextField appears disabled no matter what I do. The code is a bit complicated but I've made this snippet that's easily testable
public static void main(String[] s)
throws Exception
{
BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.generalNoTranslucencyShadow;
org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
final JPopupMenu popupTable = new JPopupMenu();
// find panel
JLabel findLabel = new JLabel("Filter for:");
findLabel.setPreferredSize(new Dimension(60, 20));
final JTextField findTextField = new JTextField();
findTextField.setColumns(10);
final JPanel container = new JPanel();
container.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 0));
container.add(findLabel);
container.add(findTextField);
popupTable.add(container);
JButton button = new JButton("Action");
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
buttonPanel.add(button);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(popupTable, BorderLayout.CENTER);
panel.add(buttonPanel, BorderLayout.SOUTH);
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(900, 800);
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(panel, BorderLayout.CENTER);
frame.setVisible(true);
button.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int x = (frame.getWidth() / 2);
int y = (frame.getHeight() / 2);
popupTable.show(frame, x, y);
}
}
);
}
If I remove the LaF (comment the first two lines) everything works as expected. The JTextField is editable. I'm asking here first in hope that I'm doing something wrong. If this proves to be a bug I will post this as an issue on Github.
In the end it was a minor change in the library. From the author:
You can find class
org.jb2011.lnf.beautyeye.ch7_popup.TranslucentPopupFactory source code
at line 416, change "setFocusableWindowState(false);" to
"setFocusableWindowState(true);" or just delete this line.
Related
I am trying to add a JScrollPane (createTeamScrollPane) to a JPanel (createTeamPanel) that I have. I have a frame, with a BorderLayout with the NORTH portion being used by a JPanel called tabMenu, and then the CENTER portion I would like my 'createTeamPanel' to have this scrolling ability as it will have more content than what I can fit on the screen at once. I am then adding both panels to the frame. Currently the code as is runs but the window appears blank. Once resizing the window, I then see the 3 buttons in the NORTH portion of my frame (why is this happening?) and when I click on 'Create Team' it brings up the list of JLabels and JButtons I expect but I don't see any scrolling bars?
public static void main (String args[]) {
JFrame frame = new JFrame();
frame.setTitle("v0.01");
frame.setSize(800, 800);
//frame.setLayout(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
JPanel tabMenu = new JPanel();
JPanel createTeamPanel = new JPanel();
createTeamPanel.setLayout(new BoxLayout(createTeamPanel, BoxLayout.Y_AXIS));
createTeamPanel.setSize(800, 700);
createTeamPanel.setVisible(showCreateTeamPanel);
createTeamPanel.setBackground(Color.gray);
JScrollPane createTeamScrollPane = new JScrollPane(createTeamPanel);
createTeamScrollPane.setBounds(50, 50, 200, 500);
createTeamScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
createTeamScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
createTeamScrollPane.setPreferredSize(new Dimension(500,500));
//createTeamPanel.add(createTeamScrollPane);
List<Player> teamList = MockTeams.initTeam();
int xcoord = 100;
int ycoord = 50;
for(Player player : teamList) {
JLabel label = new JLabel(player.getName());
label.setBounds(xcoord, ycoord, Constants.buttonWidth, Constants.buttonHeight);
JButton addToTeamBtn = new JButton("Add to team");
addToTeamBtn.setBounds(xcoord + 100, ycoord, Constants.buttonWidth, Constants.buttonHeight);
addToTeamBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
myTeam.add(player);
addToTeamBtn.setEnabled(false);
}
});
createTeamPanel.add(label);
//createTeamFrame.add(label);
createTeamPanel.add(addToTeamBtn);
//createTeamFrame.add(addToTeamBtn);
ycoord += 50;
}
JButton createTeamBtn = new JButton("Create Team");
createTeamBtn.setBounds(0,0,150,20);
createTeamBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Hide/Show Create team panel
if (!showCreateTeamPanel) {
showCreateTeamPanel = true;
createTeamPanel.setVisible(showCreateTeamPanel);
} else {
showCreateTeamPanel = false;
createTeamPanel.setVisible(showCreateTeamPanel);
}
}
});
JButton manageTeamBtn = new JButton("Team Statistics");
manageTeamBtn.setBounds(100,150,150,40);
JButton resetBtn = new JButton("Reset Season");
resetBtn.setBounds(100,200,150,40);
tabMenu.add(createTeamBtn);
tabMenu.add(manageTeamBtn);
tabMenu.add(resetBtn);
mainPanel.add(tabMenu, BorderLayout.NORTH);
mainPanel.add(createTeamPanel, BorderLayout.CENTER);
frame.add(mainPanel);
}
Expected result is to see a scrolling ability on the createTeamPanel but it is not there.
Fixed: I was able to add the JScrollPane to the mainPanel with:
mainPanel.add(createTeamScrollPane, BorderLayout.CENTER);
How can I fix blank screen on jframe and set values of vgap and hgap from textfield. i am using borderlayout for this.
import java.awt.*;
import javax.swing.*;
public class d1{
public static void main(String[] args) {
JFrame f1 = new JFrame ("Border Layout") ;
f1.setLayout(new BorderLayout());
f1.setVisible(true);
f1.setSize(400,400);
f1.setLocationRelativeTo(null);
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField t1 = new JTextField();
t1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
JTextField t2 = new JTextField();
t2.setBorder(BorderFactory.createLineBorder(Color.BLACK));
JPanel p1 = new JPanel ();
p1.setLayout(new BorderLayout());
p1.add(new JButton("East"), BorderLayout.EAST);
p1.add(new JButton("South"), BorderLayout.SOUTH);
p1.add(new JButton("West"), BorderLayout.WEST);
p1.add(new JButton("North"), BorderLayout.NORTH);
p1.add(new JButton("Center"), BorderLayout.CENTER);
JPanel p2 = new JPanel ();
p2.setLayout(new BorderLayout());
JPanel p3 = new JPanel ();
p3.setLayout(new BorderLayout());
p3.add(new JLabel("Vgap"), BorderLayout.WEST);
p3.add(t1, BorderLayout.CENTER);
JPanel p4 = new JPanel ();
p4.setLayout(new BorderLayout());
p4.add(new JLabel("Hgap"), BorderLayout.WEST);
p4.add(t2, BorderLayout.CENTER);
JPanel p5 = new JPanel();
p5.setLayout(new BorderLayout());
p5.add(new JLabel("Container of BorderLayout"));
JPanel p6 = new JPanel();
p6.setLayout(new BorderLayout());
p6.add(new JLabel("BorderLayout Properties"));
JPanel p7 = new JPanel ();
p7.setLayout(new BorderLayout());
p7.add(p6, BorderLayout.NORTH);
p7.add(p2, BorderLayout.SOUTH);
p2.add(p3, BorderLayout.NORTH);
p2.add(p4, BorderLayout.SOUTH);
f1.add(p1, BorderLayout.CENTER);
f1.add(p7, BorderLayout.SOUTH);
f1.add(p5, BorderLayout.NORTH);
}
}
after this code.
There should be space between north-south and center, north, south and center.
I can not fix blank screen problem when frame is opened.
Add your panels before f1.setVisible(True) on frame and f1.pack() the frame after it.
You don't need to set a fixed size for your frame. Your added components should take care of it.
To set your components, look at MigLayout. It's easy to use and set components the way you need it.
please check this link you can find it here but first you should add ActionListener into your textField objects so that they can take numbers from inside of themselves. Providing white space in a Swing GUI
something like that
t1 = new JTextField();
t1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String v = t1.getText();
int numberhGap = Integer.parseInt(v);
}
});
t1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
t2 = new JTextField();
t2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String h = t2.getText();
int numberhGap = Integer.parseInt(h);
}
});
and declare t1 and t2 outside main method.
like this
public class d1{
static JTextField t1;
static JTextField t2;
I am playing around with Java GUIs, and I came across JLabel.setVerticalAlignment. I have set something up so that curlLeft and curlRight would go to the corners. However, it does not seem to have ny effect. Why is that so?
private void prepareGUI() throws IOException {
mainFrame = new JFrame("Holy Bible");
mainFrame.setSize(700, 500);
mainFrame.setLayout(new GridLayout(1, 2));
mainFrame.setLocationRelativeTo(null);
mainFrame.setIconImage(new ImageIcon(getClass().getResource("/assets/bible/textures/icon.png")).getImage());
mainFrame.getContentPane().setBackground(Color.WHITE);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent) {
System.exit(0);
}
});
mainFrame.addKeyListener(this);
leftPanel = new JPanel();
leftPanel.setBackground(Color.WHITE);
leftPanel.setLayout(new FlowLayout());
rightPanel = new JPanel();
rightPanel.setBackground(Color.WHITE);
rightPanel.setLayout(new FlowLayout());
leftLabel = new JLabel("", JLabel.CENTER);
leftLabel.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
leftPanel.add(leftLabel);
rightLabel = new JLabel("", JLabel.CENTER);
rightLabel.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
rightPanel.add(rightLabel);
leftCurl = new JLabel();
leftCurl.setHorizontalAlignment(JLabel.LEFT);
leftCurl.setVerticalAlignment(JLabel.BOTTOM);
leftCurl.setIcon(new ImageIcon(getClass().getResource("/assets/bible/textures/curlleft15.png")));
leftPanel.add(leftCurl);
rightCurl = new JLabel();
rightCurl.setHorizontalAlignment(JLabel.RIGHT);
rightCurl.setVerticalAlignment(JLabel.BOTTOM);
rightCurl.setIcon(new ImageIcon(getClass().getResource("/assets/bible/textures/curlright15.png")));
rightPanel.add(rightCurl, BorderLayout.SOUTH);
mainFrame.add(leftPanel);
mainFrame.add(rightPanel);
mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH); // Maximizes frame
mainFrame.setUndecorated(fullScreen);
mainFrame.setVisible(true);
}
All the variables needed are initialized at class level.
Your JLabels are not going to the corners because you are adding them to JPanels that have a FLowLayout. With FlowLayout your components don't occupy the 100% of the space of the JPanel, they only occupy the necessary.
I changed the 2 FlowLayouts to GridLayouts and now I can see the different orientations.
(Also, as your objective is learning how this works, I recommend you set a border on each component so you can see where their bounds are. This is quite good for understanding Swing's layout management).
Okay, so when I press the JButton menuselect1, I want it to create 4 new objecs, attack1 2 3 and 4, and then add them to the JPanel fightmenu.
This is my code so far, it's a mini pokemon game.
First I create all my objects, and then I set the sizes and adds them to the different JPanels
public class MainFrame extends JFrame {
JPanel mainwindow = new JPanel();
JPanel bottom = new JPanel();
JPanel combat = new JPanel();
JPanel selectionmenu = new JPanel();
JPanel fightmenu = new JPanel();
JButton menuselect1 = new JButton("Fight");
JButton menuselect2 = new JButton("Minimons");
JButton menuselect3 = new JButton("Bag");
JButton menuselect4 = new JButton("Run");
JButton attack1 = new JButton("Tackle");
JButton attack2 = new JButton("Lightningbolt");
JButton attack3 = new JButton("Thunder-Shock");
JButton attack4 = new JButton("Hyper-Beam");
JButton poke1 = new JButton("Ekans");
JButton poke2 = new JButton("Pikachu");
public static void main(String[] args){
new MainFrame();
}
public MainFrame(){
super("MiniMon");
setSize(640,640);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(mainwindow);
// SIZES
combat.setPreferredSize(new Dimension(640,452));
bottom.setPreferredSize(new Dimension(640,160));
selectionmenu.setPreferredSize(new Dimension(320,160));
fightmenu.setPreferredSize(new Dimension(320,160));
mainwindow.setLayout(new BorderLayout());
mainwindow.add(combat, BorderLayout.NORTH);
mainwindow.add(bottom, BorderLayout.SOUTH);
combat.setLayout(new BorderLayout());
combat.add(poke1, BorderLayout.NORTH);
combat.add(poke2, BorderLayout.SOUTH);
bottom.setLayout(new BorderLayout());
bottom.add(selectionmenu, BorderLayout.EAST);
bottom.add(fightmenu, BorderLayout.WEST);
selectionmenu.setLayout(new GridLayout(2,2));
selectionmenu.add(menuselect1);
selectionmenu.add(menuselect2);
selectionmenu.add(menuselect3);
selectionmenu.add(menuselect4);
fightmenu.setLayout(new GridLayout(2,2));
setVisible(true);
}
}
I set up my fightmenu to use a 2x2 gridlayout, so I just need to add the 4 objects whenever I press the JButton menuselect1. I'm not really sure how to go about this. I know I should add an eventlistener, but when I tried, it did nothing at all.
I tried doing this:
fightmenu.setLayout(new GridLayout(2,2));
menuselect1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fightmenupress();
}
private void fightmenupress() {
fightmenu.add(attack1);
fightmenu.add(attack2);
fightmenu.add(attack3);
fightmenu.add(attack4);
}
} );
But it just did nothing.
When you add (or remove) components to a visible GUI, the basic code is:
panel.add(...);
panel.revalidate(); // to invoke the layout manager
panel.repaint(); // to repaint all the components on the panel
I added revalidate and repaint, and it worked!
private void fightmenupress() {
fightmenu.add(attack1);
fightmenu.add(attack2);
fightmenu.add(attack3);
fightmenu.add(attack4);
fightmenu.revalidate();
fightmenu.repaint();
}
} );
So I'm trying to create a gui, I've tinkered with gui's before in java but I'm still new to them. So my issued here is that my JLabels (butLabel & cbLabel) are filled with buttons and checkboxes. Sadly my JFrame will only show whichever is set to the BorderLayout.CENTER. NORTH & SOUTH don't ever show, even if I only set the butLabel to SOUTH and don't even use the cbLabel. What am I overlooking?? It's much appreciated, thanks!
public class mainWindow
{
JFrame frame = new JFrame("Main Window");
JLabel butLabel = new JLabel();
JLabel cbLabel = new JLabel();
JButton showBut = new JButton("Show");
JButton exitBut = new JButton("Exit");
JButton addBut = new JButton("Add");
JButton remBut = new JButton("Remove");
JCheckBox aCB = new JCheckBox("Airplane");
JCheckBox bCB = new JCheckBox("Boat");
JCheckBox cCB = new JCheckBox("Clock");
public mainWindow()
{
frame.setLayout(new BorderLayout()); //I know this is set by default to BorderLayout but I just did it when I was out of options to try.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setMinimumSize(new Dimension(360, 480));
butLabel.setLayout(new GridLayout(1,4));
cbLabel.setLayout(new GridLayout(2, 2));
butLabel.add(showBut);
butLabel.add(exitBut);
butLabel.add(addBut);
butLabel.add(remBut);
cbLabel.add(aCB);
cbLabel.add(bCB);
cbLabel.add(cCB);
frame.add(butLabel, BorderLayout.CENTER);
frame.add(cbLabel, BorderLayout.NORTH);
}
public void setVisible()
{
butLabel.setVisible(true);//Didn't think I needed butLabel.setVisible or the cbLabel.setVisible but
cbLabel.setVisible(true);//again I was trying things that I thought might make sense.
frame.setVisible(true);
}
}
do not use Label for grouping elements, use JPanel instead
I have tried replace all
Label
with
Panel
it works