JComboBox Index just returns 0 upon request - java

Im just working on a JFrame.I added a JComboBox there but unfortunately the JComboBox Index, and so the selected Item doesn't change upon "the action of changing", I mean when I select another Item on the Swing Frame. When requested, it only returns an Index of 0, no matter what Item is selected. The name of the ComboBox is "Kataloge".
It does not return me any Error.
How can I fix this ?
static BufferedImage icon;
private JButton update;
private JButton getKata;
private JComboBox<String> Kataloge;
private JLabel Title;
private JLabel WhichKatalog;
private JLabel WhichDatum;
private JLabel line;
private JPanel topper;
private JPanel middle;
private JPanel bottom;
private JPanel frame;
public String Katalog = "Fragenkatalog 1 (normiert)";
public static void main(String args[]) {
MainFrame frame = new MainFrame();
frame.draw();
}
public MainFrame(){
setTitle("Fragebogen erstellen Section");
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
}
draw();
setResizable(false);
try {
icon = ImageIO.read(new File("icon.png"));
} catch (IOException e) {
e.printStackTrace();
}
setIconImage(icon);
setVisible(true);
}
public void draw(){
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
update = new JButton();
getKata = new JButton();
Title = new JLabel();
line = new JLabel();
WhichKatalog = new JLabel();
WhichDatum = new JLabel();
topper = new JPanel();
middle = new JPanel();
bottom = new JPanel();
frame = new JPanel();
setSize(575, 220);
getKata.setFont(new java.awt.Font("Tahoma", 0, 14));
getKata.setText("Fragebogen erstellen");
getKata.addActionListener(new ActionHandler());
update.setFont(new java.awt.Font("Tahoma", 0, 14));
update.setText("Katalog bearbeiten");
update.addActionListener(new ActionHandler());
Kataloge = new JComboBox<String>();
Kataloge.addItem("Fragenkatalog 1 (normiert)");
Kataloge.addItem("Fragenkatalog 2 (normal)");
Kataloge.setFont(new java.awt.Font("Tahoma", 0, 14));
Kataloge.addItemListener(new ItemHandlerMainFrame());
Title.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
Title.setText("Main Menu");
WhichKatalog.setText("Ausgewählter Katalog: "+Katalog);
WhichDatum.setText(new Datum().getDatum());
line.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
frame.setSize(575, 220);
topper.setSize(575,40);
topper.setLayout(new FlowLayout());
topper.add(Title, CENTER_ALIGNMENT);
middle.setSize(575, 150);
middle.setLayout(null);
middle.add(getKata).setBounds(15, 30, 160, 30);;
middle.add(update).setBounds(195,30,145,30);;
middle.add(Kataloge).setBounds(360, 30, 200, 30);;
bottom.setSize(575,30);
bottom.setLayout(new BorderLayout(50,5));
bottom.add(line, BorderLayout.NORTH);
bottom.add(WhichKatalog, BorderLayout.WEST);
bottom.add(WhichDatum, BorderLayout.EAST);
frame.setLayout(null);
frame.add(topper).setBounds(0, 10, 575, 40);;
frame.add(middle).setBounds(0,45,575,60);;
frame.add(bottom).setBounds(15, 150, 535, 30);;
getContentPane().add(frame);
setLocationRelativeTo(null);
setLocationRelativeTo(null);
}
public void close(){
this.setVisible(false);
this.dispose();
}
private class ActionHandler implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="Fragebogen erstellen"){
close();
FragebogenErstellen frame = new FragebogenErstellen();
frame.drawIt();
}
}
}
private class ItemHandlerMainFrame implements ItemListener{
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED){
System.out.println("Changed to: "+Kataloge.getSelectedIndex());
}
//Katalog = (String) Kataloge.getSelectedItem();
if (Katalog == "Fragenkatalog 1 (normiert)"){
WhichKatalog.setText("Ausgewählter Katalog: "+Katalog);
}if (Katalog == "Fragenkatalog 2 (normal)"){
WhichKatalog.setText("Ausgewählter Katalog: "+Katalog+" ");
}
}
}

