JButtons Wont Display?

I'm sorry for posting this but, I can't find out why my Array of JButtons won't display in my ButtonsPanel. After adding them, I tried using my code in a separate class to test my code and it worked!, but why don't my buttons show up in this class?

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.LineBorder;

public class WordGui extends JFrame {

private JPanel contentPane;
private JTextField textField;
private JLabel lblNewLabel_1;
private JPanel buttonPanel;
private JTextArea txtrShuffleHistory;
private Word word ;
private boolean val;
private JButton btnGo;
private JButton button;
private JButton btnShuffleText;
private JPanel panel_1;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    try {
        com.jtattoo.plaf.noire.NoireLookAndFeel.setTheme("Small-Font","","05:Bageo,Dexter");
        UIManager.setLookAndFeel("com.jtattoo.plaf.noire.NoireLookAndFeel");
    } catch (Throwable e) {
        e.printStackTrace();
    }
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                WordGui frame = new WordGui();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public WordGui() {
    setTitle("Word App Gui");
    setResizable(false);
    setBounds(new Rectangle(100, 100, 600, 200));
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 719, 394);
    contentPane = new JPanel();
    contentPane.setBounds(new Rectangle(100, 100, 600, 200));
    contentPane.setSize(new Dimension(600, 250));
    contentPane.setPreferredSize(new Dimension(600, 250));
    contentPane.setBorder(new LineBorder(new Color(204, 0, 255), 5, true));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JPanel panel = new JPanel();
    panel.setBorder(new LineBorder(new Color(204, 0, 255), 3, true));
    panel.setBounds(10, 11, 683, 45);
    contentPane.add(panel);

    panel_1 = new JPanel();
    panel_1.setBorder(new LineBorder(new Color(204, 0, 255), 3, true));
    panel_1.setBounds(140, 59, 553, 286);
    contentPane.add(panel_1);
    panel_1.setLayout(null);

    JLabel lblNewLabel = new JLabel("Enter a new word here:");
    lblNewLabel.setBounds(10, 11, 183, 14);
    panel_1.add(lblNewLabel);

    textField = new JTextField();
    textField.setBounds(224, 8, 157, 20);
    panel_1.add(textField);
    textField.setColumns(10);

    btnGo = new JButton("Go");
    btnGo.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            if(ev.getSource()== btnGo){
                assignWord(textField.getText());

            }
        }
    });
    btnGo.setBounds(391, 7, 57, 23);
    panel_1.add(btnGo);

    JLabel lblOriginalText = new JLabel("Original Text:");
    lblOriginalText.setBounds(10, 36, 84, 14);
    panel_1.add(lblOriginalText);

    lblNewLabel_1 = new JLabel("New label");
    lblNewLabel_1.setToolTipText("This is the original text entered\r\n");
    lblNewLabel_1.setHorizontalTextPosition(SwingConstants.CENTER);
    lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
    lblNewLabel_1.setBorder(new LineBorder(new Color(204, 0, 255), 3, true));
    lblNewLabel_1.setBounds(90, 36, 358, 79);
    panel_1.add(lblNewLabel_1);

    JLabel lblShuffledText = new JLabel("Shuffled Text:");
    lblShuffledText.setBounds(10, 126, 110, 14);
    panel_1.add(lblShuffledText);

    buttonPanel = new JPanel();
    buttonPanel.setBorder(new LineBorder(new Color(204, 0, 255), 4, true));
    buttonPanel.setBounds(10, 147, 533, 56);
    panel_1.add(buttonPanel);
    buttonPanel.setLayout(new GridLayout(1, 0, 0, 0));

    btnShuffleText = new JButton("Shuffle Text");
    btnShuffleText.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            if(ev.getSource() == btnShuffleText){
                shuffle();
            }

        }
    });
    btnShuffleText.setBounds(90, 214, 196, 39);
    panel_1.add(btnShuffleText);

    button = new JButton("Reset");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            if(ev.getSource() == button){
                textField.setText("");
                getGoBtn().setEnabled(true);
                getShuffleBtn().setEnabled(false);
                getButtonPanel().removeAll();
            }
        }
    });
    button.setBounds(294, 214, 196, 39);
    panel_1.add(button);

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    scrollPane.setBounds(10, 59, 128, 258);
    contentPane.add(scrollPane);

    txtrShuffleHistory = new JTextArea();
    txtrShuffleHistory.setToolTipText("View Shuffle History");
    txtrShuffleHistory.setText("Shuffle History\r\n=================");
    txtrShuffleHistory.setEditable(false);
    scrollPane.setViewportView(txtrShuffleHistory);

    JButton btnClear = new JButton("Clear");
    btnClear.setBounds(10, 322, 126, 23);
    contentPane.add(btnClear);
    valid();
}

