W
wagen
Gast
Hallo zusammen, bräuchte dringend Hilfe für folgendes Problem:
ich habe eine Swing Applikation, welche JRadioButtons und JCheckBoxes enthaltet. diese werden im ActionListener eines Search-Buttons mit isSelected() überpfüft und das Resultat wird (zu Test-zwecken) vorläufig auf der Konsole ausgegeben.
Jedoch wird nur immer der default gesetzte Button ausgegeben und keine Checkbox, egal was ich verändere und was nicht.
Bitte helft mir
Gruss wagen
P.S. IDE: RAD
ich habe eine Swing Applikation, welche JRadioButtons und JCheckBoxes enthaltet. diese werden im ActionListener eines Search-Buttons mit isSelected() überpfüft und das Resultat wird (zu Test-zwecken) vorläufig auf der Konsole ausgegeben.
Jedoch wird nur immer der default gesetzte Button ausgegeben und keine Checkbox, egal was ich verändere und was nicht.
Bitte helft mir
Gruss wagen
P.S. IDE: RAD
Code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileFilter;
public class GUI
{
private JFrame jFrame = null;
private JPanel jContentPane = null;
private JPanel jAdvancedPane = null;
private JPanel jMainPane = null;
private JPanel jOutputPane = null;
private JPanel jSearchPane = null;
private JPanel jSearchFieldPane = null;
private JPanel jAdvanced1Pane = null;
private JPanel jAdvanced2Pane = null;
private JMenuBar jJMenuBar = null;
private JMenu fileMenu = null;
private JMenu helpMenu = null;
private JMenuItem exitMenuItem = null;
private JMenuItem aboutMenuItem = null;
private JMenuItem browseMenuItem = null;
private JDialog aboutDialog = null;
private JPanel aboutContentPane = null;
private JPanel searchButtonPane = null;
private JLabel aboutVersionLabel = null;
private JLabel titleLabel = null;
private JLabel emtpyLabel = null;
private TextField searchField = null;
private JRadioButton entireWordRadioButton = null;
private JRadioButton beginWithRadioButton = null;
private JRadioButton endWithRadioButton = null;
private ButtonGroup wordOptionsButtonGroup = null; // @jve:decl-index=0:
private JCheckBox ignoreCaseCheckBox = null;
private JCheckBox fuzzyCheckBox = null;
private JButton searchButton = null;
private JTextPane jOutputTextPane = null;
private JScrollPane jOutputScrollPane = null;
public String filetype;
public ArrayList<String> readFileArrayList;
public int file = 0;
/**
* This method initializes jFrame
*
* @return javax.swing.JFrame
*/
private JFrame getJFrame()
{
if (jFrame == null)
{
jFrame = new JFrame();
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setJMenuBar(getJJMenuBar());
jFrame.setSize(400, 500);
jFrame.setResizable(false);
jFrame.setContentPane(getJContentPane());
jFrame.setTitle("STERA-Search");
}
return jFrame;
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane()
{
if (jContentPane == null)
{
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane
.setBorder(new LineBorder(new Color(238, 238, 238), 20));
jContentPane.add(getSearchPane(), BorderLayout.NORTH);
jContentPane.add(getMainPane(), BorderLayout.CENTER);
}
return jContentPane;
}
private JPanel getSearchFieldPane()
{
jSearchFieldPane = new JPanel();
searchField = new TextField();
searchField.setColumns(20);
jSearchFieldPane.add(searchField);
return jSearchFieldPane;
}
private JPanel getSearchPane()
{
jSearchPane = new JPanel();
jSearchPane.setLayout(new GridLayout(2, 1));
titleLabel = new JLabel();
titleLabel.setText("STERA-Search");
titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
titleLabel.setDisplayedMnemonic(KeyEvent.VK_UNDEFINED);
jSearchPane.add(titleLabel);
jSearchPane.add(getSearchFieldPane());
return jSearchPane;
}
private JPanel getAdvancedPane()
{
FlowLayout flowLayout = new FlowLayout();
flowLayout.setAlignment(FlowLayout.RIGHT);
flowLayout.setHgap(3);
flowLayout.setVgap(5);
jAdvancedPane = new JPanel();
jAdvancedPane.setLayout(new GridLayout(2, 2));
emtpyLabel = new JLabel();
jAdvancedPane.add(getAdvanced1Pane());
jAdvancedPane.add(getAdvanced2Pane());
jAdvancedPane.add(emtpyLabel);
searchButtonPane = new JPanel();
searchButton = new JButton();
searchButton.setText("Search");
searchButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
getSearchButtonAction();
}
});
searchButtonPane.setLayout(flowLayout);
searchButtonPane.add(searchButton, null);
jAdvancedPane.add(searchButtonPane);
return jAdvancedPane;
}
public void getSearchButtonAction()
{
boolean b = entireWordRadioButton.isSelected();
if (b) {
System.out.println("Entire Word!");
}
b = beginWithRadioButton.isSelected();
if (b) {
System.out.println("Begin with!");
}
b = endWithRadioButton.isSelected();
if (b) {
System.out.println("End with!");
}
if (ignoreCaseCheckBox.isSelected()) {
System.out.println("Ignore case");
}
if(fuzzyCheckBox.isSelected()) {
System.out.println("Fuzzy");
}
System.out.println("Entire word: " + entireWordRadioButton.isSelected());
System.out.println("Begin with: " + beginWithRadioButton.isSelected());
System.out.println("End with: " + endWithRadioButton.isSelected());
System.out.println("Ignore Case: " + ignoreCaseCheckBox.isSelected());
System.out.println("Fuzzy: " + fuzzyCheckBox.isSelected());
if (file == 1)
{
String s = "";
int z = 0;
for (int i = 0; i < readFileArrayList.size(); i++)
{
if (readFileArrayList.get(i).equals(searchField.getText()))
{
s = readFileArrayList.get(i);
z++;
}
}
s = "Suchwort: " + searchField.getText() + "\n" + s;
s = s + ": " + z + " mal gefunden!";
jOutputTextPane.setText(s);
} else
{
jOutputTextPane.setText("Error: No file selected");
}
}
private JPanel getAdvanced1Pane()
{
jAdvanced1Pane = new JPanel();
jAdvanced1Pane.setLayout(new GridLayout(3, 1));
jAdvanced1Pane.setBorder(BorderFactory.createTitledBorder(null,
"Word options", TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION, new Font("Microsoft Sans Serif",
Font.PLAIN, 11), new Color(0, 70, 213)));
wordOptionsButtonGroup = new ButtonGroup();
entireWordRadioButton = new JRadioButton();
entireWordRadioButton.setText("Entire Word");
entireWordRadioButton.setSelected(true);
wordOptionsButtonGroup.add(entireWordRadioButton);
jAdvanced1Pane.add(entireWordRadioButton);
beginWithRadioButton = new JRadioButton();
beginWithRadioButton.setText("Begin with");
wordOptionsButtonGroup.add(beginWithRadioButton);
jAdvanced1Pane.add(beginWithRadioButton);
endWithRadioButton = new JRadioButton();
endWithRadioButton.setText("End with");
wordOptionsButtonGroup.add(endWithRadioButton);
jAdvanced1Pane.add(endWithRadioButton);
return jAdvanced1Pane;
}
private JPanel getAdvanced2Pane()
{
jAdvanced2Pane = new JPanel();
jAdvanced2Pane.setLayout(new GridLayout(3, 1));
jAdvanced2Pane.setBorder(BorderFactory.createTitledBorder(null,
"Advanced options", TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION, new Font("Microsoft Sans Serif",
Font.PLAIN, 11), new Color(0, 70, 213)));
ignoreCaseCheckBox = new JCheckBox();
ignoreCaseCheckBox.setText("Ignore case");
fuzzyCheckBox = new JCheckBox();
fuzzyCheckBox.setText("Fuzzy");
jAdvanced2Pane.add(ignoreCaseCheckBox, ignoreCaseCheckBox.getName());
jAdvanced2Pane.add(fuzzyCheckBox, fuzzyCheckBox.getName());
return jAdvanced2Pane;
}
private JPanel getMainPane()
{
jMainPane = new JPanel();
jMainPane.setLayout(new GridLayout(2, 1));
jMainPane.add(getAdvancedPane(), getAdvancedPane().getName());
jMainPane.add(getOutputPane());
return jMainPane;
}
public JPanel getOutputPane()
{
jOutputPane = new JPanel();
jOutputPane.setBorder(BorderFactory.createTitledBorder(null, "Output",
TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION, new Font("Microsoft Sans Serif",
Font.PLAIN, 11), new Color(0, 70, 213)));
jOutputPane.add(getJOutputScrollPane(), null);
return jOutputPane;
}
/**
* This method initializes jJMenuBar
*
* @return javax.swing.JMenuBar
*/
private JMenuBar getJJMenuBar()
{
if (jJMenuBar == null)
{
jJMenuBar = new JMenuBar();
jJMenuBar.add(getFileMenu());
jJMenuBar.add(getHelpMenu());
}
return jJMenuBar;
}
/**
* This method initializes jMenu
*
* @return javax.swing.JMenu
*/
private JMenu getFileMenu()
{
if (fileMenu == null)
{
fileMenu = new JMenu();
fileMenu.setText("File");
fileMenu.add(getBrowseMenuItem());
fileMenu.add(getExitMenuItem());
}
return fileMenu;
}
/**
* This method initializes jMenu
*
* @return javax.swing.JMenu
*/
private JMenu getHelpMenu()
{
if (helpMenu == null)
{
helpMenu = new JMenu();
helpMenu.setText("Help");
helpMenu.add(getAboutMenuItem());
}
return helpMenu;
}
/**
* This method initializes jMenuItem
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getExitMenuItem()
{
if (exitMenuItem == null)
{
exitMenuItem = new JMenuItem();
exitMenuItem.setText("Exit");
exitMenuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
}
return exitMenuItem;
}
/**
* This method initializes jMenuItem
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getAboutMenuItem()
{
if (aboutMenuItem == null)
{
aboutMenuItem = new JMenuItem();
aboutMenuItem.setText("About");
aboutMenuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JDialog aboutDialog = getAboutDialog();
aboutDialog.pack();
Point loc = getJFrame().getLocation();
loc.translate(20, 20);
aboutDialog.setLocation(loc);
aboutDialog.setVisible(true);
}
});
}
return aboutMenuItem;
}
/**
* This method initializes aboutDialog
*
* @return javax.swing.JDialog
*/
private JDialog getAboutDialog()
{
if (aboutDialog == null)
{
aboutDialog = new JDialog(getJFrame(), true);
aboutDialog.setTitle("About");
aboutDialog.setContentPane(getAboutContentPane());
}
return aboutDialog;
}
/**
* This method initializes aboutContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getAboutContentPane()
{
if (aboutContentPane == null)
{
aboutContentPane = new JPanel();
aboutContentPane.setLayout(new BorderLayout());
aboutContentPane.add(getAboutVersionLabel(), BorderLayout.CENTER);
}
return aboutContentPane;
}
/**
* This method initializes aboutVersionLabel
*
* @return javax.swing.JLabel
*/
private JLabel getAboutVersionLabel()
{
if (aboutVersionLabel == null)
{
aboutVersionLabel = new JLabel();
aboutVersionLabel.setText("Version 1.0");
aboutVersionLabel.setHorizontalAlignment(SwingConstants.CENTER);
}
return aboutVersionLabel;
}
/**
* This method initializes jMenuItem
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getBrowseMenuItem()
{
if (browseMenuItem == null)
{
browseMenuItem = new JMenuItem();
browseMenuItem.setText("Browse");
browseMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
Event.CTRL_MASK, true));
browseMenuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
getBrowseAction();
}
});
}
return browseMenuItem;
}
public void getBrowseAction()
{
JFileChooser chooser = new JFileChooser();
chooser.addChoosableFileFilter(new FileFilter()
{
public boolean accept(File f)
{
if (f.isDirectory())
return true;
filetype = "TXT";
return f.getName().toLowerCase().endsWith(".txt");
}
public String getDescription()
{
return "TXT";
}
});
chooser.addChoosableFileFilter(new FileFilter()
{
public boolean accept(File f)
{
if (f.isDirectory())
return true;
filetype = "HTML";
return f.getName().toLowerCase().endsWith(".html");
}
public String getDescription()
{
return "HTML";
}
});
chooser.setMultiSelectionEnabled(false);
JFrame fileframe = new JFrame();
fileframe.setTitle("Choose File");
fileframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
if (chooser.showOpenDialog(fileframe) == JFileChooser.APPROVE_OPTION)
{
readFileArrayList = new ArrayList<String>();
try
{
Scanner readFile = new Scanner(chooser.getSelectedFile());
while (readFile.hasNext())
{
readFileArrayList.add(readFile.next());
}
} catch (FileNotFoundException e)
{
}
String s = "File:\n" + chooser.getSelectedFile() + "\nFiletype: "
+ filetype;
for (int i = 0; i < readFileArrayList.size(); i++)
{
s = s + "\n" + readFileArrayList.get(i);
}
file = 1;
jOutputTextPane.setText(s);
}
}
/**
* This method initializes jOutputTextPane
*
* @return javax.swing.JTextPane
*/
private JTextPane getJOutputTextPane()
{
if (jOutputTextPane == null)
{
jOutputTextPane = new JTextPane();
jOutputTextPane.setBackground(new Color(238, 238, 238));
jOutputTextPane.setPreferredSize(new Dimension(300, 126));
jOutputTextPane.setEditable(false);
}
return jOutputTextPane;
}
/**
* This method initializes jOutputScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJOutputScrollPane()
{
if (jOutputScrollPane == null)
{
jOutputScrollPane = new JScrollPane();
jOutputScrollPane.setPreferredSize(new Dimension(300, 126));
jOutputScrollPane.setViewportView(getJOutputTextPane());
jOutputScrollPane.setBorder(null);
}
return jOutputScrollPane;
}
/**
* Launches this application
*/
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
GUI application = new GUI();
application.getJFrame().setVisible(true);
}
});
}
}