You're calling draw twice, which is going to cause no end of issue as it's re-creating many of you objects.
Basically what's happening, is your double layering your components, you actually have two JComboBoxs on the screen, one you interact with and one which you can't. In your case, it's actually the one you can't interact with which was created last, it's returning 0 constantly when you call getSelectedIndex, because that's what's selected
In this case, there's no reason for draw to be public, in fact, for the most part, it could just be part of classes constructor
e.getActionCommand()=="Fragebogen erstellen" is not how Strings are compared in Java, you should be using String#equals
Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify

Related

Scroll Bar does not adjust to JPanel dimensions, Java JScrollPane

I am trying to get a scroll bar for a Panel that will get a big number of lines, however I am stuck whit this.
As you can see in my window (click here to open the picture), Scroll bars does not adjust to the panel's dimensions
Below you can see the code i am working with:
public class Config extends JFrame {
private static JPanel contentPane;
private static JScrollPane paneSiteScroll;
private static JPanel paneSite;
private JTextField txtURL;
private JButton btnAdd;
private JLabel lblName;
private JTextField txtName;
private JPanel paneBar;
/**
* Launch the application.
*/
public static void config() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Config frame = new Config();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
} catch (Throwable e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
* #throws Throwable
*/
public Config() throws Throwable {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 550);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
//Start top Add Bar
paneBar = new JPanel();
paneBar.setBounds(0, 0, 700, 35);
contentPane.add(paneBar);
paneBar.setLayout(null);
JButton btnBack = new JButton("Back");
btnBack.setBounds(1, 3, 65, 28);
paneBar.add(btnBack);
btnBack.setVerticalAlignment(SwingConstants.TOP);
JLabel lblUrl = new JLabel("URL:");
lblUrl.setBounds(80, 9, 35, 20);
paneBar.add(lblUrl);
txtURL = new JTextField();
txtURL.setBounds(115, 5, 310, 26);
paneBar.add(txtURL);
txtURL.setColumns(10);
lblName = new JLabel("Name:");
lblName.setBounds(435, 9, 47, 20);
paneBar.add(lblName);
txtName = new JTextField();
txtName.setBounds(480, 5, 155, 26);
paneBar.add(txtName);
txtName.setColumns(10);
btnAdd = new JButton("Add");
btnAdd.setBounds(635, 3, 61, 29);
paneBar.add(btnAdd);
//End top Add Bar
//Start Scroll and Panel configuration
paneSiteScroll = new JScrollPane();
paneSite = new JPanel();
paneSite.setLayout(null);
paneSite.setBounds(10, 40, 400, 600);
/*Testing different dimensions configuration*/
paneSiteScroll.setSize(new Dimension(300, 300));
paneSiteScroll.setBounds(10, 40, 300, 200);
paneSiteScroll.setPreferredSize(new Dimension(400, 300));
/*Testing different dimensions configuration*/
paneSiteScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
paneSiteScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
paneSiteScroll.getViewport().add(paneSite);
contentPane.add(paneSiteScroll);
paneSiteScroll.setVisible(true);
paneSite.setVisible(true);
//End Scroll and Panel configuration
try {
Categories.categories(paneSite); //GET CATEGORIES
} catch (Throwable e) {
e.printStackTrace();
}
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!txtURL.getText().isEmpty() || !txtName.getText().isEmpty()) {
InsertDB insert = new InsertDB();
try {
insert.insertDB("INSERT INTO sites (siUrl, siName) VALUES ('" + txtURL.getText() +"'"
+ ", '" + txtName.getText() + "')",
conn);
} catch (Exception e1) {
e1.printStackTrace();
}
}
else {
JOptionPane.showMessageDialog(null, "URL or Name cannot be empty!");
}
}
});
btnBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Back action button
dispose();
}
});
}
}
This is my first question, I do not know what else to add, also i visited many question from stak overflow with similar problems but i couldn't figure it out.
Thanks in advance.
Finally fixed it, I restored the layout to null and added paneSite.setPreferredSize(new Dimension(600, 600)); after paneSite.setbounds. This make it work.

JLabel not positioning correctly