public void valid(){
    JTextField field = new JTextField();
    while(val == false){
    int result = JOptionPane.showConfirmDialog(null,field, "Enter a Text?", JOptionPane.OK_CANCEL_OPTION);
    if(result == JOptionPane.OK_OPTION){
        String s = field.getText();
        assignWord(s);
        }
    else if(result == JOptionPane.CANCEL_OPTION){
        getResetBtn().setEnabled(false);
        getShuffleBtn().setEnabled(false);
        val = true;
    }
}
}
public void assignWord(String s){

    try{word = new Word(s); 
    val = true;
    getTextLabel().setText("<html><body><font size = 30 >"+s+"</font></body></html>");
    getTextArea().append("\nOriginal Word :"+s+"\n=================");
    getGoBtn().setEnabled(false);
    getResetBtn().setEnabled(true);
    getShuffleBtn().setEnabled(true);
    }
    catch(RuntimeException e){JOptionPane.showMessageDialog(getParent(),e.getMessage()); val = false;}

}
public void shuffle(){
    word.shuffle();
    getButtonPanel().removeAll();
    String temp = word.getShuffledText();
    JButton[] buttons = new JButton[word.getText().length()];
    for(int x = 0; x < word.getShuffledText().length(); x++){
        buttons[x] = new JButton(""+temp.charAt(x));
        getBody().add(buttons[x]);
        buttons[x].setVisible(true);
    }
    getTextArea().append("\n"+word.getShuffledText());
}
public JLabel getTextLabel() {
    return lblNewLabel_1;
}
public JPanel getButtonPanel() {
    return buttonPanel;
}
public JTextArea getTextArea() {
    return txtrShuffleHistory;
}
public JTextField getTextField() {
    return textField;
}
public JButton getGoBtn() {
    return btnGo;
}
public JButton getResetBtn() {
    return button;
}
public JButton getShuffleBtn() {
    return btnShuffleText;
}
public JPanel getBody() {
    return panel_1;
}
}

I have the shuffle method which does the adding of JButtons, I'm sorry for posting this long long code, but can somebody compile this and figure out why the buttons wont show ? I tried changing the panels layout but still doesn't work,

here's the supporting class

    import java.util.*;

    public class Word {
private String text;
private List<Character> charList;
private String shuffled;

public Word(String str) {
    if (str.length() < 3) {
        throw new RuntimeException("Word must be more than 2 characters...");
    }
    if(invalid(str)) {
        throw new RuntimeException("Word must not be composed of a single character...");
    }
    text = str.toUpperCase();
    charList = getChars();
    shuffle();
}



private boolean invalid(String s) {
    int count = 1;
    for (int i = 1; i < s.length(); i++) {
        if (Character.toLowerCase(s.charAt(0)) == Character.toLowerCase(s.charAt(i)))
            count++;
    }
    return (count == s.length());
}

private ArrayList<Character> getChars() {
    ArrayList<Character> tempList = new ArrayList<Character>();
    for (int i = 0; i < text.length(); i++) {
        tempList.add(text.charAt(i));
    }
    return tempList;
}

public void shuffle() {
    String orig = shuffled;
    String tempShuffled;
    do {
        Collections.shuffle(charList);
        tempShuffled = listToString();
    } while (tempShuffled.equals(orig) || tempShuffled.equals(text));
    shuffled = tempShuffled;
}

private String listToString() {
    String strTemp = "";
    for (Character ch: charList) {
        strTemp += ch;
    }
    return strTemp;
}

public String toString() {
    return text;
}

public String getShuffledText() {
    return shuffled;
}

public String getText(){
    return text;
}
}

Answers


You're using absolute positioning (null layout) for the JPanel panel_1. Each component in the JButton array buttons will have a default size of 0 x 0 so will not appear.

Swing was designed to use a layout manager so its recommended to always use one. It removes the need to set component dimensions and locations. Use one for panel_1, such as GridLayout.

Also invoke

panel_1.revalidate();
panel_1.repaint();

after adding all of the buttons from the array.

Aside: Java naming conventions show that variables use camelCase such as panelOne instead of panel_1.


Need Your Help

How make the script wait/sleep in a simple way in unity

c# unity3d monodevelop sleep wait

How can put between the TextUI.text = .... a sleep function, for wait 3 seconds between each phrase?

CreateRemoteThread, WriteProcessMemory, VirtualAllocEx - why use them?

windows winapi visual-c++ win32-process

Recently I have been reading up articles about DLL injection and I understand them fairly well.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.