How to set another Items Visible when Checkbox is clicked? - java

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
}
}

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();

JComboBox Index just returns 0 upon request

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

final JPanel with added labels (java)

Iam trying to position my jlabel in a jpanel what is made final because of an actionlistener.
final JPanel panelPayDetails = new JPanel();
panelPayDetails.setBounds(250, 25, 350, 250);
JLabel lblnumber = new JLabel("Insert Number:");
lblnumber.setFont(Applicatie.FONT_12_BOLD);
lblnumber.setBounds(5, 5, 200, 20);
panelPayDetails.add(lblnumber);
panelPayDetails.setVisible(false);
jpExtraDetails.add(panelPayDetails);
bill.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if(bill.getSelectedItem() == "CREDIT CARD")
{
panelPayDetails.setVisible(true);
}
}
});
im not luck so far.. because the label is positioned in the middle of the jpanel.. how come?
Because the default layout manager for JPanel is FlowLayout, which centers things. Read this tutorial.

windowbuilder textfield not working with combobox

I have a problem where I have a comboBox that causes an action that sets the text in a textField. Here's the code:
public class Main extends JFrame implements ActionListener{
private JPanel contentPane;
private JTextField textField;
private JComboBox comboBox;
//public static void main - nothing much in it except Main frame = new Main();
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 563, 407);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
comboBox.addActionListener(frame);
textField = new JTextField();
textField.setBounds(42, 99, 445, 235);
textField.setText("HERE");
contentPane.add(textField);
textField.setColumns(10);
comboBox = new JComboBox();
comboBox.setModel(new DefaultComboBoxModel(new String[] {"Bob", "Dan ", "Emily"}));
comboBox.setBounds(42, 48, 140, 29);
contentPane.add(comboBox);
/*ONE WAY OF DOING IT: comboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.setText(studentOutputString((String)comboBox.getSelectedItem()));
textField.setText("BLAH");
}
});*/
}
public String studentOutputString(String student){
String s = student + "is printed.";
return s;
}
public void actionPerformed(ActionEvent e) {
comboBox = (JComboBox) e.getSource();
String selectedStudent = (String) comboBox.getSelectedItem();
textField.setText(studentOutputString(selectedStudent));
}
Nothing shows up in the textField. Any idea about what I'm doing wrong?
I reformatted it and caught up on my past threads.
You need to generate an action on the ComboBox in order for the action listener to be called and set the text of the TextField. Try selecting an item in the ComboBox
Also, comboBox.getSelectedItem() might return null if there are other events besides change of selected items (e.g. an event is generated before a selection was made). In that case your call to student + "is printed." inside studentOutputString() will throw a null-pointer exception

Categories