I am just throwing together a quick and dirty GUI to display some data when I ran into an odd issue. The last label I add to the JFrame doesn't want to be positioned or display the border I put on it, so it looks like this:
Here is my code:
public DisplayData (Connection tConn)
{
ID = tID;
conn = tConn;
setupObjects();
setupFrame();
}
private void setupObjects()
{
JLabel caseLabel = new JLabel ("Case #:");
JLabel dateLabel = new JLabel ("Date:");
JLabel reportLabel = new JLabel ("Report:");
JLabel offenceLabel = new JLabel ("Offence:");
JLabel descriptionLabel = new JLabel ("Description:");
this.add(caseLabel);
this.add(dateLabel);
this.add(reportLabel);
this.add(offenceLabel);
this.add(descriptionLabel);
caseLabel.setBounds(50, 50, 130, 25); //x, y, width, height
dateLabel.setBounds(50, 100, 130, 25);
reportLabel.setBounds(50, 150, 130, 25);
offenceLabel.setBounds(50, 200, 130, 25);
descriptionLabel.setBounds(100, 50, 130, 25);
caseLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
dateLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
reportLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
offenceLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
descriptionLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
}
private void setupFrame()
{
this.setTitle("Data Display");
this.setSize (650, 700); //Width, Height
this.setLocation(300, 10);
this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(null);
}
Yes, I know I should be using a proper layout manager, but like I said i just wanted something quick and dirty. Plus, I will not be beaten by something that should be this simple. Any ideas would be appreciated.
EDIT:
As Compass and Neophyte pointed out, my order of operations was off. Flipped my method calls and all is good again in the world. Thanks for the 2nd pair of eyes.
Contrary to the original poster's strategy, or any of the answers so far, the best approach to this problem is to use layouts.
Here is an example that shows how easy it is to position fields using layouts, and to change the GUI on later updates to the specification.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class CourtDetailsGUI {
private JComponent ui = null;
static final String[] FIELD_NAMES = {
"Case #:",
"Date:",
"Report:",
"Offence:",
"Plaintiff:",
"Defendant:"
};
CourtDetailsGUI(int num) {
initUI(num);
}
public void initUI(int num) {
if (ui != null) {
return;
}
ui = new JPanel(new BorderLayout(4, 4));
ui.setBorder(new EmptyBorder(4, 4, 4, 4));
ui.add(getFieldsPanel(num), BorderLayout.PAGE_START);
JTextArea ta = new JTextArea(5, 40);
JScrollPane sp = new JScrollPane(ta);
JPanel p = new JPanel(new GridLayout());
p.add(sp);
p.setBorder(new TitledBorder("Details"));
ui.add(p);
}
private JPanel getFieldsPanel(int num) {
JPanel outerPanel = new JPanel(new FlowLayout());
JPanel innerPanel = new JPanel(new GridLayout(0, 1, 15, 15));
outerPanel.add(innerPanel);
for (int ii=1; ii<num; ii++) {
JLabel l = new JLabel(FIELD_NAMES[ii]);
l.setBorder(new LineBorder(Color.BLACK));
innerPanel.add(l);
}
return outerPanel;
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
for (int ii=0; ii<FIELD_NAMES.length; ii++) {
CourtDetailsGUI o = new CourtDetailsGUI(ii+1);
JFrame f = new JFrame("Data " + (ii+1));
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
}
};
SwingUtilities.invokeLater(r);
}
}
Your order of operations is incorrect.
You initially call setupObjects();
This plays your objects out onto a JFrame, which has the default LayoutManager of BorderLayout.
By using the default add(Component comp) method, with BorderLayout, you end up putting the component into null for BorderLayout, which is not supposed to be normal. Furthermore, the reason you can't see the border for this object is because the border is actually the size of the frame. If you explicitly set a region for BorderLayout, then you'll see it work, but setting to no region seems to just break BorderLayout.
Additional add calls appear to free the previous item from the BorderLayout null management, allowing the bounds to take over.
Afterwards, you call setupFrame(); which removes the layout manager, but does not refresh what is currently rendered.
This sets the layout to null, which does nothing to how the frame is displayed, but just removes the layout.
To avoid this issue, call setupFrame(); prior to setupObjects();, and then setVisible(true) can be called at the end of setupObjects();

How to set another Items Visible when Checkbox is clicked?

