Search Results

Search found 39 results on 2 pages for 'gridbaglayout'.

Page 1/2 | 1 2  | Next Page >

  • JTextField only shows as a slit Using GridBagLayout, need help

    - by Bill Caffery
    Hi thank you in advance for any help, I'm trying to build a simple program to learn GUI's but when I run the code below my JTextFields all show as a slit thats not large enough for even one character. cant post an image but it would look similar to: Label [| where [| is what the text field actually looks like import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class lab6start implements ActionListener { JTextField custNameTxt; JTextField acctNumTxt; JTextField dateCreatedTxt; JButton checkingBtn; JButton savingsBtn; JTextField witAmountTxt; JButton withDrawBtn; JTextField depAmountTxt; JButton depositBtn; lab6start() { JFrame bankTeller = new JFrame("Welcome to Suchnsuch Bank"); bankTeller.setSize(500, 280); bankTeller.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); bankTeller.setResizable(false); bankTeller.setLayout(new GridBagLayout()); bankTeller.setBackground(Color.gray); //bankTeller.getContentPane().add(everything, BorderLayout.CENTER); GridBagConstraints c = new GridBagConstraints(); JPanel acctInfo = new JPanel(new GridBagLayout()); c.gridx = 0; c.gridy = 0; c.gridwidth = 2; c.gridheight = 1; c.insets = new Insets(5,5,5,5); bankTeller.add(acctInfo, c); c.gridwidth = 1; //labels //name acct# balance interestRate dateCreated JLabel custNameLbl = new JLabel("Name"); c.gridx = 0; c.gridy = 0; c.insets = new Insets(0,0,0,0); acctInfo.add(custNameLbl, c); custNameTxt = new JTextField("customer name",50); c.gridx = 1; c.gridy = 0; c.insets = new Insets(5,5,5,5); acctInfo.add(custNameTxt,c); custNameTxt.requestFocusInWindow(); JLabel acctNumLbl = new JLabel("Account Number"); c.gridx = 0; c.gridy = 1; c.insets = new Insets(5,5,5,5); acctInfo.add(acctNumLbl,c); acctNumTxt = new JTextField("account number"); c.gridx = 1; c.gridy = 1; c.insets = new Insets(5,5,5,5); acctInfo.add(acctNumTxt,c); JLabel dateCreatedLbl = new JLabel("Date Created"); c.gridx = 0; c.gridy = 2; c.insets = new Insets(5,5,5,5); acctInfo.add(dateCreatedLbl,c); dateCreatedTxt = new JTextField("date created"); c.gridx = 1; c.gridy = 2; c.insets = new Insets(5,5,5,5); acctInfo.add(dateCreatedTxt,c); //buttons checkingBtn = new JButton("Checking"); c.gridx = 0; c.gridy = 3; c.insets = new Insets(5,5,5,5); acctInfo.add(checkingBtn,c); savingsBtn = new JButton("Savings"); c.gridx = 1; c.gridy = 3; c.insets = new Insets(5,5,5,5); acctInfo.add(savingsBtn,c); //end of info panel JPanel withDraw = new JPanel(new GridBagLayout()); c.gridx = 0; c.gridy = 1; c.insets = new Insets(5,5,5,5); bankTeller.add(withDraw, c); witAmountTxt = new JTextField("Amount to Withdraw:"); c.gridx = 0; c.gridy = 0; c.insets = new Insets(5,5,5,5); withDraw.add(witAmountTxt,c); withDrawBtn = new JButton("Withdraw"); c.gridx = 1; c.gridy = 0; c.insets = new Insets(5,5,5,5); withDraw.add(withDrawBtn,c); //add check balance //end of withdraw panel JPanel deposit = new JPanel(new GridBagLayout()); c.gridx = 1; c.gridy = 1; c.insets = new Insets(5,5,5,5); bankTeller.add(deposit, c); depAmountTxt = new JTextField("Amount to Deposit"); c.gridx = 0; c.gridy = 0; c.insets = new Insets(5,5,5,5); deposit.add(depAmountTxt,c); depositBtn = new JButton("Deposit"); c.gridx = 1; c.gridy = 0; c.insets = new Insets(5,5,5,5); deposit.add(depositBtn,c); bankTeller.setVisible(true); // action/event checkingBtn.addActionListener(this); savingsBtn.addActionListener(this); withDrawBtn.addActionListener(this); depositBtn.addActionListener(this); } public void actionPerformed(ActionEvent e) { if (e.getSource()== checkingBtn) { witAmountTxt.requestFocusInWindow(); //checking newcheck = new checking(); } } } /* String accountType = null; accountType = JOptionPane.showInputDialog(null, "Checking or Savings?"); if (accountType.equalsIgnoreCase("checking")) { checking c_Account = new checking(); } else if (accountType.equalsIgnoreCase("savings")) { // savings s_Account = new savings(); } else { JOptionPane.showMessageDialog(null, "Invalid Selection"); } */

    Read the article

  • How to put a background image on GridBagLayout

    - by Loligans
    I am trying to work with layout managers for the first time, and they are just kicking me in the teeth. I am trying to make a background image and then put buttons on top, using GridBagLayout, if there is a a better layoutmanager please do tell. As for trying to learn how to use layout managers, its very difficult and any learning references would also be much appreciated. This is what it looks like currently, I can get the frame to show the full image, but when i use gridlayout manager, it does that public void addComponentsToPane(Container pane){ BackgroundImage image = new BackgroundImage(); JButton button1, button2, button3, button4, button5; pane.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); if(shouldFill){ c.fill = GridBagConstraints.NONE; } button1 = new JButton("Button 1"); if (shouldWeightX) { c.weightx = 0.5; } c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 1; c.gridy = 0; button1.setOpaque(false); pane.add(button1, c); button2 = new JButton("Button 2"); c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 0.5; c.gridx = 0; c.gridy = 0; button2.setOpaque(false); pane.add(button2, c); button3 = new JButton("Button 3"); c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 0.5; c.gridx = 2; c.gridy = 0; button3.setOpaque(false); pane.add(button3, c); button4 = new JButton("Long-Named Button 4"); c.fill = GridBagConstraints.HORIZONTAL; c.ipady = 40; //make this component tall c.weightx = 0.0; c.gridwidth = 3; c.gridx = 0; c.gridy = 1; pane.add(button4, c); button5 = new JButton("button 1"); c.fill = GridBagConstraints.HORIZONTAL; c.ipady = 0; //reset to default c.weighty = 1.0; //request any extra vertical space c.anchor = GridBagConstraints.PAGE_END; //bottom of space c.insets = new Insets(10,0,0,0); //top padding c.gridx = 1; //aligned with button 2 c.gridwidth = 2; //2 columns wide c.gridy = 2; //third row pane.add(button5, c); c.ipadx = 800; c.ipady = 400; pane.add(image, c); } This is what i'm trying to make it look like

    Read the article

  • How do I get a JPanel with an empty JLabel to take up space in a GridBagLayout

    - by user2888663
    I am working on a GUI for a project at school. I am using a GridBagLayout in swing. I want to have a label indicating the input(a type of file @ x = 0, y = 0), followed by another label(the actual file name once selected @ x = 1, y = 0), followed by a browse button for a file chooser( @ x = 2, y = 0). The label at (1,0) is initially blank, however I want the area that the text will occupy to take up some space when the label contains no text. I also want the space between the label at (0,0) and the button at (2,0) to remain constant. To achieve this, I'm trying to put the label onto a panel and then play with the layouts. However I can't seam to achieve the desired results. Could anyone offer some suggestions? The next three rows of the GridBagLayout will be laid out exactly the same way. Here is a link to a screen shot of the GUI. calibrationFileSelectionValueLabel = new JLabel("",Label.LEFT); calibrationFileSelectionValueLabel.setName("calibrationFileSelection"); calibrationFileSelectionValueLabel.setMinimumSize(new Dimension(100,0)); calibrationFileSelectionValuePanel = new JPanel(); calibrationFileSelectionValuePanel.setBorder(BorderFactory.createEtchedBorder()); calibrationFileSelectionValuePanel.add(calibrationFileSelectionValueLabel); c.gridx = 0; c.gridy = 0; c.fill = GridBagConstraints.NONE; filesPanel.add(calibrationFileLabel,c); c.gridy = 1; filesPanel.add(frequencyFileLabel,c); c.gridy = 2; filesPanel.add(sampleFileLabel,c); c.gridy = 3; filesPanel.add(outputFileLabel,c); c.gridx = 1; c.gridy = 0; c.fill = GridBagConstraints.BOTH; // filesPanel.add(calibrationFileSelection,c); filesPanel.add(calibrationFileSelectionValuePanel,c); c.gridy = 1; // filesPanel.add(frequencyFileSelection,c); filesPanel.add(frequencyFileSelectionValueLabel,c); c.gridy = 2; // filesPanel.add(sampleFileSelection,c); filesPanel.add(sampleFileSelectionValueLabel,c); c.gridy = 3; // filesPanel.add(outputFileSelection,c); filesPanel.add(outputFileSelectionValueLabel,c); c.gridx = 2; c.gridy = 0; c.fill = GridBagConstraints.NONE; filesPanel.add(calibrationFileSelectionButton,c); c.gridy = 1; filesPanel.add(frequencyFileSelectionButton,c); c.gridy = 2; filesPanel.add(sampleFileSelectionButton,c); c.gridy = 3; filesPanel.add(createOutputFileButton,c); panelForFilesPanelBorder = new JPanel(); panelForFilesPanelBorder.setBorder(BorderFactory.createCompoundBorder(new EmptyBorder(5,10,5,10), BorderFactory.createEtchedBorder())); panelForFilesPanelBorder.add(filesPanel); buttonsPanel = new JPanel(); buttonsPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); buttonsPanel.setBorder(BorderFactory.createCompoundBorder(new EmptyBorder(5,10,10,10), BorderFactory.createEtchedBorder())); buttonsPanel.add(startButton); buttonsPanel.add(stopButton); basePanel.add(panelForFilesPanelBorder); basePanel.add(numericInputPanel); basePanel.add(buttonsPanel);

    Read the article

  • Building a Monopoly Board with GridBagLayout

    - by Oetzi
    I have been building a Java version of Monopoly in my spare time and am having some trouble understanding layouts etc for Swing. Each one of my spaces on the board is a essentially a custom JButton and I have been trying to lay them around the edge of a frame (like on the monopoly board itself). I can't seem to find a useful explanation of how the layout system works and so am having trouble do so. Could someone please give me an example of how they would lay the buttons around the edge of the frame? Should I be using a different layout?

    Read the article

  • Java Layout Insets

    - by ikurtz
    board image the above is a board layout done by using gridbaglayout. i would like insets of 5 pixels around the whole board but not between each label. i scaned the api and havent come up with something that lets me do this. does this mean i have to nest my board gui inside another gridbaglayout with the insets desired? thank you.

    Read the article

  • Problem with setVisible (true)

    - by Jessy
    The two examples shown below are same. Both are supposed to produce same result e.g. generate the coordinates of images displayed on JPanel. Example 1, works perfectly (print the coordinates of images), however example 2 returning 0 for the coordinate. I was wondering why because, I have put the setvisible (true) after adding the panel, in both examples. The only difference is that example 1 used extends JPanel and example 2 extends JFrame EXAMPLE 1: public class Grid extends JPanel{ public static void main(String[] args){ JFrame jf=new JFrame(); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final Grid grid = new Grid(); jf.add(grid); jf.pack(); Component[] components = grid.getComponents(); for (Component component : components) { System.out.println("Coordinate: "+ component.getBounds()); } jf.setVisible(true); } } EXAMPLE 2: public class Grid extends JFrame { public Grid () { setLayout(new GridBagLayout()); GridBagLayout m = new GridBagLayout(); Container c = getContentPane(); c.setLayout (m); GridBagConstraints con = new GridBagConstraints(); //construct the JPanel pDraw = new JPanel(); ... m.setConstraints(pDraw, con); pDraw.add (new GetCoordinate ()); // call new class to generate the coordinate c.add(pDraw); pack(); setVisible(true); } public static void main(String[] args) { new Grid(); } }

    Read the article

  • GridBagConstraints problem-moved to left and size isn't the same

    - by Damir
    I have in Java two panels which need to have same layout, there is my functions for initializations panels. private void InitializePanelCom(){ pnlCom=new JPanel(); pnlCom.setSize(300,160); pnlCom.setLocation(10, 60); add(pnlCom); GridBagLayout gb=new GridBagLayout(); GridBagConstraints gc=new GridBagConstraints(); pnlCom.setLayout(gb); jLabelcommPort = setJLabel("Com Port : "); jLabelbaudRate = setJLabel("Baud Rate : "); jLabelplcAddress = setJLabel("Plc Address : "); jLabelsendTime = setJLabel("Send Time : "); jLabelx50 = setJLabel(" x 50 ms (2 - 99)"); jComboBoxcommPort = setJComboBox(commPortList); jComboBoxbaudRate = setJComboBox(bitRateList); jTextAreaPlcAddress = setJTextField(""); jTextAreaSendTime = setJTextField(""); gc.insets = new Insets(10,0,0,0); gc.ipadx = 120; gc.weightx = 1; gc.gridx = 0; gc.gridy = 0; gc.anchor=GridBagConstraints.EAST; pnlCom.add(jLabelcommPort,gc); gc.insets = new Insets(10,0,0,0); gc.ipadx = 120; gc.weightx = 1; gc.gridx = 1; gc.gridy = 0; gc.anchor=GridBagConstraints.EAST; pnlCom.add(jComboBoxcommPort,gc); gc.insets=new Insets(10,0,0,0); gc.ipadx=120; gc.weightx=1; gc.gridx=0; gc.gridy=1; gc.anchor=GridBagConstraints.EAST; pnlCom.add(jLabelbaudRate,gc); gc.insets=new Insets(10,0,0,0); gc.ipadx=120; gc.weightx=1; gc.gridx=1; gc.gridy=1; gc.anchor=GridBagConstraints.EAST; pnlCom.add(jComboBoxbaudRate,gc); gc.insets=new Insets(10,0,0,0); gc.ipadx=120; gc.weightx=1; gc.gridx=0; gc.gridy=2; gc.anchor=GridBagConstraints.EAST; pnlCom.add(jLabelplcAddress,gc); gc.insets=new Insets(10,0,0,0); gc.ipadx=120; gc.weightx=1; gc.gridx=1; gc.gridy=2; gc.anchor=GridBagConstraints.EAST; pnlCom.add(jTextAreaPlcAddress,gc); gc.insets=new Insets(10,0,0,0); gc.ipadx=120; gc.weightx=1; gc.gridx=0; gc.gridy=3; gc.anchor=GridBagConstraints.EAST; pnlCom.add(jLabelsendTime,gc); gc.insets=new Insets(10,0,0,0); gc.ipadx=120; gc.weightx=1; gc.gridx=1; gc.gridy=3; gc.anchor=GridBagConstraints.EAST; pnlCom.add(jTextAreaSendTime,gc); gc.insets=new Insets(10,0,0,0); gc.ipadx=120; gc.weightx=1; gc.gridx=2; gc.gridy=3; gc.anchor=GridBagConstraints.EAST; pnlCom.add(jLabelx50,gc); } ![alt text][1] private void InitializePanelTcp(){ pnlTcp=new JPanel(); pnlTcp.setSize(300,160); pnlTcp.setLocation(10, 60); add(pnlTcp); GridBagLayout gb=new GridBagLayout(); GridBagConstraints gc=new GridBagConstraints(); pnlTcp.setLayout(gb); lblIPAddress=setJLabel("IP Address : "); txtIPAddress=setJTextField(""); lblPort=setJLabel("Port : "); txtPort=setJTextField(""); cmbBaudRateTCP = setJComboBox(bitRateList); lblBaudRateTCP = setJLabel("Baud Rate : "); lblParityCheck=setJLabel("Parity check : "); txtParityCheck=setJTextField(""); gc.insets = new Insets(10,0,0,0); //gc.ipadx = 20; gc.weightx = 0.3; gc.gridx = 0; gc.gridy = 0; gc.anchor=GridBagConstraints.WEST; pnlTcp.add(lblIPAddress,gc); gc.insets = new Insets(10,0,0,0); //gc.ipadx = 80; gc.weightx = 0.7; gc.gridx = 1; gc.gridy = 0; gc.anchor=GridBagConstraints.WEST; pnlTcp.add(txtIPAddress,gc); gc.insets=new Insets(10,0,0,0); //gc.ipadx=120; gc.weightx=0.3; gc.gridx=0; gc.gridy=1; gc.anchor=GridBagConstraints.WEST; pnlTcp.add(lblPort,gc); gc.insets=new Insets(10,0,0,0); //gc.ipadx=80; gc.weightx=0.7; gc.gridx=1; gc.gridy=1; gc.anchor=GridBagConstraints.WEST; pnlTcp.add(txtPort,gc); gc.insets=new Insets(10,0,0,0); //gc.ipadx=120; gc.weightx=0.3; gc.gridx=0; gc.gridy=2; gc.anchor=GridBagConstraints.WEST; pnlTcp.add(lblBaudRateTCP,gc); gc.insets=new Insets(10,0,0,0); //gc.ipadx=0; gc.weightx=0.7; gc.gridx=1; gc.gridy=2; gc.anchor=GridBagConstraints.WEST; pnlTcp.add(cmbBaudRateTCP,gc); gc.insets=new Insets(10,0,0,0); //gc.ipadx=120; gc.weightx=0.3; gc.gridx=0; gc.gridy=3; gc.anchor=GridBagConstraints.WEST; pnlTcp.add(lblParityCheck,gc); gc.insets=new Insets(10,0,0,0); //gc.ipadx=0; gc.weightx=1.7; gc.gridx=1; gc.gridy=3; gc.anchor=GridBagConstraints.WEST; pnlTcp.add(txtParityCheck,gc); } Problem is that second panel (initializetcp, second picture doesn't look the same, labels are moved at left , it is different ). Can anybody help, I am new with GridBagContsraints at all ?

    Read the article

  • How to get image's coordinate on JPanel

    - by Jessy
    This question is related to my previous question http://stackoverflow.com/questions/2376027/how-to-generate-cartesian-coordinate-x-y-from-gridbaglayout I have successfully get the coordinate of each pictures, however when I checked the coordinate through (System.out.println) and the placement of the images on the screen, it seems to be wrong. e.g. if on the screen it was obvious that the x point of the first picture is on cell 2 which is on coordinate of 20, but the program shows x=1. Here is part of the code: public Grid (){ setPreferredSize(new Dimension(600,600)); .... setLayout(new GridBagLayout()); GridBagConstraints gc = new GridBagConstraints(); gc.weightx = 1d; gc.weighty = 1d; gc.insets = new Insets(0, 0, 0, 0);//top, left, bottom, and right gc.fill = GridBagConstraints.BOTH; JLabel[][] label = new JLabel[ROWS][COLS]; Random rand = new Random(); // fill the panel with labels for (int i=0;i<IMAGES;i++){ ImageIcon icon = createImageIcon("myPics.jpg"); int r, c; do{ //pick random cell which is empty r = (int)Math.floor(Math.random() * ROWS); c = (int)Math.floor(Math.random() * COLS); } while (label[r][c]!=null); //randomly scale the images int x = rand.nextInt(50)+30; int y = rand.nextInt(50)+30; Image image = icon.getImage().getScaledInstance(x,y, Image.SCALE_SMOOTH); icon.setImage(image); JLabel lbl = new JLabel(icon); // Instantiate GUI components gc.gridx = r; gc.gridy = c; add(lbl, gc); //add(component, constraintObj); label[r][c] = lbl; } I checked the coordinate through this code: Component[] components = getComponents(); for (Component component : components) { System.out.println(component.getBounds()); }

    Read the article

  • How to display image within cell boundary

    - by Jessy
    How can I place an image within the cell boundary? I mean without taking the space of other cell? In the code below, random cells were selected to display images. One image in one cell. The problem is that, the image seems to take other cells as well. ... setPreferredSize(new Dimension(600,600)); final int ROWS = 6; final int COLS = 6; final int IMAGES = 10; setLayout(new GridBagLayout()); GridBagConstraints gc = new GridBagConstraints(); gc.weightx = 1d; gc.weighty = 1d; gc.insets = new Insets(0, 0, 0, 0);//top, left, bottom, and right gc.fill = GridBagConstraints.NONE; JLabel[][] label = new JLabel[ROWS][COLS]; Random rand = new Random(); // fill the panel with labels for (int i=0;i<IMAGES;i++){ ImageIcon icon = createImageIcon("myImage.jpg"); int r, c; do{ //pick random cell which is empty to avoid overlap image in the same cell r = (int)Math.floor(Math.random() * ROWS); c = (int)Math.floor(Math.random() * COLS); } while (label[r][c]!=null); //scale the image int x = rand.nextInt(20)+30; int y = rand.nextInt(20)+30; Image image = icon.getImage().getScaledInstance(x,y, Image.SCALE_SMOOTH); icon.setImage(image); JLabel lbl = new JLabel(icon); gc.gridx = r; gc.gridy = c; add(lbl, gc); //add image to the cell label[r][c] = lbl; }

    Read the article

  • How to display image within cell boundary

    - by Jessy
    How can I place an image within the cell boundary? I mean without taking the space of other cell? In the code below, random cells were selected to display images. One image in one cell. The problem is that, the image seems to take other cells as well. ... setPreferredSize(new Dimension(600,600)); final int ROWS = 6; final int COLS = 6; final int IMAGES = 10; setLayout(new GridBagLayout()); GridBagConstraints gc = new GridBagConstraints(); gc.weightx = 1d; gc.weighty = 1d; gc.insets = new Insets(0, 0, 0, 0);//top, left, bottom, and right gc.fill = GridBagConstraints.NONE; JLabel[][] label = new JLabel[ROWS][COLS]; Random rand = new Random(); // fill the panel with labels for (int i=0;i<IMAGES;i++){ ImageIcon icon = createImageIcon("myImage.jpg"); int r, c; do{ //pick random cell which is empty to avoid overlap image in the same cell r = (int)Math.floor(Math.random() * ROWS); c = (int)Math.floor(Math.random() * COLS); } while (label[r][c]!=null); //scale the image int x = rand.nextInt(20)+30; int y = rand.nextInt(20)+30; Image image = icon.getImage().getScaledInstance(x,y, Image.SCALE_SMOOTH); icon.setImage(image); JLabel lbl = new JLabel(icon); gc.gridx = r; gc.gridy = c; add(lbl, gc); //add image to the cell label[r][c] = lbl; }

    Read the article

  • JButton Image Ignoring GridBagConstraints

    - by daemor
    I am working on an application and have a screen that needs to have elements (namely some custom JButtons) appear and disappear based on a user selection. However for some reason, when I add these buttons to their pane, the buttton image goes to the top corner, and leaves the text in the center, completely ignoring GridBagConstraints. I am completely stumped on this one as I have done this same exact thing dozens of times earlier in the program without any issues. Here is an image of the problem: The problem is in this method here, and occurs down towards the bottom. public void init(){ contentPane.removeAll(); // Setup jlabels JLabel countyLabel = new JLabel("County"); countyLabel.setFont(new Font("Times New Roman", Font.PLAIN, 18)); JLabel measureByLabel = new JLabel("Measure By: "); measureByLabel.setFont(new Font("Times New Roman", Font.PLAIN, 18)); String[] countyChoices = {"Washtenaw", "Oakland", "Livingston"}; // setup components JComboBox<String> countyCombo = new JComboBox<String>(countyChoices); // place baseComponents c.weightx = 0.5; c.weighty = 0.5; c.gridx = 0; c.gridy = 0; c.anchor = GridBagConstraints.NORTH; contentPane.add(countyLabel, c); c.gridx = 2; contentPane.add(countyCombo, c); c.gridy = 1; c.gridx = 0; contentPane.add(trenchButton, c); c.gridx = 2; contentPane.add(bedButton, c); c.gridy = 2; c.gridx = 1; contentPane.add(systemSelection, c); c.gridy = 3; c.gridx = 0; contentPane.add(lengthButton, c); c.fill = GridBagConstraints.BOTH; c.gridwidth = 4; c.gridy = 4; c.gridx = 0; contentPane.add(choicePane, c); GridBagConstraints con = new GridBagConstraints(); con.weightx = 0.5; con.weighty = 0.5; con.gridx = 0; con.gridy = 0; choicePane.add(lengthButton, c); // revalidate and repaint choicePane.revalidate(); choicePane.repaint(); contentPane.revalidate(); contentPane.repaint(); } I have tried doing this in separate methods, the button looks fine when added to the contentPane, the pane is for sure set to gridbagconstraints as I used the expression JPanel choicePane = new JPanel(new GridBagLayout()) to initialize it.

    Read the article

  • I create a JPanel and GridBagLayout within an object but when I get it in the main object, attributes are missing

    - by chickeneaterguy
    public oijoij() { String name = "Jackie"; int priority = 50; int minPriority = 90; setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new BorderLayout(0, 0)); setContentPane(contentPane); JPanel panel = new JPanel(); GridBagLayout gbc_panel = new GridBagLayout(); gbc_panel.columnWidths = new int[]{0,0,0}; gbc_panel.rowHeights = new int[]{0, 0, 0, 0, 0, 0}; gbc_panel.columnWeights = new double[]{0.0, 0.0, Double.MIN_VALUE}; gbc_panel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE}; panel.setBorder(new LineBorder(new Color(0,0,0),1)); panel.setLayout(gbc_panel); panel.setAlignmentX(Component.LEFT_ALIGNMENT); panel.setMinimumSize(new Dimension(110,110)); panel.setPreferredSize(new Dimension(110, 110)); panel.setSize(new Dimension(110,110)); JLabel lblNewLabel = new JLabel("Process ID:"); GridBagConstraints gbc_lblNewLabel = new GridBagConstraints(); gbc_lblNewLabel.gridheight = 2; gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5); gbc_lblNewLabel.gridx = 0; gbc_lblNewLabel.gridy = 0; panel.add(lblNewLabel, gbc_lblNewLabel); JLabel lblNewLabel_1 = new JLabel(name); GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints(); gbc_lblNewLabel_1.gridheight = 2; gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 0); gbc_lblNewLabel_1.gridx = 1; gbc_lblNewLabel_1.gridy = 0; panel.add(lblNewLabel_1, gbc_lblNewLabel_1); JLabel lblNewLabel_2 = new JLabel("Priority:"); GridBagConstraints gbc_lblNewLabel_2 = new GridBagConstraints(); gbc_lblNewLabel_2.insets = new Insets(0, 0, 5, 5); gbc_lblNewLabel_2.gridx = 0; gbc_lblNewLabel_2.gridy = 2; panel.add(lblNewLabel_2, gbc_lblNewLabel_2); JLabel lblNum = new JLabel(Integer.toString(priority)); GridBagConstraints gbc_lblNum = new GridBagConstraints(); gbc_lblNum.insets = new Insets(0, 0, 5, 0); gbc_lblNum.gridx = 1; gbc_lblNum.gridy = 2; panel.add(lblNum, gbc_lblNum); JLabel lblNewLabel_3 = new JLabel("Min Priority:"); GridBagConstraints gbc_lblNewLabel_3 = new GridBagConstraints(); gbc_lblNewLabel_3.insets = new Insets(0, 0, 5, 5); gbc_lblNewLabel_3.gridx = 0; gbc_lblNewLabel_3.gridy = 3; panel.add(lblNewLabel_3, gbc_lblNewLabel_3); JLabel lblMp = new JLabel(Integer.toString(minPriority)); GridBagConstraints gbc_lblMp = new GridBagConstraints(); gbc_lblMp.insets = new Insets(0, 0, 5, 0); gbc_lblMp.gridx = 1; gbc_lblMp.gridy = 3; panel.add(lblMp, gbc_lblMp); JLabel lblTimeSlice = new JLabel("Time Slice:"); GridBagConstraints gbc_lblTimeSlice = new GridBagConstraints(); gbc_lblTimeSlice.insets = new Insets(0, 0, 0, 5); gbc_lblTimeSlice.gridx = 0; gbc_lblTimeSlice.gridy = 4; panel.add(lblTimeSlice, gbc_lblTimeSlice); Random r = new Random(System.currentTimeMillis()); panel.setBackground(new Color( r.nextInt(255 - 210) + 210, r.nextInt(255 - 210) + 210, r.nextInt(255 - 210) + 210)); } I have accessor methods for the GridBagLayout and the JPanel. When calling the functions in another file, it looks like I just get the JPanel (but without any labels or the layout or other GridBagLayout features). Help?

    Read the article

  • Java JTextPane JScrollPane Display Issue

    - by ikurtz
    The following class implements a chatGUI. When it runs okay the screen looks like this: Fine ChatGUI The problem is very often when i enter text of large length ie. 50 - 100 chars the gui goes crazy. the chat history box shrinks as shown in this image. Any ideas regarding what is causing this? Thank you. package Sartre.Connect4; import javax.swing.*; import java.net.*; import java.awt.*; import java.awt.event.*; import javax.swing.text.StyledDocument; import javax.swing.text.Style; import javax.swing.text.StyleConstants; import javax.swing.text.BadLocationException; import java.io.BufferedOutputStream; import javax.swing.text.html.HTMLEditorKit; import java.io.FileOutputStream; import java.io.IOException; import java.io.FileNotFoundException; import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.JFileChooser; /** * Chat form class * @author iAmjad */ public class ChatGUI extends JDialog implements ActionListener { /** * Used to hold chat history data */ private JTextPane textPaneHistory = new JTextPane(); /** * provides scrolling to chat history pane */ private JScrollPane scrollPaneHistory = new JScrollPane(textPaneHistory); /** * used to input local message to chat history */ private JTextPane textPaneHome = new JTextPane(); /** * Provides scrolling to local chat pane */ private JScrollPane scrollPaneHomeText = new JScrollPane(textPaneHome); /** * JLabel acting as a statusbar */ private JLabel statusBar = new JLabel("Ready"); /** * Button to clear chat history pane */ private JButton JBClear = new JButton("Clear"); /** * Button to save chat history pane */ private JButton JBSave = new JButton("Save"); /** * Holds contentPane */ private Container containerPane; /** * Layout GridBagLayout manager */ private GridBagLayout gridBagLayout = new GridBagLayout(); /** * GridBagConstraints */ private GridBagConstraints constraints = new GridBagConstraints(); /** * Constructor for ChatGUI */ public ChatGUI(){ setTitle("Chat"); // set up dialog icon URL url = getClass().getResource("Resources/SartreIcon.jpg"); ImageIcon imageIcon = new ImageIcon(url); Image image = imageIcon.getImage(); this.setIconImage(image); this.setAlwaysOnTop(true); setLocationRelativeTo(this.getParent()); //////////////// End icon and placement ///////////////////////// // Get pane and set layout manager containerPane = getContentPane(); containerPane.setLayout(gridBagLayout); ///////////////////////////////////////////////////////////// //////////////// Begin Chat History ////////////////////////////// textPaneHistory.setToolTipText("Chat History Window"); textPaneHistory.setEditable(false); textPaneHistory.setPreferredSize(new Dimension(350,250)); scrollPaneHistory.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); scrollPaneHistory.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); // fill Chat History GridBagConstraints constraints.gridx = 0; constraints.gridy = 0; constraints.gridwidth = 10; constraints.gridheight = 10; constraints.weightx = 100; constraints.weighty = 100; constraints.fill = GridBagConstraints.BOTH; constraints.anchor = GridBagConstraints.CENTER; constraints.insets = new Insets(10,10,10,10); constraints.ipadx = 0; constraints.ipady = 0; gridBagLayout.setConstraints(scrollPaneHistory, constraints); // add to the pane containerPane.add(scrollPaneHistory); /////////////////////////////// End Chat History /////////////////////// ///////////////////////// Begin Home Chat ////////////////////////////// textPaneHome.setToolTipText("Home Chat Message Window"); textPaneHome.setPreferredSize(new Dimension(200,50)); textPaneHome.addKeyListener(new MyKeyAdapter()); scrollPaneHomeText.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); scrollPaneHomeText.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); // fill Chat History GridBagConstraints constraints.gridx = 0; constraints.gridy = 10; constraints.gridwidth = 6; constraints.gridheight = 1; constraints.weightx = 100; constraints.weighty = 100; constraints.fill = GridBagConstraints.BOTH; constraints.anchor = GridBagConstraints.CENTER; constraints.insets = new Insets(10,10,10,10); constraints.ipadx = 0; constraints.ipady = 0; gridBagLayout.setConstraints(scrollPaneHomeText, constraints); // add to the pane containerPane.add(scrollPaneHomeText); ////////////////////////// End Home Chat ///////////////////////// ///////////////////////Begin Clear Chat History //////////////////////// JBClear.setToolTipText("Clear Chat History"); // fill Chat History GridBagConstraints constraints.gridx = 6; constraints.gridy = 10; constraints.gridwidth = 2; constraints.gridheight = 1; constraints.weightx = 100; constraints.weighty = 100; constraints.fill = GridBagConstraints.BOTH; constraints.anchor = GridBagConstraints.CENTER; constraints.insets = new Insets(10,10,10,10); constraints.ipadx = 0; constraints.ipady = 0; gridBagLayout.setConstraints(JBClear, constraints); JBClear.addActionListener(this); // add to the pane containerPane.add(JBClear); ///////////////// End Clear Chat History //////////////////////// /////////////// Begin Save Chat History ////////////////////////// JBSave.setToolTipText("Save Chat History"); constraints.gridx = 8; constraints.gridy = 10; constraints.gridwidth = 2; constraints.gridheight = 1; constraints.weightx = 100; constraints.weighty = 100; constraints.fill = GridBagConstraints.BOTH; constraints.anchor = GridBagConstraints.CENTER; constraints.insets = new Insets(10,10,10,10); constraints.ipadx = 0; constraints.ipady = 0; gridBagLayout.setConstraints(JBSave, constraints); JBSave.addActionListener(this); // add to the pane containerPane.add(JBSave); ///////////////////// End Save Chat History ///////////////////// /////////////////// Begin Status Bar ///////////////////////////// constraints.gridx = 0; constraints.gridy = 11; constraints.gridwidth = 10; constraints.gridheight = 1; constraints.weightx = 100; constraints.weighty = 50; constraints.fill = GridBagConstraints.BOTH; constraints.anchor = GridBagConstraints.CENTER; constraints.insets = new Insets(0,10,5,0); constraints.ipadx = 0; constraints.ipady = 0; gridBagLayout.setConstraints(statusBar, constraints); // add to the pane containerPane.add(statusBar); ////////////// End Status Bar //////////////////////////// // set resizable to false this.setResizable(false); // pack the GUI pack(); } /** * Deals with necessary menu click events * @param event */ public void actionPerformed(ActionEvent event) { Object source = event.getSource(); // Process Clear button event if (source == JBClear){ textPaneHistory.setText(null); statusBar.setText("Chat History Cleared"); } // Process Save button event if (source == JBSave){ // process only if there is data in history pane if (textPaneHistory.getText().length() > 0){ // process location where to save the chat history file JFileChooser chooser = new JFileChooser(); chooser.setMultiSelectionEnabled(false); chooser.setAcceptAllFileFilterUsed(false); FileNameExtensionFilter filter = new FileNameExtensionFilter("HTML Documents", "htm", "html"); chooser.setFileFilter(filter); int option = chooser.showSaveDialog(ChatGUI.this); if (option == JFileChooser.APPROVE_OPTION) { // Set up document to be parsed as HTML StyledDocument doc = (StyledDocument)textPaneHistory.getDocument(); HTMLEditorKit kit = new HTMLEditorKit(); BufferedOutputStream out; try { // add final file name and extension String filePath = chooser.getSelectedFile().getAbsoluteFile() + ".html"; out = new BufferedOutputStream(new FileOutputStream(filePath)); // write out the HTML document kit.write(out, doc, doc.getStartPosition().getOffset(), doc.getLength()); } catch (FileNotFoundException e) { JOptionPane.showMessageDialog(ChatGUI.this, "Application will now close. \n A restart may cure the error!\n\n" + e.getMessage(), "Fatal Error", JOptionPane.WARNING_MESSAGE, null); System.exit(2); } catch (IOException e){ JOptionPane.showMessageDialog(ChatGUI.this, "Application will now close. \n A restart may cure the error!\n\n" + e.getMessage(), "Fatal Error", JOptionPane.WARNING_MESSAGE, null); System.exit(3); } catch (BadLocationException e){ JOptionPane.showMessageDialog(ChatGUI.this, "Application will now close. \n A restart may cure the error!\n\n" + e.getMessage(), "Fatal Error", JOptionPane.WARNING_MESSAGE, null); System.exit(4); } statusBar.setText("Chat History Saved"); } } } } /** * Process return key for sending the message */ private class MyKeyAdapter extends KeyAdapter { @Override @SuppressWarnings("static-access") public void keyPressed(KeyEvent ke) { DateTime dateTime = new DateTime(); String nowdateTime = dateTime.getDateTime(); int kc = ke.getKeyCode(); if (kc == ke.VK_ENTER) { try { // Process only if there is data if (textPaneHome.getText().length() > 0){ // Add message origin formatting StyledDocument doc = (StyledDocument)textPaneHistory.getDocument(); Style style = doc.addStyle("HomeStyle", null); StyleConstants.setBold(style, true); String home = "Home [" + nowdateTime + "]: "; doc.insertString(doc.getLength(), home, style); StyleConstants.setBold(style, false); doc.insertString(doc.getLength(), textPaneHome.getText() + "\n", style); // update caret location textPaneHistory.setCaretPosition(doc.getLength()); textPaneHome.setText(null); statusBar.setText("Message Sent"); } } catch (BadLocationException e) { JOptionPane.showMessageDialog(ChatGUI.this, "Application will now close. \n A restart may cure the error!\n\n" + e.getMessage(), "Fatal Error", JOptionPane.WARNING_MESSAGE, null); System.exit(1); } ke.consume(); } } } }

    Read the article

  • How can I update a Jtextarea once? (mysql side-?)

    - by user1294196
    Ok what I've been trying to do is figure out how to make it so when I press the search button on my program the code that is currently just being printed to the console will print to the text area I have. I can't figure out how to do this and I've searched google and still found no answer. And while I'm at it if anyone could help me figure out how to send this same line of information to a mysql database that would help greatly. package GTE; import java.awt.EventQueue; public class GTE { private JFrame frmGte; public String hashq = "..."; public String twtresults; public int refresh = 1; public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { try { GTE window = new GTE(); window.frmGte.setVisible(true); } catch (Exception e) {} } }); } /** * Create the application. * @throws IOException * @throws FontFormatException */ public GTE(){ try { initialize(); } catch (FontFormatException e) {} catch (IOException e) {} } /** * Initialize the contents of the frame. * @throws IOException * @throws FontFormatException */ private void initialize() throws FontFormatException, IOException { frmGte = new JFrame(); frmGte.setResizable(false); frmGte.setTitle("GTE"); frmGte.setBounds(100, 100, 450, 390); frmGte.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GridBagLayout gridBagLayout = new GridBagLayout(); gridBagLayout.columnWidths = new int[]{434, 0}; gridBagLayout.rowHeights = new int[]{21, 0, 0, 0, 0, 0, 0, 0, 0}; gridBagLayout.columnWeights = new double[]{0.0, Double.MIN_VALUE}; gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE}; frmGte.getContentPane().setLayout(gridBagLayout); JLabel GTETitle = new JLabel("Personal Tweet Extractor"); InputStream is = this.getClass().getResourceAsStream("ultraviolentbb_reg.ttf"); Font GTEFont = Font.createFont(Font.TRUETYPE_FONT,is); Font f = GTEFont.deriveFont(24f); GTETitle.setFont(f); GTETitle.setHorizontalAlignment(SwingConstants.CENTER); GridBagConstraints gbc_GTETitle = new GridBagConstraints(); gbc_GTETitle.insets = new Insets(0, 0, 5, 0); gbc_GTETitle.anchor = GridBagConstraints.NORTH; gbc_GTETitle.fill = GridBagConstraints.HORIZONTAL; gbc_GTETitle.gridx = 0; gbc_GTETitle.gridy = 0; frmGte.getContentPane().add(GTETitle, gbc_GTETitle); Label label_2 = new Label("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); GridBagConstraints gbc_label_2 = new GridBagConstraints(); gbc_label_2.insets = new Insets(0, 0, 5, 0); gbc_label_2.gridx = 0; gbc_label_2.gridy = 1; frmGte.getContentPane().add(label_2, gbc_label_2); JLabel SearchTweets = new JLabel("Search For Tweets With" + hashq + ":"); GridBagConstraints gbc_SearchTweets = new GridBagConstraints(); gbc_SearchTweets.insets = new Insets(0, 0, 5, 0); gbc_SearchTweets.gridx = 0; gbc_SearchTweets.gridy = 2; frmGte.getContentPane().add(SearchTweets, gbc_SearchTweets); JLabel label = new JLabel("#"); GridBagConstraints gbc_label = new GridBagConstraints(); gbc_label.insets = new Insets(0, 0, 5, 0); gbc_label.gridx = 0; gbc_label.gridy = 3; frmGte.getContentPane().add(label, gbc_label); JButton Search = new JButton("Start Search"); Search.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { TS(hashq); GTE.this.refresh = 0; try { nulll dialog = new nulll(); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); dialog.setVisible(true); } catch (Exception e) {} } public void TS(String hashtag){ Twitter twitter = new TwitterFactory().getInstance(); try { System.out.println(hashtag); QueryResult result = twitter.search(new Query("#" + hashtag)); List<Tweet> tweets = result.getTweets(); for (Tweet tweet : tweets) { System.out.println("@" + tweet.getFromUser() + " : " + tweet.getText()); GTE.this.twtresults = ("@" + tweet.getFromUser() + " : " + tweet.getText()); } } catch (TwitterException te) { te.printStackTrace(); System.out.println("Failed to search tweets: " + te.getMessage()); System.exit(-1); } } }); TextField textField = new TextField(); textField.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { GTE.this.hashq = evt.getActionCommand(); } }); GridBagConstraints gbc_textField = new GridBagConstraints(); gbc_textField.ipadx = 99; gbc_textField.insets = new Insets(0, 0, 5, 0); gbc_textField.gridx = 0; gbc_textField.gridy = 4; frmGte.getContentPane().add(textField, gbc_textField); GridBagConstraints gbc_Search = new GridBagConstraints(); gbc_Search.insets = new Insets(0, 0, 5, 0); gbc_Search.gridx = 0; gbc_Search.gridy = 5; frmGte.getContentPane().add(Search, gbc_Search); Label label_1 = new Label("Search Results For Tweets With"); GridBagConstraints gbc_label_1 = new GridBagConstraints(); gbc_label_1.insets = new Insets(0, 0, 5, 0); gbc_label_1.gridx = 0; gbc_label_1.gridy = 6; frmGte.getContentPane().add(label_1, gbc_label_1); TextArea textArea = new TextArea(); textArea.setText(twtresults); textArea.setEditable(false); GridBagConstraints gbc_textArea = new GridBagConstraints(); gbc_textArea.gridx = 0; gbc_textArea.gridy = 7; frmGte.getContentPane().add(textArea, gbc_textArea); JMenuBar menuBar = new JMenuBar(); frmGte.setJMenuBar(menuBar); JMenu Filemenu = new JMenu("File"); menuBar.add(Filemenu); JMenuItem Exititem = new JMenuItem("Exit"); Exititem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { System.exit(0); } }); Filemenu.add(Exititem); JMenu Helpmenu = new JMenu("Help"); menuBar.add(Helpmenu); JMenuItem Aboutitem = new JMenuItem("About"); Helpmenu.add(Aboutitem); } }

    Read the article

  • How do I get my character to move after adding to JFrame?

    - by A.K.
    So this is kind of a follow up on my other JPanel question that got resolved by playing around with the Layout... Now my MouseListener allows me to add a new Board(); object from its class, which is the actual game map and animator itself. But since my Board() takes Key Events from a Player Object inside the Board Class, I'm not sure if they are being started. Here's my Frame Class, where SideScroller S is the player object: package OurPackage; //Made By A.K. 5/24/12 //Contains Frame. import java.awt.BorderLayout; import java.awt.Button; import java.awt.CardLayout; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GridBagLayout; import java.awt.GridLayout; import java.awt.Image; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.*; import javax.swing.plaf.basic.BasicOptionPaneUI.ButtonActionListener; public class Frame implements MouseListener { public static boolean StartGame = false; JFrame frm = new JFrame("Action-Packed Jack"); ImageIcon img = new ImageIcon(getClass().getResource("/Images/ActionJackTitle.png")); ImageIcon StartImg = new ImageIcon(getClass().getResource("/Images/JackStart.png")); public Image Title; JLabel TitleL = new JLabel(img); public JPanel TitlePane = new JPanel(); public JPanel BoardPane = new JPanel(); JPanel cards; JButton StartB = new JButton(StartImg); Board nBoard = new Board(); static Sound nSound; public Frame() { frm.setLayout(new GridBagLayout()); cards = new JPanel(new CardLayout()); nSound = new Sound("/Sounds/BunchaJazz.wav"); TitleL.setPreferredSize(new Dimension(970, 420)); frm.add(TitleL); frm.add(cards); cards.setSize(new Dimension(150, 45)); cards.setLayout(new GridBagLayout ()); cards.add(StartB); StartB.addMouseListener(this); StartB.setPreferredSize(new Dimension(150, 45)); frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frm.setSize(1200, 420); frm.setVisible(true); frm.setResizable(false); frm.setLocationRelativeTo(null); frm.pack(); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new Frame(); } }); } public void mouseClicked(MouseEvent e) { nSound.play(); StartB.setContentAreaFilled(false); cards.remove(StartB); frm.remove(TitleL); frm.remove(cards); frm.setLayout(new GridLayout(1, 1)); frm.add(nBoard); //Add Game "Tiles" Or Content. x = 1200 nBoard.setPreferredSize(new Dimension(1200, 420)); cards.revalidate(); frm.validate(); } @Override public void mouseEntered(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent arg0) { // TODO Auto-generated method stub } }

    Read the article

  • Can't remove JPanel from JFrame while adding new class into it

    - by A.K.
    Basically, I have my Frame class, which instantiates all the properties for the JFrame, and draws a JLabel with an image (my title screen). Then I made a separate JPanel with a start button on it, and made a mouse listener that will allow me to remove these objects while adding in a new Board() class (Which paints the main game). *Note: The JLabel is SEPARATE from the JPanel, but it still gets moved to the side by it. Problem: Whenever I click the button though, it only shows a little square of what I presume is my board class trying to run. Code below for the Frame Class: package OurPackage; //Made By A.K. 5/24/12 //Contains Frame. import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GridBagLayout; import java.awt.GridLayout; import java.awt.Image; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.*; import javax.swing.plaf.basic.BasicOptionPaneUI.ButtonActionListener; public class Frame implements MouseListener { public static boolean StartGame = false; ImageIcon img = new ImageIcon(getClass().getResource("/Images/ActionJackTitle.png")); ImageIcon StartImg = new ImageIcon(getClass().getResource("/Images/JackStart.png")); public Image Title; JLabel TitleL = new JLabel(img); public JPanel panel = new JPanel(); JButton StartB = new JButton(StartImg); JFrame frm = new JFrame("Action-Packed Jack"); public Frame() { TitleL.setPreferredSize(new Dimension(1200, 420)); frm.add(TitleL); frm.setLayout(new GridBagLayout()); frm.add(panel); panel.setSize(new Dimension(220, 45)); panel.setLayout(new GridBagLayout ()); panel.add(StartB); StartB.addMouseListener(this); StartB.setPreferredSize(new Dimension(220, 45)); frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frm.setSize(1200, 420); frm.setVisible(true); frm.setResizable(false); frm.setLocationRelativeTo(null); } public static void main(String[] args) { new Frame(); } public void mouseClicked(MouseEvent e) { StartB.setContentAreaFilled(false); panel.remove(StartB); frm.remove(panel); frm.remove(TitleL); //frm.setLayout(null); frm.add(new Board()); //Add Game "Tiles" Or Content. x = 1200 frm.validate(); System.out.println("Hit!"); } @Override public void mouseEntered(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent arg0) { // TODO Auto-generated method stub } }

    Read the article

  • Does anyone know how to layout a JToolBar that does't move or re-size any components placed in it?

    - by S1.Mac
    Can anyone help with this problem i'm trying to create a JToolBar and I want all its components to be fixed in size and position. I'v tried a few different layout managers but they all center and/or re-size the components when the frame its in is re-sized. here is an example using GridbagLayout, I have also used the default layout manager using the toolbar.add( component ) method but the result is the same : ' import java.awt.BorderLayout; import java.awt.Component; import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import javax.swing.*; public class ToolBarTest extends JFrame { private JToolBar toolbar; private JPanel mainPanel; private JPanel toolBarPanel; private JButton aButton; private JCheckBox aCheckBox; private JList aList; private Box toolbarBox; private GridBagConstraints toolbarConstraints; private GridBagLayout toolbarLayout; private JLabel shapeLabel; private JComboBox<ImageIcon> shapeChooser; private JLabel colorLabel; private JComboBox colorChooser; private String colorNames[] = { "Black" , "Blue", "Cyan", "Dark Gray", "Gray", "Green", "Light Gray", "Magenta", "Orange", "Pink", "Red", "White", "Yellow", "Custom" }; private String shapeNames[] = { "Line", "Oval", "Rectangle", "3D Rectangle","Paint Brush", "Rounded Rectangle" }; public ToolBarTest() { setLayout( new BorderLayout() ); setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); setSize( 500, 500 ); add( createToolBar(), BorderLayout.PAGE_START ); setVisible( true ); } public void addToToolbar( Component component, int row, int column ) { toolbarConstraints.gridx = column; toolbarConstraints.gridy = row; toolbarConstraints.anchor = GridBagConstraints.WEST; toolbarConstraints.fill = GridBagConstraints.NONE; toolbarConstraints.weightx = 0; toolbarConstraints.weighty = 0; toolbarConstraints.gridwidth = 1; toolbarConstraints.gridheight = 1; toolbarLayout.setConstraints( component, toolbarConstraints ); toolbar.add( component ); }// end addToToolbar public final JToolBar createToolBar() { toolbarLayout = new GridBagLayout(); toolbarConstraints = new GridBagConstraints(); // create the tool bar which holds the items to draw toolbar = new JToolBar(); toolbar.setBorderPainted(true); toolbar.setLayout( toolbarLayout ); toolbar.setFloatable( true ); shapeLabel = new JLabel( "Shapes: " ); addToToolbar( shapeLabel, 0, 1 ); String iconNames[] = { "PaintImages/Line.jpg", "PaintImages/Oval.jpg", "PaintImages/Rect.jpg", "PaintImages/3DRect.jpg","PaintImages/PaintBrush.jpg", "PaintImages/RoundRect.jpg"}; ImageIcon shapeIcons[] = new ImageIcon[ shapeNames.length ]; // create image icons for( int shapeButton = 0; shapeButton < shapeNames.length; shapeButton++ ) { shapeIcons[ shapeButton ] = new ImageIcon( iconNames[ shapeButton ] ); }// end for shapeChooser = new JComboBox< ImageIcon >( shapeIcons ); shapeChooser.setSize( new Dimension( 50, 20 )); shapeChooser.setPrototypeDisplayValue( shapeIcons[ 0 ] ); shapeChooser.setSelectedIndex( 0 ); addToToolbar( shapeChooser, 0, 2 ); colorLabel = new JLabel( "Colors: " ); addToToolbar( colorLabel, 0, 3 ); colorChooser = new JComboBox( colorNames ); addToToolbar( colorChooser, 0, 4 ); return toolbar; }// end createToolBar public static void main( String args[] ) { new ToolBarTest(); }// end main }// end class ToolBarTest'

    Read the article

  • How do I ensure that a JPanel Shrinks when the parent frame is resized?

    - by dah
    I have a basic notes panel that I'm looking to shrink the width of when the parent jframe is resized but it isn't happening. I'm using nested gridbaglayouts. package com.protocase.notes.views; import com.protocase.notes.controller.NotesController; import com.protocase.notes.model.Subject; import com.protocase.notes.model.Note; import com.protocase.notes.model.database.PMSNotesAdapter; import java.awt.Color; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; /** * @author DavidH */ public class NotesViewer extends JPanel { // <editor-fold defaultstate="collapsed" desc="Attributes"> private Subject subject; private NotesController controller; //</editor-fold> // <editor-fold defaultstate="collapsed" desc="Getters N' Setters"> /** * Gets back the current subject. * @return */ public Subject getSubject() { return subject; } public NotesController getController() { return controller; } public void setController(NotesController controller) { this.controller = controller; } /** * Should clear the panel of the current subject and load the details for * the other object. * @param subject */ public void setSubject(Subject subject) { this.subject = subject; } //</editor-fold> // <editor-fold defaultstate="collapsed" desc="Constructor"> /** * -- Sets up a note viewer with a subject and a controller. Likely this * would be the constructor used if you were passing off from another * NoteViewer or something else that used a notes adapter or controller. * @param subject * @param controller */ public NotesViewer(Subject subject, NotesController controller) { this.subject = subject; this.controller = controller; initComponents(); } /** * -- Sets up a note view with a subject and creates a new controller. This * would be the constructor typically chosen if choosing notes was * infrequent and only one or two notes needs to be displayed. * @param subject */ public NotesViewer(Subject subject) { this(subject, new NotesController(new PMSNotesAdapter())); } /** * -- Sets up a note view without a subject and creates a new controller. * This would be for a note viewer without any notes, perhaps populating * as you choose values in another form. * @param subject */ public NotesViewer() { this(null); } //</editor-fold> // <editor-fold defaultstate="collapsed" desc="initComponents()"> /** * Sets up the view for the NotesViewer */ private void initComponents() { // -- Make a new panel for the header JPanel panel = new JPanel(); panel.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); c.gridx = 0; c.fill = GridBagConstraints.HORIZONTAL; c.gridy = 0; c.weightx = .5; //c.anchor = GridBagConstraints.NORTHWEST; JLabel label = new JLabel("Viewing Notes for [Subject]"); label.setAlignmentX(JLabel.LEFT_ALIGNMENT); label.setBorder(BorderFactory.createLineBorder(Color.YELLOW)); panel.add(label); JButton newNoteButton = new JButton("New"); c = new GridBagConstraints(); // c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 1; c.gridy = 0; c.weightx = .5; c.anchor = GridBagConstraints.EAST; panel.add(newNoteButton, c); // -- NotePanels c = new GridBagConstraints(); c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 1; c.weighty = 1; c.gridx = 0; c.gridwidth = 2; int y = 1; for (Note n : subject.getNotes()) { c.gridy = y++; panel.add(new NotesPanel(n, controller), c); } this.setLayout(new GridBagLayout()); GridBagConstraints pc = new GridBagConstraints(); pc.gridx = 0; pc.gridy = 0; pc.weightx = 1; pc.weighty = 1; pc.fill = GridBagConstraints.BOTH; panel.setBackground(Color.blue); JScrollPane scroll = new JScrollPane(); scroll.setViewportView(panel); //scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); this.add(scroll, pc); //this.add(panel, pc); // -- Add it all to the layout } //</editor-fold> // <editor-fold defaultstate="collapsed" desc="private methods"> //</editor-fold> } package com.protocase.notes.views; import com.protocase.notes.controller.NotesController; import com.protocase.notes.model.Note; import java.awt.CardLayout; import java.awt.Color; import java.awt.Component; import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.DateFormat; import java.text.SimpleDateFormat; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.border.BevelBorder; import javax.swing.border.Border; import javax.swing.border.MatteBorder; /** * @author dah01 */ public class NotesPanel extends JPanel { // <editor-fold defaultstate="collapsed" desc="Attributes"> private Note note; private NotesController controller; private CardLayout cardLayout; private JTextArea viewTextArea; private JTextArea editTextArea; //</editor-fold> // <editor-fold defaultstate="collapsed" desc="Getters N' Setters"> public NotesController getController() { return controller; } public void setController(NotesController controller) { this.controller = controller; } public Note getNote() { return note; } public void setNote(Note note) { this.note = note; } //</editor-fold> // <editor-fold defaultstate="collapsed" desc="Constructor"> /** * Sets up a note panel that shows everything about the note. * @param note */ public NotesPanel(Note note, NotesController controller) { this.note = note; cardLayout = new CardLayout(); this.setLayout(cardLayout); // -- Setup the layout manager. this.setBackground(new Color(199, 187, 192)); this.setBorder(new BevelBorder(BevelBorder.RAISED)); // -- ViewPanel this.add("ViewPanel", initViewPanel()); this.add("EditPanel", initEditPanel()); } //</editor-fold> // <editor-fold defaultstate="collapsed" desc="EditPanel"> private JPanel initEditPanel() { JPanel editPanel = new JPanel(); editPanel.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.HORIZONTAL; c.gridy = 0; c.weightx = 1; c.weighty = 0.3; editPanel.add(initCreatorLabel(), c); c.gridy++; editPanel.add(initEditTextScroll(), c); c.gridy++; c.anchor = GridBagConstraints.WEST; c.fill = GridBagConstraints.NONE; editPanel.add(initEditorLabel(), c); c.gridx++; c.anchor = GridBagConstraints.EAST; editPanel.add(initSaveButton(), c); return editPanel; } private JScrollPane initEditTextScroll() { this.editTextArea = new JTextArea(note.getContents()); editTextArea.setLineWrap(true); editTextArea.setWrapStyleWord(true); JScrollPane scrollPane = new JScrollPane(editTextArea); scrollPane.setAlignmentX(JScrollPane.LEFT_ALIGNMENT); Border b = scrollPane.getViewportBorder(); MatteBorder mb = BorderFactory.createMatteBorder(2, 2, 2, 2, Color.BLUE); scrollPane.setBorder(mb); return scrollPane; } private JButton initSaveButton() { final CardLayout l = this.cardLayout; final JPanel p = this; final NotesController c = this.controller; final Note n = this.note; final JTextArea noteText = this.viewTextArea; final JTextArea textToSubmit = this.editTextArea; ActionListener al = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //controller.saveNote(n); noteText.setText(textToSubmit.getText()); l.next(p); } }; JButton saveButton = new JButton("Save"); saveButton.addActionListener(al); saveButton.setPreferredSize(new Dimension(62, 26)); return saveButton; } //</editor-fold> // <editor-fold defaultstate="collapsed" desc="ViewPanel"> private JPanel initViewPanel() { JPanel viewPanel = new JPanel(); viewPanel.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.HORIZONTAL ; c.gridy = 0; c.weightx = 1; c.weighty = 0.3; viewPanel.add(initCreatorLabel(), c); c.gridy++; viewPanel.add(this.initNoteTextArea(), c); c.fill = GridBagConstraints.NONE; c.anchor = GridBagConstraints.WEST; c.gridy++; viewPanel.add(initEditorLabel(), c); c.gridx++; c.anchor = GridBagConstraints.EAST; viewPanel.add(initEditButton(), c); return viewPanel; } private JLabel initCreatorLabel() { DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); if (note != null) { String noteBy = "Note by " + note.getCreator(); String noteCreated = formatter.format(note.getDateCreated()); JLabel creatorLabel = new JLabel(noteBy + " @ " + noteCreated); creatorLabel.setAlignmentX(JLabel.LEFT_ALIGNMENT); return creatorLabel; } else { System.out.println("NOTE IS NULL"); return null; } } private JScrollPane initNoteTextArea() { // -- Setup the notes area. this.viewTextArea = new JTextArea(note.getContents()); viewTextArea.setEditable(false); viewTextArea.setLineWrap(true); viewTextArea.setWrapStyleWord(true); JScrollPane scrollPane = new JScrollPane(viewTextArea); scrollPane.setAlignmentX(JScrollPane.LEFT_ALIGNMENT); return scrollPane; } private JLabel initEditorLabel() { // -- Setup the edited by label. JLabel editorLabel = new JLabel(" -- Last edited by " + note.getLastEdited() + " at " + note.getDateModified()); editorLabel.setAlignmentX(Component.LEFT_ALIGNMENT); return editorLabel; } private JButton initEditButton() { final CardLayout l = this.cardLayout; final JPanel p = this; ActionListener ar = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { l.next(p); } }; JButton editButton = new JButton("Edit"); editButton.setPreferredSize(new Dimension(62,26)); editButton.addActionListener(ar); return editButton; } //</editor-fold> // <editor-fold defaultstate="collapsed" desc="Grow Width When Resized"> @Override public Dimension getPreferredSize() { int fw = this.getParent().getSize().width; int fh = super.getPreferredSize().height; return new Dimension(fw,fh); } //</editor-fold> }

    Read the article

  • AWT Textfield behaves weird with MicroSoft JVM

    - by AKh
    Hi, I am facing a weird problem when using MicroSoft JVM to run my Applet. I have an AWT panel with 4 textfields which is added to a dialog box. Everything goes fine until I enter a decimal value into the textfield and close the dialog box. When i reopen the dialog box the textfield inside the panel with all the decimal digits (entered in the previous step) behaves weird. The decimal values along with the WHITE area inside the textfield moves to the left and hides the digits. When I click inside the textfield it becomes normal. The Panel earlier had gridlayout and I even tried changing it to gridbaylayout and still the problem persist. NOTE: All Development are pertained to JRE1.1 to compatibility with MS JVM If any can help me with this it would be a great help. Thanks in advance. . . . . public MyPanel(Dialog myDialog) { Panel panel = new Panel(); this.dialog = myDialog; //Previous code with grid layout /* panel.setLayout(new GridLayout2(4,2,2,2)); panel.add(new Label("Symbol:")); panel.add(symbolField = new TextField("",20)); panel.add(new Label("Quantity:")); panel.add( qtyField = new TextField()); panel.add(new Label("Price per Share:")); panel.add( costField = new TextField()); panel.add(new Label("Date Acquired:")); panel.add( purchaseDate = new TextField() );*/ GridBagLayout gridbag = new GridBagLayout(); System.out.println("######## Created New GridBagLayout"); GridBagConstraints constraints = new GridBagConstraints(); panel.setLayout( gridbag ); constraints = buildConstraints( constraints, 0, 0, 1, 1, 1.5, 1 ); constraints.anchor = GridBagConstraints.WEST; constraints.fill = GridBagConstraints.HORIZONTAL; panel.add( new Label("Symbol:"), constraints); constraints = buildConstraints( constraints, 1, 0, 1, 1, 1.5, 1 ); constraints.anchor = GridBagConstraints.WEST; constraints.fill = GridBagConstraints.HORIZONTAL; panel.add( symbolField = new TextField("",20), constraints); constraints = buildConstraints( constraints, 0, 1, 1, 1, 1.5, 1 ); constraints.anchor = GridBagConstraints.WEST; constraints.fill = GridBagConstraints.HORIZONTAL; panel.add( new Label("Quantity:"), constraints); constraints = buildConstraints( constraints, 1, 1, 1, 1, 1.5, 1 ); constraints.anchor = GridBagConstraints.WEST; constraints.fill = GridBagConstraints.HORIZONTAL; panel.add( qtyField = new TextField(), constraints); constraints = buildConstraints( constraints, 0, 2, 1, 1, 1.5, 1 ); constraints.anchor = GridBagConstraints.WEST; constraints.fill = GridBagConstraints.HORIZONTAL; panel.add( new Label("Price per Share:"), constraints); constraints = buildConstraints( constraints, 1, 2, 1, 1, 1.5, 1 ); constraints.anchor = GridBagConstraints.WEST; constraints.fill = GridBagConstraints.HORIZONTAL; panel.add( costField = new TextField(), constraints); constraints = buildConstraints( constraints, 0, 3, 1, 1, 1.5, 1 ); constraints.anchor = GridBagConstraints.WEST; constraints.fill = GridBagConstraints.HORIZONTAL; panel.add( new Label("Date Acquired:"), constraints); constraints = buildConstraints( constraints, 1, 3, 1, 1, 1.5, 1 ); constraints.anchor = GridBagConstraints.WEST; constraints.fill = GridBagConstraints.HORIZONTAL; panel.add( purchaseDate = new TextField(), constraints); .............. ......... }

    Read the article

  • Java, two JPanel on JFrame - Settings JPanel, StartMenu JPanel [on hold]

    - by Andy Tyurin
    There is my first question and I welcome community! I'm making a simple game and have some problems with Start menu. I have three buttons on my JPanel StartMenu and when I click "Settings" button, new JPanel will be open, but I don't know why buttons from StartMenu JPanel appeared in my Settings JPanel. My "Settings" JPanel has one ugly button "Back" in center and ugly grey background. I made some screens to see a problem. Start Menu JPanel when game launched Settings JPanel when button clicked Settings JPanel when mouse was over settings window There is code of StartMenu class: public class StartMenu extends JPanel { private GameButton startGameButton = new GameButton("Start game"); private GameButton settingsGameButton = new GameButton("Settings"); private GameButton exitGameButton = new GameButton("Exit game"); private Image bgImage = new ImageIcon(getClass().getClassLoader().getResource("ru/andydevs/astraLaserForce/bg.png")).getImage(); private int posX; private int posY; final private int WIDTH=(int)Game.SCREEN_DIMENSION.getWidth()/3; final private int HEIGHT=(int)Game.SCREEN_DIMENSION.getHeight()/2; public StartMenu() { setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); setSize(new Dimension(WIDTH, HEIGHT)); posX=(int)Game.SCREEN_DIMENSION.getWidth()/2-WIDTH/2; posY=(int)Game.SCREEN_DIMENSION.getHeight()/2-HEIGHT/2; setBounds(posX, posY,WIDTH,HEIGHT); c.ipadx=95; c.ipady=15; c.fill = GridBagConstraints.HORIZONTAL; c.insets = new Insets(20,0,0,0); c.gridy=0; add(startGameButton, c); c.gridy=1; c.insets = new Insets(20,0,0,0); System.out.println(settingsGameButton.getWidth()); add(settingsGameButton, c); c.gridy=2; c.insets = new Insets(20,0,0,0); add(exitGameButton, c); settingsGameButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { GameOptionsPanel gop = new GameOptionsPanel(); Game.container.add(gop); Game.container.setComponentZOrder(gop, 0); Game.container.revalidate(); Game.container.repaint(); } }); exitGameButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Main.currentGame.stop(); } }); } public void paintComponent(Graphics g) { g.drawImage(bgImage,0,0,WIDTH,HEIGHT,null); } } There is code of Settings JPanel public class GameOptionsPanel extends GamePanel { private GameButton backButton = new GameButton("Back"); private GameOptionsPanel that; public GameOptionsPanel() { super((int) (Game.SCREEN_DIMENSION.getWidth()/3), (int) (Game.SCREEN_DIMENSION.getHeight()/2), new Color(50,50,50)); that=this; setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.fill=gbc.HORIZONTAL; add(backButton); backButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Game.container.remove(that); Game.container.revalidate(); Game.container.repaint(); } }); } } I glad to see some suggestions. Thanks.

    Read the article

  • How to get the coordinate of Gridlayout

    - by Jessy
    Hi, I set my JPanel to GridLayout (6,6), with dimension (600,600) Each cell of the grid will display one pictures with different widths and heights. The picture first add to a JLabel, and the JLabel then added to the cells. How can retrieved the coordinate of the pictures in the cells and not the coordinate of cells? So far the out give these coordinate which equal height and width even on screen the pictures showed in different sizes. e.g. java.awt.Rectangle[x=100,y=100,width=100,height=100] java.awt.Rectangle[x=200,y=100,width=100,height=100] java.awt.Rectangle[x=300,y=100,width=100,height=100] The reason why I used GridLayout instead of gridBagLayout is that, I want each pictures to have boundary. If I use GridBagLayout, the grid will expand according to the picture size. I want grid size to be in fix size. JPanel pDraw = new JPanel(new GridLayout(6,6)); pDraw.setPreferredSize(new Dimension(600,600)); for (int i =0; i<(6*6); i++) { //get random number for height and width of the image int x = rand.nextInt(40)+(50); int y = rand.nextInt(40)+(50); ImageIcon icon = createImageIcon("bird.jpg"); //rescale the image according to the size selected Image img = icon.getImage().getScaledInstance(x,y,img.SCALE_SMOOTH); icon.setImage(img ); JLabel label = new JLabel(icon); pDraw.add(label); } for(Component component:components) { //retrieve the coordinate System.out.println(component.getBounds()); }

    Read the article

  • How to solve JAVA menubar, layout and panel problem ?

    - by Berkay
    i'm not a java guy however i start implementing some security tools with java, in these days i'm creating a gui for my security tools : Here is the how my Gui looks: menuBar = new JMenuBar(); // construct menu bar // hash functions fileMenu = new JMenu("HASH FUNCTIONS"); // define file menu fileMenu.setMnemonic('H'); // shortcut hashes = new JMenuItem("Md5&Sha1"); // define file menu options hashes.setMnemonic('M'); hashes.addActionListener(this); fileMenu.add(hashes); menuBar.add(fileMenu); // add file menu to menu bar // symmetric encryption asMenu = new JMenu("SYMMETRIC ENCRYPTION"); // define format menu asMenu.setMnemonic('S'); // shortcut desItem = new JMenuItem("DES"); // define format menu options desItem.setMnemonic('D'); desItem.addActionListener(this); asMenu.add(desItem); ... ... menuBar.add(helpMenu); // add help menu to menu bar setJMenuBar(menuBar); // put menu bar on application textColor = Color.RED; when from the Menu desitem is selected, desvar is just for not showing the panel multiple times, it calls Panels () else if(e.getSource() == desItem && desvar ==1 ) { // make other panels unvisible. if(hashvar!=1) MyPanel.setVisible(false);//hash functions if(rsavar!=1) MyPanel3.setVisible(false);//rsa function if (dhvar!=1) MyPanel4.setVisible(false);//dh diffie hellman hashvar=1; rsavar=1; dhvar=1; ++desvar; desPanel=true; Panels(); } and in Panels() Method: if (hashPanel){ MyPanel.add("West",radioSHA1); MyPanel.add("West",radioMD5); MyPanel.add("Center", inputField); MyPanel.add("East",SubmitButton); MyPanel.add("South",resultHash); add(MyPanel); validate(); hashPanel=false; } i have many panels for example : -hash functions=mypanel1 , des=mypanel2, rsa functions= mypanel3 , dh= mypanel4 However in may other panals such as rsa function: i have to use some layout properties of the java:in this panel i selected to use gridbaglayout if (dhPanel){ System.out.println("rsa panel burda misin"); MyPanel3.add(pqLabel); MyPanel3.add(pLabel); MyPanel3.add(pTextArea); MyPanel3.add(qLabel); ... ... add(MyPanel3); generate_pqButton.addActionListener(this); calculate_nButton.addActionListener(this); ... ... GridBagLayout layout = new GridBagLayout(); GridBagConstraints gbc = new GridBagConstraints(); setLayout(layout); // x, y, w, h, wx, wy gbc.fill = GridBagConstraints.NONE; add (bit_length_label, gbc, 0, 0, 1, 1, 0, 10); add (p, gbc, 0, 1, 1, 1, 0, 10); add (g, gbc, 0, 2, 1, 1, 0, 10); add (a, gbc, 1, 3, 1, 1, 100, 10); add (x, gbc, 0, 4, 1, 1, 0, 10); add (gx, gbc, 0, 5, 1, 1, 0, 10); add (gxy, gbc, 0, 6, 1, 1, 0, 10); add (b, gbc, 1, 7, 1, 1, 100, 10); add (y, gbc, 0, 8, 1, 1, 0, 10); add (gy, gbc, 0, 9, 1, 1, 0, 10); add (gyx, gbc, 0, 10, 1, 1, 0, 10); add (sk, gbc, 1, 11, 1, 1, 100, 10); add (key, gbc, 0, 12, 1, 1, 0, 10); add (status, gbc, 1, 13, 1, 1, 100, 10); add (dhstart, gbc, 1, 14, 1, 1, 100, 10); add (bit_length_value, gbc, 1, 0, 1, 1, 100, 10); add (p_value, gbc, 1, 1, 1, 1, 100, 10); add (g_value, gbc, 1, 2, 1, 1, 100, 10); add (x_value, gbc, 1, 4, 1, 1, 100, 10); add (gx_value, gbc, 1, 5, 1, 1, 100, 10); add (gxy_value, gbc, 1, 6, 1, 1, 100, 10); add (y_value, gbc, 1, 8, 1, 1, 100, 10); add (gy_value, gbc, 1, 9, 1, 1, 100, 10); add (gyx_value, gbc, 1, 10, 1, 1, 100, 10); validate(); repaint(); rsaPanel=false; } Everthing seems okey however when i swith from one menu to another sometimes components are seen or apper in wrong places or mixed. where i'm doing wrong?

    Read the article

  • I can''t figure out how to use the comboBox to remove radiobuttons

    - by user3576336
    import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.ButtonGroup; import javax.swing.JLabel; import javax.swing.JDesktopPane; import javax.swing.JRadioButton; import javax.swing.JComboBox; import java.awt.GridBagLayout; import java.awt.GridBagConstraints; import java.awt.Insets; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; @SuppressWarnings("serial") public class Calendar1 extends JFrame implements ActionListener { private JPanel contentPane; String[] Months = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; JDesktopPane desktopPane; ButtonGroup bG = new ButtonGroup(); JRadioButton radioButton, rdbtnNewRadioButton, radioButton_1, radioButton_2, radioButton_3, radioButton_4, radioButton_5, radioButton_6, radioButton_7, radioButton_8, radioButton_9, radioButton_10, radioButton_11, radioButton_12, radioButton_13, radioButton_14, radioButton_15, radioButton_16, radioButton_17, radioButton_18, radioButton_19, radioButton_20, radioButton_21, radioButton_22, radioButton_23, radioButton_24, radioButton_25, radioButton_26, radioButton_27, radioButton_28, radioButton_29; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Calendar1 frame = new Calendar1(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ @SuppressWarnings("unchecked") public Calendar1() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 521, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JComboBox comboBox = new JComboBox(Months); comboBox.setBounds(28, 16, 132, 27); contentPane.add(comboBox); comboBox.setSelectedIndex(0); comboBox.addActionListener(this); JLabel label = new JLabel("2014"); label.setBounds(350, 20, 61, 16); contentPane.add(label); desktopPane = new JDesktopPane(); desktopPane.setBackground(new Color(30, 144, 255)); desktopPane.setBounds(0, 63, 495, 188); contentPane.add(desktopPane); GridBagLayout gbl_desktopPane = new GridBagLayout(); gbl_desktopPane.columnWidths = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; gbl_desktopPane.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0 }; gbl_desktopPane.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE }; gbl_desktopPane.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE }; desktopPane.setLayout(gbl_desktopPane); // JLabel lblSun = new JLabel("Sun"); // lblSun.setForeground(Color.RED); // // GridBagConstraints gbc_lblSun = new GridBagConstraints(); // gbc_lblSun.insets = new Insets(0, 0, 5, 5); // gbc_lblSun.gridx = 2; // gbc_lblSun.gridy = 0; // desktopPane.add(lblSun, gbc_lblSun); // // JLabel lblMon = new JLabel("Mon"); // GridBagConstraints gbc_lblMon = new GridBagConstraints(); // gbc_lblMon.insets = new Insets(0, 0, 5, 5); // gbc_lblMon.gridx = 4; // gbc_lblMon.gridy = 0; // desktopPane.add(lblMon, gbc_lblMon); // // JLabel lblTues = new JLabel("Tues"); // GridBagConstraints gbc_lblTues = new GridBagConstraints(); // gbc_lblTues.insets = new Insets(0, 0, 5, 5); // gbc_lblTues.gridx = 6; // gbc_lblTues.gridy = 0; // desktopPane.add(lblTues, gbc_lblTues); // // JLabel lblWed = new JLabel("Wed"); // GridBagConstraints gbc_lblWed = new GridBagConstraints(); // gbc_lblWed.insets = new Insets(0, 0, 5, 5); // gbc_lblWed.gridx = 8; // gbc_lblWed.gridy = 0; // desktopPane.add(lblWed, gbc_lblWed); // // JLabel lblThurs = new JLabel("Thurs"); // GridBagConstraints gbc_lblThurs = new GridBagConstraints(); // gbc_lblThurs.insets = new Insets(0, 0, 5, 5); // gbc_lblThurs.gridx = 10; // gbc_lblThurs.gridy = 0; // desktopPane.add(lblThurs, gbc_lblThurs); // // JLabel lblFri = new JLabel("Friday"); // GridBagConstraints gbc_lblFri = new GridBagConstraints(); // gbc_lblFri.insets = new Insets(0, 0, 5, 5); // gbc_lblFri.gridx = 12; // gbc_lblFri.gridy = 0; // desktopPane.add(lblFri, gbc_lblFri); // // JLabel lblSat = new JLabel("Sat"); // lblSat.setForeground(Color.RED); // GridBagConstraints gbc_lblSat = new GridBagConstraints(); // gbc_lblSat.insets = new Insets(0, 0, 5, 5); // gbc_lblSat.gridx = 14; // gbc_lblSat.gridy = 0; // desktopPane.add(lblSat, gbc_lblSat); radioButton = new JRadioButton("1"); GridBagConstraints gbc_radioButton = new GridBagConstraints(); gbc_radioButton.insets = new Insets(0, 0, 5, 5); gbc_radioButton.gridx = 8; gbc_radioButton.gridy = 1; desktopPane.add(radioButton, gbc_radioButton); bG.add(radioButton); rdbtnNewRadioButton = new JRadioButton("2"); GridBagConstraints gbc_rdbtnNewRadioButton = new GridBagConstraints(); gbc_rdbtnNewRadioButton.insets = new Insets(0, 0, 5, 5); gbc_rdbtnNewRadioButton.gridx = 10; gbc_rdbtnNewRadioButton.gridy = 1; desktopPane.add(rdbtnNewRadioButton, gbc_rdbtnNewRadioButton); bG.add(rdbtnNewRadioButton); radioButton_1 = new JRadioButton("3"); GridBagConstraints gbc_radioButton_1 = new GridBagConstraints(); gbc_radioButton_1.insets = new Insets(0, 0, 5, 5); gbc_radioButton_1.gridx = 12; gbc_radioButton_1.gridy = 1; desktopPane.add(radioButton_1, gbc_radioButton_1); bG.add(radioButton_1); radioButton_2 = new JRadioButton("4"); GridBagConstraints gbc_radioButton_2 = new GridBagConstraints(); gbc_radioButton_2.insets = new Insets(0, 0, 5, 5); gbc_radioButton_2.gridx = 14; gbc_radioButton_2.gridy = 1; desktopPane.add(radioButton_2, gbc_radioButton_2); bG.add(radioButton_2); radioButton_3 = new JRadioButton("5"); GridBagConstraints gbc_radioButton_3 = new GridBagConstraints(); gbc_radioButton_3.insets = new Insets(0, 0, 5, 5); gbc_radioButton_3.gridx = 2; gbc_radioButton_3.gridy = 2; desktopPane.add(radioButton_3, gbc_radioButton_3); bG.add(radioButton_3); radioButton_4 = new JRadioButton("6"); GridBagConstraints gbc_radioButton_4 = new GridBagConstraints(); gbc_radioButton_4.insets = new Insets(0, 0, 5, 5); gbc_radioButton_4.gridx = 4; gbc_radioButton_4.gridy = 2; desktopPane.add(radioButton_4, gbc_radioButton_4); bG.add(radioButton_4); radioButton_5 = new JRadioButton("7"); GridBagConstraints gbc_radioButton_5 = new GridBagConstraints(); gbc_radioButton_5.insets = new Insets(0, 0, 5, 5); gbc_radioButton_5.gridx = 6; gbc_radioButton_5.gridy = 2; desktopPane.add(radioButton_5, gbc_radioButton_5); bG.add(radioButton_5); radioButton_6 = new JRadioButton("8"); GridBagConstraints gbc_radioButton_6 = new GridBagConstraints(); gbc_radioButton_6.insets = new Insets(0, 0, 5, 5); gbc_radioButton_6.gridx = 8; gbc_radioButton_6.gridy = 2; desktopPane.add(radioButton_6, gbc_radioButton_6); bG.add(radioButton_6); radioButton_7 = new JRadioButton("9"); GridBagConstraints gbc_radioButton_7 = new GridBagConstraints(); gbc_radioButton_7.insets = new Insets(0, 0, 5, 5); gbc_radioButton_7.gridx = 10; gbc_radioButton_7.gridy = 2; desktopPane.add(radioButton_7, gbc_radioButton_7); bG.add(radioButton_7); radioButton_8 = new JRadioButton("10"); GridBagConstraints gbc_radioButton_8 = new GridBagConstraints(); gbc_radioButton_8.insets = new Insets(0, 0, 5, 5); gbc_radioButton_8.gridx = 12; gbc_radioButton_8.gridy = 2; desktopPane.add(radioButton_8, gbc_radioButton_8); bG.add(radioButton_8); radioButton_9 = new JRadioButton("11"); GridBagConstraints gbc_radioButton_9 = new GridBagConstraints(); gbc_radioButton_9.insets = new Insets(0, 0, 5, 5); gbc_radioButton_9.gridx = 14; gbc_radioButton_9.gridy = 2; desktopPane.add(radioButton_9, gbc_radioButton_9); bG.add(radioButton_9); radioButton_10 = new JRadioButton("12"); GridBagConstraints gbc_radioButton_10 = new GridBagConstraints(); gbc_radioButton_10.insets = new Insets(0, 0, 5, 5); gbc_radioButton_10.gridx = 2; gbc_radioButton_10.gridy = 3; desktopPane.add(radioButton_10, gbc_radioButton_10); bG.add(radioButton_10); radioButton_11 = new JRadioButton("13"); GridBagConstraints gbc_radioButton_11 = new GridBagConstraints(); gbc_radioButton_11.insets = new Insets(0, 0, 5, 5); gbc_radioButton_11.gridx = 4; gbc_radioButton_11.gridy = 3; desktopPane.add(radioButton_11, gbc_radioButton_11); bG.add(radioButton_11); radioButton_12 = new JRadioButton("14"); GridBagConstraints gbc_radioButton_12 = new GridBagConstraints(); gbc_radioButton_12.insets = new Insets(0, 0, 5, 5); gbc_radioButton_12.gridx = 6; gbc_radioButton_12.gridy = 3; desktopPane.add(radioButton_12, gbc_radioButton_12); bG.add(radioButton_12); radioButton_13 = new JRadioButton("15"); GridBagConstraints gbc_radioButton_13 = new GridBagConstraints(); gbc_radioButton_13.insets = new Insets(0, 0, 5, 5); gbc_radioButton_13.gridx = 8; gbc_radioButton_13.gridy = 3; desktopPane.add(radioButton_13, gbc_radioButton_13); bG.add(radioButton_13); radioButton_14 = new JRadioButton("16"); GridBagConstraints gbc_radioButton_14 = new GridBagConstraints(); gbc_radioButton_14.insets = new Insets(0, 0, 5, 5); gbc_radioButton_14.gridx = 10; gbc_radioButton_14.gridy = 3; desktopPane.add(radioButton_14, gbc_radioButton_14); bG.add(radioButton_14); radioButton_15 = new JRadioButton("17"); GridBagConstraints gbc_radioButton_15 = new GridBagConstraints(); gbc_radioButton_15.insets = new Insets(0, 0, 5, 5); gbc_radioButton_15.gridx = 12; gbc_radioButton_15.gridy = 3; desktopPane.add(radioButton_15, gbc_radioButton_15); bG.add(radioButton_15); radioButton_16 = new JRadioButton("18"); GridBagConstraints gbc_radioButton_16 = new GridBagConstraints(); gbc_radioButton_16.insets = new Insets(0, 0, 5, 5); gbc_radioButton_16.gridx = 14; gbc_radioButton_16.gridy = 3; desktopPane.add(radioButton_16, gbc_radioButton_16); bG.add(radioButton_16); radioButton_17 = new JRadioButton("19"); GridBagConstraints gbc_radioButton_17 = new GridBagConstraints(); gbc_radioButton_17.insets = new Insets(0, 0, 5, 5); gbc_radioButton_17.gridx = 2; gbc_radioButton_17.gridy = 4; desktopPane.add(radioButton_17, gbc_radioButton_17); bG.add(radioButton_17); radioButton_18 = new JRadioButton("20"); GridBagConstraints gbc_radioButton_18 = new GridBagConstraints(); gbc_radioButton_18.insets = new Insets(0, 0, 5, 5); gbc_radioButton_18.gridx = 4; gbc_radioButton_18.gridy = 4; desktopPane.add(radioButton_18, gbc_radioButton_18); bG.add(radioButton_18); radioButton_19 = new JRadioButton("21"); GridBagConstraints gbc_radioButton_19 = new GridBagConstraints(); gbc_radioButton_19.insets = new Insets(0, 0, 5, 5); gbc_radioButton_19.gridx = 6; gbc_radioButton_19.gridy = 4; desktopPane.add(radioButton_19, gbc_radioButton_19); bG.add(radioButton_19); radioButton_20 = new JRadioButton("22"); GridBagConstraints gbc_radioButton_20 = new GridBagConstraints(); gbc_radioButton_20.insets = new Insets(0, 0, 5, 5); gbc_radioButton_20.gridx = 8; gbc_radioButton_20.gridy = 4; desktopPane.add(radioButton_20, gbc_radioButton_20); bG.add(radioButton_20); radioButton_21 = new JRadioButton("23"); GridBagConstraints gbc_radioButton_21 = new GridBagConstraints(); gbc_radioButton_21.insets = new Insets(0, 0, 5, 5); gbc_radioButton_21.gridx = 10; gbc_radioButton_21.gridy = 4; desktopPane.add(radioButton_21, gbc_radioButton_21); bG.add(radioButton_21); radioButton_22 = new JRadioButton("24"); GridBagConstraints gbc_radioButton_22 = new GridBagConstraints(); gbc_radioButton_22.insets = new Insets(0, 0, 5, 5); gbc_radioButton_22.gridx = 12; gbc_radioButton_22.gridy = 4; desktopPane.add(radioButton_22, gbc_radioButton_22); bG.add(radioButton_22); radioButton_23 = new JRadioButton("25"); GridBagConstraints gbc_radioButton_23 = new GridBagConstraints(); gbc_radioButton_23.insets = new Insets(0, 0, 5, 5); gbc_radioButton_23.gridx = 14; gbc_radioButton_23.gridy = 4; desktopPane.add(radioButton_23, gbc_radioButton_23); bG.add(radioButton_23); radioButton_24 = new JRadioButton("26"); GridBagConstraints gbc_radioButton_24 = new GridBagConstraints(); gbc_radioButton_24.insets = new Insets(0, 0, 5, 5); gbc_radioButton_24.gridx = 2; gbc_radioButton_24.gridy = 5; desktopPane.add(radioButton_24, gbc_radioButton_24); bG.add(radioButton_24); radioButton_25 = new JRadioButton("27"); GridBagConstraints gbc_radioButton_25 = new GridBagConstraints(); gbc_radioButton_25.insets = new Insets(0, 0, 5, 5); gbc_radioButton_25.gridx = 4; gbc_radioButton_25.gridy = 5; desktopPane.add(radioButton_25, gbc_radioButton_25); bG.add(radioButton_25); radioButton_26 = new JRadioButton("28"); GridBagConstraints gbc_radioButton_26 = new GridBagConstraints(); gbc_radioButton_26.insets = new Insets(0, 0, 5, 5); gbc_radioButton_26.gridx = 6; gbc_radioButton_26.gridy = 5; desktopPane.add(radioButton_26, gbc_radioButton_26); bG.add(radioButton_26); radioButton_27 = new JRadioButton("29"); GridBagConstraints gbc_radioButton_27 = new GridBagConstraints(); gbc_radioButton_27.insets = new Insets(0, 0, 5, 5); gbc_radioButton_27.gridx = 8; gbc_radioButton_27.gridy = 5; desktopPane.add(radioButton_27, gbc_radioButton_27); bG.add(radioButton_27); radioButton_28 = new JRadioButton("30"); GridBagConstraints gbc_radioButton_28 = new GridBagConstraints(); gbc_radioButton_28.insets = new Insets(0, 0, 5, 5); gbc_radioButton_28.gridx = 10; gbc_radioButton_28.gridy = 5; desktopPane.add(radioButton_28, gbc_radioButton_28); bG.add(radioButton_28); radioButton_29 = new JRadioButton("31"); GridBagConstraints gbc_radioButton_29 = new GridBagConstraints(); gbc_radioButton_29.insets = new Insets(0, 0, 5, 5); gbc_radioButton_29.gridx = 12; gbc_radioButton_29.gridy = 5; desktopPane.add(radioButton_29, gbc_radioButton_29); bG.add(radioButton_29); } @Override public void actionPerformed(ActionEvent e) { JComboBox cb = (JComboBox) e.getSource(); String months = (String) cb.getSelectedItem(); if (months.equals("February")) { desktopPane.remove(radioButton_28); desktopPane.revalidate(); } } } I'm trying to use the combobox to remove radiobuttons in the actionperformed, but when I run the program, nothing happens, nor can I enable new buttons in the actionperformed. Thank you so much in advance for helping me out.

    Read the article

  • while loop ignore the event listener

    - by Tamer
    so when i run this code to try to change the background the GUI crashes and gets stuck in a infinite while loop ignoring the event listeners. here is the code: `private Panel getPanel1() { if (panel1 == null) { panel1 = new Panel(); panel1.setLayout(new GridBagLayout()); while(frame.isVisible()){ panel1.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent e) { frame.setVisible(false); } }); int r = (int) (Math.random()*255); int g = (int) (Math.random()*255); int b = (int) (Math.random()*255); Color c = new Color(r, g, b); panel1.setBackground(c); try { Thread.sleep(4000); } catch (InterruptedException e1) { e1.printStackTrace(); } panel1.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent e) { /*panel1.setVisible(false); frame.setVisible(false);*/ System.exit(0); } }); } } return panel1; }` instead of exiting the loop of terminating the program or event changing the background it just displays the panel and does nothing else and i have to force it to quit. what should i do?

    Read the article

  • Why Look and feel is not getting updated properly?

    - by swift
    I’m developing a swing application in which I have an option to change the Look and feel of the application on click of a button. Now my problem is when I click the button to change the theme it’s not properly updating the L&F of my app, say my previous theme is “noire” and I choose “MCWin” after it, but the style of the noire theme is still there Here is sample working code: package whiteboard; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ComponentEvent; import java.awt.event.ComponentListener; import javax.swing.JFrame; import javax.swing.JLayeredPane; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.WindowConstants; public class DiscussionBoard extends JFrame implements ComponentListener,ActionListener { // Variables declaration private JMenuItem audioMenuItem; private JMenuItem boardMenuItem; private JMenuItem exitMenuItem; private JMenuItem clientsMenuItem; private JMenuItem acryl; private JMenuItem hifi; private JMenuItem aero; private JMenuItem aluminium; private JMenuItem bernstein; private JMenuItem fast; private JMenuItem graphite; private JMenuItem luna; private JMenuItem mcwin; private JMenuItem noire; private JMenuItem smart; private JMenuBar boardMenuBar; private JMenuItem messengerMenuItem; private JMenu openMenu; private JMenu saveMenu; private JMenu themesMenu; private JMenuItem saveMessengerMenuItem; private JMenuItem saveWhiteboardMenuItem; private JMenu userMenu; JLayeredPane layerpane; /** Creates new form discussionBoard * @param connection */ public DiscussionBoard() { initComponents(); setLocationRelativeTo(null); addComponentListener(this); } private void initComponents() { boardMenuBar = new JMenuBar(); openMenu = new JMenu(); themesMenu = new JMenu(); messengerMenuItem = new JMenuItem(); boardMenuItem = new JMenuItem(); audioMenuItem = new JMenuItem(); saveMenu = new JMenu(); saveMessengerMenuItem = new JMenuItem(); saveWhiteboardMenuItem = new JMenuItem(); userMenu = new JMenu(); clientsMenuItem = new JMenuItem(); exitMenuItem = new JMenuItem(); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setLayout(new GridBagLayout()); setResizable(false); setTitle("Discussion Board"); openMenu.setText("Open"); saveMenu.setText("Save"); themesMenu.setText("Themes"); acryl = new JMenuItem("Acryl"); hifi = new JMenuItem("HiFi"); aero = new JMenuItem("Aero"); aluminium = new JMenuItem("Aluminium"); bernstein = new JMenuItem("Bernstein"); fast = new JMenuItem("Fast"); graphite = new JMenuItem("Graphite"); luna = new JMenuItem("Luna"); mcwin = new JMenuItem("MCwin"); noire = new JMenuItem("Noire"); smart = new JMenuItem("Smart"); hifi.addActionListener(this); acryl.addActionListener(this); aero.addActionListener(this); aluminium.addActionListener(this); bernstein.addActionListener(this); fast.addActionListener(this); graphite.addActionListener(this); luna.addActionListener(this); mcwin.addActionListener(this); noire.addActionListener(this); smart.addActionListener(this); messengerMenuItem.setText("Messenger"); openMenu.add(messengerMenuItem); openMenu.add(boardMenuItem); audioMenuItem.setText("Audio Messenger"); openMenu.add(audioMenuItem); exitMenuItem.setText("Exit"); exitMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { exitMenuItemActionPerformed(evt); } }); openMenu.add(exitMenuItem); boardMenuBar.add(openMenu); saveMessengerMenuItem.setText("Messenger"); saveMenu.add(saveMessengerMenuItem); saveWhiteboardMenuItem.setText("Whiteboard"); saveMenu.add(saveWhiteboardMenuItem); boardMenuBar.add(saveMenu); userMenu.setText("Users"); clientsMenuItem.setText("Current Session"); userMenu.add(clientsMenuItem); themesMenu.add(acryl); themesMenu.add(hifi); themesMenu.add(aero); themesMenu.add(aluminium); themesMenu.add(bernstein); themesMenu.add(fast); themesMenu.add(graphite); themesMenu.add(luna); themesMenu.add(mcwin); themesMenu.add(noire); themesMenu.add(smart); boardMenuBar.add(userMenu); boardMenuBar.add(themesMenu); saveMessengerMenuItem.setEnabled(false); saveWhiteboardMenuItem.setEnabled(false); setJMenuBar(boardMenuBar); setSize(1024, 740); setVisible(true); } protected void exitMenuItemActionPerformed(ActionEvent evt) { System.exit(0); } @Override public void componentHidden(ComponentEvent arg0) { } @Override public void componentMoved(ComponentEvent e) { } @Override public void componentResized(ComponentEvent arg0) { } @Override public void componentShown(ComponentEvent arg0) { } @Override public void actionPerformed(ActionEvent e) { try { if(e.getSource()==hifi) { UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); SwingUtilities.updateComponentTreeUI(getRootPane()); UIManager.setLookAndFeel("com.jtattoo.plaf.hifi.HiFiLookAndFeel"); enableTheme(); hifi.setEnabled(false); } else if(e.getSource()==acryl) { UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); SwingUtilities.updateComponentTreeUI(getRootPane()); UIManager.setLookAndFeel("com.jtattoo.plaf.acryl.AcrylLookAndFeel"); enableTheme(); acryl.setEnabled(false); } else if(e.getSource()==aero) { UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); SwingUtilities.updateComponentTreeUI(getRootPane()); UIManager.setLookAndFeel("com.jtattoo.plaf.aero.AeroLookAndFeel"); enableTheme(); aero.setEnabled(false); } else if(e.getSource()==aluminium) { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); SwingUtilities.updateComponentTreeUI(getRootPane()); UIManager.setLookAndFeel("com.jtattoo.plaf.aluminium.AluminiumLookAndFeel"); enableTheme(); aluminium.setEnabled(false); } else if(e.getSource()==bernstein) { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); SwingUtilities.updateComponentTreeUI(getRootPane()); UIManager.setLookAndFeel("com.jtattoo.plaf.bernstein.BernsteinLookAndFeel"); enableTheme(); bernstein.setEnabled(false); } else if(e.getSource()==fast) { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); SwingUtilities.updateComponentTreeUI(getRootPane()); UIManager.setLookAndFeel("com.jtattoo.plaf.fast.FastLookAndFeel"); enableTheme(); fast.setEnabled(false); } else if(e.getSource()==graphite) { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); SwingUtilities.updateComponentTreeUI(getRootPane()); UIManager.setLookAndFeel("com.jtattoo.plaf.graphite.GraphiteLookAndFeel"); enableTheme(); graphite.setEnabled(false); } else if(e.getSource()==luna) { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); SwingUtilities.updateComponentTreeUI(getRootPane()); UIManager.setLookAndFeel("com.jtattoo.plaf.luna.LunaLookAndFeel"); enableTheme(); luna.setEnabled(false); } else if(e.getSource()==mcwin) { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); SwingUtilities.updateComponentTreeUI(getRootPane()); UIManager.setLookAndFeel("com.jtattoo.plaf.mcwin.McWinLookAndFeel"); enableTheme(); mcwin.setEnabled(false); } else if(e.getSource()==noire) { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); SwingUtilities.updateComponentTreeUI(getRootPane()); UIManager.setLookAndFeel("com.jtattoo.plaf.noire.NoireLookAndFeel"); enableTheme(); noire.setEnabled(false); } else if(e.getSource()==smart) { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); SwingUtilities.updateComponentTreeUI(getRootPane()); UIManager.setLookAndFeel("com.jtattoo.plaf.smart.SmartLookAndFeel"); enableTheme(); smart.setEnabled(false); } SwingUtilities.updateComponentTreeUI(getRootPane()); } catch (Exception ex) { ex.printStackTrace(); } } private void enableTheme() { acryl.setEnabled(true); hifi.setEnabled(true); aero.setEnabled(true); aluminium.setEnabled(true); bernstein.setEnabled(true); fast.setEnabled(true); graphite.setEnabled(true); luna.setEnabled(true); mcwin.setEnabled(true); noire.setEnabled(true); smart.setEnabled(true); } public static void main(String []ar) { try { UIManager.setLookAndFeel("com.jtattoo.plaf.acryl.AcrylLookAndFeel"); } catch (Exception e) { e.printStackTrace(); } new DiscussionBoard(); } } What’s the problem here? why its not getting updated? There is a demo application here which is exactly doing what i want but i cant get a clear idea of it.

    Read the article

1 2  | Next Page >