I have written a frame in java swing . In it I have a checkbox . I want , that after clicking checkbox others Item will change it visibility. I was trying to do it as in code below but is not working as i wish .
public InFrm() {
setTitle("In");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
getContentPane().setLayout(new GridLayout(1, 1, 0, 0));
seeMe=false;
JSplitPane splitPane = new JSplitPane();
splitPane.setResizeWeight(0.7);
splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
getContentPane().add(splitPane);
JPanel panel = new JPanel();
splitPane.setLeftComponent(panel);
panel.setLayout(null);
JPanel panel_1 = new JPanel();
splitPane.setRightComponent(panel_1);
panel_1.setLayout(null);
JLabel lblKind= new JLabel("Kind");
lblKind.setBounds(10, 8, 33, 14);
lblKind.setVisible(seeMe);
panel_1.add(lblKind);
JComboBox ChoiceOd = new JComboBox();
ChoiceOd.setBounds(53, 5, 28, 20);
ChoiceOd.setVisible(seeMe);
panel_1.add(ChoiceOd);
// more items using seeMe
JCheckBox chckbxOd = new JCheckBox("Od");
chckbxOd.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
seeOd();
}
});
chckbxOd.setBounds(6, 150, 97, 23);
panel.add(chckbxOd);
}
protected void seeOd() {
if(seeMe){
seeMe=false;
}
else
{
seeMe=true;
}
}
In your see method you only set the flag but of course it does not set the visibility of your component. Set the visibility direct to the component, than it will work
Your Listener should look something like this:
public void itemStateChanged(ItemEvent ev) {
Object item = ev.getItem();
if (ev.getStateChange() == ItemEvent.DESELECTED) {
//hide the component associated with this item
} else {
//show the component associated with this item
}
}

Receive KeyEvents when JTextField is focused

I'm developing a game and I have a JFrame that receives in input the player name in a JTextField.
What I want is the possibility to close the window by either pressing a JButton or by pressing the ENTER key.
When the window opens the JTextField must have the focus (the cursor should appear in the component).
I already saw:
How do you make key bindings for a java.awt.Frame?
How do you make key binding for a JFrame no matter what JComponent is in focus?
but I have not solved the problem, there's probably something wrong in the focus management.
I tried the following code:
public class PlayerNameWindow extends JFrame implements KeyListener {
private String playerName;
private JLabel backgroundLabel;
private JLabel enterNameLabel;
private JButton confirmButton;
private JTextField nameField;
private Image background;
public PlayerNameWindow() {
initComponents();
}
private void initComponents() {
backgroundLabel = new JLabel();
enterNameLabel = new JLabel();
confirmButton = new JButton();
nameField = new JTextField();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setPreferredSize(new Dimension(400, 200));
setResizable(false);
getContentPane().setLayout(null);
addKeyListener(this);
enterNameLabel.setFont(new Font("Tahoma", 1, 18));
enterNameLabel.setForeground(new Color(255, 255, 255));
enterNameLabel.setText("Enter your name:");
getContentPane().add(enterNameLabel);
enterNameLabel.setBounds(40, 80, 160, 30);
confirmButton.setText("Confirm");
confirmButton.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
confirmButtonMouseClicked(evt);
}
});
confirmButton.setBounds(160, 150, 90, 25);
getContentPane().add(confirmButton);
getContentPane().add(nameField);
nameField.setBounds(220, 80, 140, 30);
getContentPane().add(backgroundLabel);
backgroundLabel.setBounds(0, 0, 400, 200);
pack();
setLocationRelativeTo(null);
}
private void confirmButtonMouseClicked(MouseEvent evt) {
confirmAction();
}
private void confirmAction() {
playerName = nameField.getText();
System.exit(0);
}
public String getPlayerName() {
return this.playerName;
}
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if (code == KeyEvent.VK_ENTER)
System.exit(0);
}
public void keyReleased(KeyEvent e) {
//do-nothing
}
public void keyTyped(KeyEvent e) {
//do-nothing
}
}
How can I do?
Thanks
add keyListener to the JTextField. in your code, nameField.addKeyListener(this);

java parsing data from component

I am creating a game, and I am doing a character selection screen which has a JTextField for entering the user name, the screen also has a JButton "Create Character", which, when pressed will parse the JTextField and if any problems(space in name, begin with space, etc..) it will put some text to a JLabel next to the JTextField.
I hook up the JButton to an actionPerformed method, which calls a function that parses the data. My problem is that every time I press the button, a new label is being placed on top of the original JLabel.
I create the JLabel like this:
public static JLabel label;
label = new JLabel();
// some properties
And I call the parsing method like so:
parseCharacterSelection(nameInput, label);
where nameInput is the JTextField.
My question is, why is there a new Label being created every time I press the JButton
public void parseCharacterCreation(JTextField input, JLabel label) {
// Variable that hold whether or not
// the User Name is acceptable
player_name = input.getText();
// Make some initial checks on the
// input string
//----------------------------------
if( player_name.isEmpty() ) {
label.setText("You need to pick a username");
} else if( player_name.startsWith(" ") ) {
label.setText("Name Cannot start with a space");
} else {
pass = true;
}
// Attempt to write to file
// Catch some errors
//----------------------------------
if(pass == true) {
try {
if( player_name.contains(" ") ) {
player_name = player_name.replaceAll("\\s", "_");
player_file = new File("src/Resources/characters/" + player_name + ".properties");
}
if(!player_file.exists()) {
player_file.createNewFile();
}
FileWriter fw = new FileWriter(player_file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(player_name);
bw.close();
} catch( IOException e ) {
e.printStackTrace();
} finally {
System.out.println("DONE");
}
}
}
// ScreenCharacter Class
public class ScreenCharacter extends JPanel {
public static JButton create;
public static JTextField nameInput;
public static JLabel errorLabel;
public ScreenCharacter() {
this.setLayout(null);
this.add(createUserName());
this.add(createMainArea());
}
private static JPanel createUserName() {
MatteBorder matteBorder = new MatteBorder(1, 0, 1, 0, Color.decode("#222222"));
EmptyBorder emptyBorder = new EmptyBorder(10, 75, 10, 10);
CompoundBorder border = new CompoundBorder(matteBorder, emptyBorder);
JPanel userNameArea = new JPanel();
userNameArea.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
userNameArea.setBounds(0, 35, Engine.screenX, 50);
userNameArea.setBorder(border);
userNameArea.setBackground(new Color(255, 255, 255, 70));
JLabel nameLabel = new JLabel();
nameLabel.setText("Enter Your Username: ");
nameInput = new JTextField();
nameInput.setForeground(Color.black);
nameInput.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0));
nameInput.setPreferredSize(new Dimension(200, 25));
errorLabel = new JLabel();
errorLabel.setText("This is a test label");
errorLabel.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 0));
errorLabel.setPreferredSize(new Dimension(200, 25));
errorLabel.setAlignmentX(JLabel.LEFT_ALIGNMENT);
userNameArea.add(nameLabel);
userNameArea.add(nameInput);
userNameArea.add(errorLabel);
return userNameArea;
}
private static JPanel createMainArea() {
JPanel resetCreatePanel = new JPanel(new GridLayout(1, 2));
resetCreatePanel.setPreferredSize(new Dimension(300, 30));
resetCreatePanel.setBackground(Color.black);
create = new JButton("Create Character");
create.setBorder(BorderFactory.createLineBorder(Color.decode("#171717")));
create.setFont(new Font("Dialog", 1, 11));
create.setBackground(Color.black);
create.setForeground(Color.white);
create.setFocusPainted(false);
create.setContentAreaFilled(false);
create.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
if(!errorLabel.getText().isEmpty()) {
errorLabel.setText("");
} else {
Engine.p_parser.parseCharacterCreation(nameInput, errorLabel);
}
}
});
resetCreatePanel.add(create);
}
}
// Engine
public class Engine {
public static PlayerParser p_parser;
public Engine() {
p_parser = new PlayerParser();
}
}
Thanks everyone for the help. The "problem" was that the JPanel that the components were sitting on had a semi-transparent background. And JComponents with transparency get messed up somehow, which made it appear like there are multiple labels being created. But it works fine without transparency.

Categories